Create a zip file from all the files under 'base_dir'. The output zip file will be named 'base_name' + ".zip". Returns the name of the output zip file.
(base_name, base_dir, verbose=0, dry_run=0,
logger=None, owner=None, group=None, root_dir=None)
| 995 | return archive_name |
| 996 | |
| 997 | def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, |
| 998 | logger=None, owner=None, group=None, root_dir=None): |
| 999 | """Create a zip file from all the files under 'base_dir'. |
| 1000 | |
| 1001 | The output zip file will be named 'base_name' + ".zip". Returns the |
| 1002 | name of the output zip file. |
| 1003 | """ |
| 1004 | import zipfile # late import for breaking circular dependency |
| 1005 | |
| 1006 | zip_filename = base_name + ".zip" |
| 1007 | archive_dir = os.path.dirname(base_name) |
| 1008 | |
| 1009 | if archive_dir and not os.path.exists(archive_dir): |
| 1010 | if logger is not None: |
| 1011 | logger.info("creating %s", archive_dir) |
| 1012 | if not dry_run: |
| 1013 | os.makedirs(archive_dir) |
| 1014 | |
| 1015 | if logger is not None: |
| 1016 | logger.info("creating '%s' and adding '%s' to it", |
| 1017 | zip_filename, base_dir) |
| 1018 | |
| 1019 | if not dry_run: |
| 1020 | with zipfile.ZipFile(zip_filename, "w", |
| 1021 | compression=zipfile.ZIP_DEFLATED) as zf: |
| 1022 | arcname = os.path.normpath(base_dir) |
| 1023 | if root_dir is not None: |
| 1024 | base_dir = os.path.join(root_dir, base_dir) |
| 1025 | base_dir = os.path.normpath(base_dir) |
| 1026 | if arcname != os.curdir: |
| 1027 | zf.write(base_dir, arcname) |
| 1028 | if logger is not None: |
| 1029 | logger.info("adding '%s'", base_dir) |
| 1030 | for dirpath, dirnames, filenames in os.walk(base_dir): |
| 1031 | arcdirpath = dirpath |
| 1032 | if root_dir is not None: |
| 1033 | arcdirpath = os.path.relpath(arcdirpath, root_dir) |
| 1034 | arcdirpath = os.path.normpath(arcdirpath) |
| 1035 | for name in sorted(dirnames): |
| 1036 | path = os.path.join(dirpath, name) |
| 1037 | arcname = os.path.join(arcdirpath, name) |
| 1038 | zf.write(path, arcname) |
| 1039 | if logger is not None: |
| 1040 | logger.info("adding '%s'", path) |
| 1041 | for name in filenames: |
| 1042 | path = os.path.join(dirpath, name) |
| 1043 | path = os.path.normpath(path) |
| 1044 | if os.path.isfile(path): |
| 1045 | arcname = os.path.join(arcdirpath, name) |
| 1046 | zf.write(path, arcname) |
| 1047 | if logger is not None: |
| 1048 | logger.info("adding '%s'", path) |
| 1049 | |
| 1050 | if root_dir is not None: |
| 1051 | zip_filename = os.path.abspath(zip_filename) |
| 1052 | return zip_filename |
| 1053 | |
| 1054 | # Maps the name of the archive format to a tuple containing: |