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", "xztar", or "zstdtar". Or any other registered format. 'root_dir' is a directory that
(base_name, format, root_dir=None, base_dir=None, verbose=0,
dry_run=0, owner=None, group=None, logger=None)
| 1182 | del _ARCHIVE_FORMATS[name] |
| 1183 | |
| 1184 | def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, |
| 1185 | dry_run=0, owner=None, group=None, logger=None): |
| 1186 | """Create an archive file (eg. zip or tar). |
| 1187 | |
| 1188 | 'base_name' is the name of the file to create, minus any format-specific |
| 1189 | extension; 'format' is the archive format: one of "zip", "tar", "gztar", |
| 1190 | "bztar", "xztar", or "zstdtar". Or any other registered format. |
| 1191 | |
| 1192 | 'root_dir' is a directory that will be the root directory of the |
| 1193 | archive; ie. we typically chdir into 'root_dir' before creating the |
| 1194 | archive. 'base_dir' is the directory where we start archiving from; |
| 1195 | ie. 'base_dir' will be the common prefix of all files and |
| 1196 | directories in the archive. 'root_dir' and 'base_dir' both default |
| 1197 | to the current directory. Returns the name of the archive file. |
| 1198 | |
| 1199 | 'owner' and 'group' are used when creating a tar archive. By default, |
| 1200 | uses the current owner and group. |
| 1201 | """ |
| 1202 | sys.audit("shutil.make_archive", base_name, format, root_dir, base_dir) |
| 1203 | try: |
| 1204 | format_info = _ARCHIVE_FORMATS[format] |
| 1205 | except KeyError: |
| 1206 | raise ValueError("unknown archive format '%s'" % format) from None |
| 1207 | |
| 1208 | kwargs = {'dry_run': dry_run, 'logger': logger, |
| 1209 | 'owner': owner, 'group': group} |
| 1210 | |
| 1211 | func = format_info[0] |
| 1212 | for arg, val in format_info[1]: |
| 1213 | kwargs[arg] = val |
| 1214 | |
| 1215 | base_name = os.fspath(base_name) |
| 1216 | |
| 1217 | if base_dir is None: |
| 1218 | base_dir = os.curdir |
| 1219 | else: |
| 1220 | base_dir = os.fspath(base_dir) |
| 1221 | |
| 1222 | supports_root_dir = getattr(func, 'supports_root_dir', False) |
| 1223 | save_cwd = None |
| 1224 | if root_dir is not None: |
| 1225 | root_dir = os.fspath(root_dir) |
| 1226 | stmd = os.stat(root_dir).st_mode |
| 1227 | if not stat.S_ISDIR(stmd): |
| 1228 | raise NotADirectoryError(errno.ENOTDIR, 'Not a directory', root_dir) |
| 1229 | |
| 1230 | if supports_root_dir: |
| 1231 | kwargs['root_dir'] = root_dir |
| 1232 | else: |
| 1233 | save_cwd = os.getcwd() |
| 1234 | if logger is not None: |
| 1235 | logger.debug("changing into '%s'", root_dir) |
| 1236 | base_name = os.path.abspath(base_name) |
| 1237 | if not dry_run: |
| 1238 | os.chdir(root_dir) |
| 1239 | |
| 1240 | try: |
| 1241 | filename = func(base_name, base_dir, **kwargs) |
searching dependent graphs…