# __BEGIN_LICENSE__
#Copyright (c) 2015, United States Government, as represented by the 
#Administrator of the National Aeronautics and Space Administration. 
#All rights reserved.
# __END_LICENSE__

import os
import time
import random
import shutil
from glob import glob
import traceback
import sys

from geocamUtil import FileUtil
from django.conf import settings


def getTempName(prefix, suffix=''):
    return '%s/%s-%s-%s%s' % (settings.TMP_DIR,
                              prefix,
                              time.strftime('%Y-%m-%d-%H%M'),
                              '%04x' % random.getrandbits(16),
                              suffix)


def deleteStaleFiles():
    files = glob('%s/*' % settings.TMP_DIR)
    now = time.time()
    for f in files:
        if (now - os.stat(f).st_ctime > settings.GEOCAM_UTIL_DELETE_TMP_FILE_WAIT_SECONDS and
                not f.endswith('/README.txt')):
            try:
                os.unlink(f)
            except OSError:
                traceback.print_exc()
                print >> sys.stderr, '[tempfiles.deleteStaleFiles: could not unlink %s]' % f


def makeTempDir(prefix):
    d = getTempName(prefix)
    if not os.path.exists(settings.TMP_DIR):
        FileUtil.mkdirP(settings.TMP_DIR)
        os.system('chmod go+rw %s' % settings.TMP_DIR)
    deleteStaleFiles()
    FileUtil.mkdirP(d)
    return d


def initZipDir(prefix):
    return makeTempDir(prefix)


def finishZipDir(zipDir):
    zipFile = '%s.zip' % zipDir
    oldDir = os.getcwd()
    os.chdir(os.path.dirname(settings.TMP_DIR))
    os.system('zip -r %s %s' % (zipFile, os.path.basename(zipDir)))
    os.chdir(oldDir)
    shutil.rmtree(zipDir)
    return zipFile

[(28, 'glob.glob', 'glob', (["('%s/*' % settings.TMP_DIR)"], {}), False, 'from glob import glob\n'), (29, 'time.time', 'time.time', ([], {}), False, 'import time\n'), (46, 'geocamUtil.FileUtil.mkdirP', 'FileUtil.mkdirP', (['d'], {}), False, 'from geocamUtil import FileUtil\n'), (56, 'os.getcwd', 'os.getcwd', ([], {}), False, 'import os\n'), (59, 'os.chdir', 'os.chdir', (['oldDir'], {}), False, 'import os\n'), (60, 'shutil.rmtree', 'shutil.rmtree', (['zipDir'], {}), False, 'import shutil\n'), (42, 'os.path.exists', 'os.path.exists', (['settings.TMP_DIR'], {}), False, 'import os\n'), (43, 'geocamUtil.FileUtil.mkdirP', 'FileUtil.mkdirP', (['settings.TMP_DIR'], {}), False, 'from geocamUtil import FileUtil\n'), (44, 'os.system', 'os.system', (["('chmod go+rw %s' % settings.TMP_DIR)"], {}), False, 'import os\n'), (57, 'os.path.dirname', 'os.path.dirname', (['settings.TMP_DIR'], {}), False, 'import os\n'), (22, 'time.strftime', 'time.strftime', (['"""%Y-%m-%d-%H%M"""'], {}), False, 'import time\n'), (23, 'random.getrandbits', 'random.getrandbits', (['(16)'], {}), False, 'import random\n'), (34, 'os.unlink', 'os.unlink', (['f'], {}), False, 'import os\n'), (58, 'os.path.basename', 'os.path.basename', (['zipDir'], {}), False, 'import os\n'), (36, 'traceback.print_exc', 'traceback.print_exc', ([], {}), False, 'import traceback\n'), (31, 'os.stat', 'os.stat', (['f'], {}), False, 'import os\n')]