Create an archive file (eg. zip or tar). 'base_name' is the name of the file to create, minus any format-specific extension; 'format' is the archive format: one of "zip", "tar", "gztar", "bztar", or "xztar". Or any other registered format. 'root_dir' is a directory that will
(base_name, format, root_dir=None, base_dir=None, verbose=0,
dry_run=0, owner=None, group=None, logger=None)
| 1109 | del _ARCHIVE_FORMATS[name] |
| 1110 | |
| 1111 | def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, |
| 1112 | dry_run=0, owner=None, group=None, logger=None): |
| 1113 | """Create an archive file (eg. zip or tar). |
| 1114 | |
| 1115 | 'base_name' is the name of the file to create, minus any format-specific |
| 1116 | extension; 'format' is the archive format: one of "zip", "tar", "gztar", |
| 1117 | "bztar", or "xztar". Or any other registered format. |
| 1118 | |
| 1119 | 'root_dir' is a directory that will be the root directory of the |
| 1120 | archive; ie. we typically chdir into 'root_dir' before creating the |
| 1121 | archive. 'base_dir' is the directory where we start archiving from; |
| 1122 | ie. 'base_dir' will be the common prefix of all files and |
| 1123 | directories in the archive. 'root_dir' and 'base_dir' both default |
| 1124 | to the current directory. Returns the name of the archive file. |
| 1125 | |
| 1126 | 'owner' and 'group' are used when creating a tar archive. By default, |
| 1127 | uses the current owner and group. |
| 1128 | """ |
| 1129 | sys.audit("shutil.make_archive", base_name, format, root_dir, base_dir) |
| 1130 | try: |
| 1131 | format_info = _ARCHIVE_FORMATS[format] |
| 1132 | except KeyError: |
| 1133 | raise ValueError("unknown archive format '%s'" % format) from None |
| 1134 | |
| 1135 | kwargs = {'dry_run': dry_run, 'logger': logger, |
| 1136 | 'owner': owner, 'group': group} |
| 1137 | |
| 1138 | func = format_info[0] |
| 1139 | for arg, val in format_info[1]: |
| 1140 | kwargs[arg] = val |
| 1141 | |
| 1142 | if base_dir is None: |
| 1143 | base_dir = os.curdir |
| 1144 | |
| 1145 | supports_root_dir = format_info[3] |
| 1146 | save_cwd = None |
| 1147 | if root_dir is not None: |
| 1148 | stmd = os.stat(root_dir).st_mode |
| 1149 | if not stat.S_ISDIR(stmd): |
| 1150 | raise NotADirectoryError(errno.ENOTDIR, 'Not a directory', root_dir) |
| 1151 | |
| 1152 | if supports_root_dir: |
| 1153 | # Support path-like base_name here for backwards-compatibility. |
| 1154 | base_name = os.fspath(base_name) |
| 1155 | kwargs['root_dir'] = root_dir |
| 1156 | else: |
| 1157 | save_cwd = os.getcwd() |
| 1158 | if logger is not None: |
| 1159 | logger.debug("changing into '%s'", root_dir) |
| 1160 | base_name = os.path.abspath(base_name) |
| 1161 | if not dry_run: |
| 1162 | os.chdir(root_dir) |
| 1163 | |
| 1164 | try: |
| 1165 | filename = func(base_name, base_dir, **kwargs) |
| 1166 | finally: |
| 1167 | if save_cwd is not None: |
| 1168 | if logger is not None: |