예제 #1
0
def unzip_excerpt_files(auxDir):
    if not os.path.exists(auxDir):
        return True

    exDir = os.path.join(auxDir, 'excerpts')
    if not os.path.exists(exDir):
        return True

    exNames = os.listdir(exDir)
    if len(exNames) == 0:
        return True

    for exName in exNames:
        try:
            exPath = os.path.join(exDir, exName)
            pair = os.path.splitext(exName)
            ext = string.capwords(pair[1])
            if ext != '.zip':
                continue
            su.zipfile_extractall(exPath, exDir)
            logging.debug('unzip_excerpt_files: successfully unzipped ' +
                          exPath)
        except:
            ryw.give_bad_news('unzip_excerpt_files: failed to unzip ' + exPath,
                              logging.error)

    return True
예제 #2
0
def try_unzip_file(form, tmpdir, filename, kB):
    """unzip the unploaded zip file.
    returns a tuple: the stuff to upload, and a temp extract directory
    that should be cleaned up."""
    
    extractDir = tmpdir + '.EXT'

    unzip = form.getfirst('unzip', '')
    
    if not unzip:
        return (os.path.join(tmpdir, filename), None)

    freeKB = ryw.free_MB(tmpdir) * 1024
    if pretty_much_out_of_space(kB, freeKB):
        ryw.give_bad_news(
            'user_error: nearly out of space while trying to unzip, zip file kB: ' + repr(kB), logging.error)
        return (None, extractDir)

    try:
        os.mkdir(extractDir)
    except:
        ryw.give_bad_news(
            'fatal_error: failed to make temporary extract directory.',
            logging.critical)
        return (None, extractDir)

    try:
        su.zipfile_extractall(os.path.join(tmpdir, filename), extractDir)
    except:
        ryw.give_bad_news('fatal_error: failed to extract zip file.',
                          logging.critical)
        return (None, extractDir)

    logging.debug('successfully extracted zipfile ' + filename + ' to ' +
                  extractDir)
    return (extractDir, extractDir)