def archive(dirpath, zfile, force=False, include=None, exclude=None): import zipfile logger = manager.app.logger dirpath = _exp(dirpath) filepaths = list(util.file_paths(dirpath, include, exclude)) def _up_to_date(): if not os.path.exists(zfile): return False ziptime = os.path.getmtime(zfile) return not any(os.path.getmtime(p) > ziptime for p in filepaths) # Don't bother archiving if zip is up-to-date if not force and _up_to_date(): return logger.info("Archiving directory %s to file %s", dirpath, zfile) t = util.perf_counter() count = 0 zf = zipfile.ZipFile(zfile, "w", compression=zipfile.ZIP_DEFLATED) for path in filepaths: rp = os.path.relpath(path, dirpath).replace("\\", "/") zf.write(path, arcname=rp) logger.debug("Adding %s", path) count += 1 logger.info("Archived %s files in %.01f sec", count, util.perf_counter() - t) zf.close()
def _copy_tree(srcdir, destdir, force=False, include=None, exclude=None, logger=None): count = 0 for srcpath in util.file_paths(srcdir, include, exclude): relpath = os.path.relpath(srcpath, srcdir) destpath = os.path.join(destdir, relpath) if _copy_file(srcpath, destpath, force, logger=logger): count += 1 return count