def create_target_dir(destdir, srcfile):
    """create zip folder structure in tmp location.
    return root folder
    """
    time_, gcm, emsc, year, var, res, _ = parse_zip_filename(srcfile)
    if time_ == 'current':
        dirname = '_'.join(('worldclim', res, var))
    else:
        dirname = '_'.join((emsc, gcm, str(year), res, var))
    root = os.path.join(destdir, time_, dirname)
    return ensure_directory(root)
def main():
    opts = parse_args()
    srcdir = os.path.abspath(opts.srcdir)

    workdir = ensure_directory(opts.workdir)
    dest = ensure_directory(opts.destdir)

    if os.path.isdir(srcdir):
        srcfiles = sorted(glob.glob(os.path.join(srcdir, '*.zip')))
    else:
        srcfiles = [srcfiles]

    # unpack contains one destination datasets
    for srcfile in tqdm.tqdm(srcfiles):
        target_work_dir = create_target_dir(workdir, srcfile)
        try:
            convert(srcfile, target_work_dir)
            target_dir = create_target_dir(dest, srcfile)
            move_files(target_work_dir, target_dir)
        finally:
            shutil.rmtree(target_work_dir)
def main():
    opts = parse_args()
    srcfile = os.path.abspath(opts.source)
    if os.path.isdir(srcfile):
        srcfiles = sorted(glob.glob(os.path.join(srcfile, '*.zip')))
    else:
        srcfiles = [srcfile]

    workdir = ensure_directory(opts.workdir)
    dest = ensure_directory(opts.destdir)
    # unpack contains one destination datasets
    for srcfile in tqdm.tqdm(srcfiles):
        target_work_dir = create_target_dir(workdir, srcfile)
        try:
            # convert files into workdir
            convert(srcfile, target_work_dir)
            # move results to dest
            target_dir = create_target_dir(dest, srcfile)
            move_files(target_work_dir, target_dir)
        finally:
            # cleanup
            shutil.rmtree(target_work_dir)
def main():
    """
    main method
    """
    opts = parse_args()
    src = os.path.abspath(opts.srcdir)

    workdir = ensure_directory(opts.workdir)
    dest = ensure_directory(opts.destdir)

    if os.path.isdir(src):
        if opts.resolution:
            # Note: this regexp works only for the current naming scheme of
            #       worldclim 1.4 files
            fmatch = re.compile(r'|'.join(opts.resolution))
            srcfiles = sorted(name for name in glob.glob(
                os.path.join(src, '**/*.zip'), recursive=True)
                              if fmatch.search(name))
        else:
            srcfiles = sorted(
                glob.glob(os.path.join(src, '**/*.zip'), recursive=True))
    else:
        srcfiles = [src]

    for srcfile in tqdm.tqdm(srcfiles):
        target_work_dir = create_target_dir(workdir, srcfile)
        try:
            # convert files into workdir
            convert(srcfile, target_work_dir)
            # move results into destination
            target_dir = create_target_dir(dest, srcfile)
            move_files(target_work_dir, target_dir)
        finally:
            # cleanup target_work_dir
            # TODO: this cleans only lowest level subdir, and leaves
            #       intermediary dirs
            shutil.rmtree(target_work_dir)