# __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, 12, 28, 43), 'glob.glob', 'glob', ({(28, 17, 28, 42): "'%s/*' % settings.TMP_DIR"}, {}), "('%s/*' % settings.TMP_DIR)", False, 'from glob import glob\n'), ((29, 10, 29, 21), 'time.time', 'time.time', ({}, {}), '()', False, 'import time\n'), ((46, 4, 46, 22), 'geocamUtil.FileUtil.mkdirP', 'FileUtil.mkdirP', ({(46, 20, 46, 21): 'd'}, {}), '(d)', False, 'from geocamUtil import FileUtil\n'), ((56, 13, 56, 24), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((59, 4, 59, 20), 'os.chdir', 'os.chdir', ({(59, 13, 59, 19): 'oldDir'}, {}), '(oldDir)', False, 'import os\n'), ((60, 4, 60, 25), 'shutil.rmtree', 'shutil.rmtree', ({(60, 18, 60, 24): 'zipDir'}, {}), '(zipDir)', False, 'import shutil\n'), ((42, 11, 42, 43), 'os.path.exists', 'os.path.exists', ({(42, 26, 42, 42): 'settings.TMP_DIR'}, {}), '(settings.TMP_DIR)', False, 'import os\n'), ((43, 8, 43, 41), 'geocamUtil.FileUtil.mkdirP', 'FileUtil.mkdirP', ({(43, 24, 43, 40): 'settings.TMP_DIR'}, {}), '(settings.TMP_DIR)', False, 'from geocamUtil import FileUtil\n'), ((44, 8, 44, 54), 'os.system', 'os.system', ({(44, 18, 44, 53): "('chmod go+rw %s' % settings.TMP_DIR)"}, {}), "('chmod go+rw %s' % settings.TMP_DIR)", False, 'import os\n'), ((57, 13, 57, 46), 'os.path.dirname', 'os.path.dirname', ({(57, 29, 57, 45): 'settings.TMP_DIR'}, {}), '(settings.TMP_DIR)', False, 'import os\n'), ((22, 30, 22, 60), 'time.strftime', 'time.strftime', ({(22, 44, 22, 59): '"""%Y-%m-%d-%H%M"""'}, {}), "('%Y-%m-%d-%H%M')", False, 'import time\n'), ((23, 39, 23, 61), 'random.getrandbits', 'random.getrandbits', ({(23, 58, 23, 60): '(16)'}, {}), '(16)', False, 'import random\n'), ((34, 16, 34, 28), 'os.unlink', 'os.unlink', ({(34, 26, 34, 27): 'f'}, {}), '(f)', False, 'import os\n'), ((58, 41, 58, 65), 'os.path.basename', 'os.path.basename', ({(58, 58, 58, 64): 'zipDir'}, {}), '(zipDir)', False, 'import os\n'), ((36, 16, 36, 37), 'traceback.print_exc', 'traceback.print_exc', ({}, {}), '()', False, 'import traceback\n'), ((31, 18, 31, 28), 'os.stat', 'os.stat', ({(31, 26, 31, 27): 'f'}, {}), '(f)', False, 'import os\n')]