Unpack an archive. `filename` is the name of the archive. `extract_dir` is the name of the target directory, where the archive is unpacked. If not provided, the current working directory is used. `format` is the archive format: one of "zip", "tar", "gztar", "bztar", or
(filename, extract_dir=None, format=None, *, filter=None)
| 1307 | return None |
| 1308 | |
| 1309 | def unpack_archive(filename, extract_dir=None, format=None, *, filter=None): |
| 1310 | """Unpack an archive. |
| 1311 | |
| 1312 | `filename` is the name of the archive. |
| 1313 | |
| 1314 | `extract_dir` is the name of the target directory, where the archive |
| 1315 | is unpacked. If not provided, the current working directory is used. |
| 1316 | |
| 1317 | `format` is the archive format: one of "zip", "tar", "gztar", "bztar", |
| 1318 | or "xztar". Or any other registered format. If not provided, |
| 1319 | unpack_archive will use the filename extension and see if an unpacker |
| 1320 | was registered for that extension. |
| 1321 | |
| 1322 | In case none is found, a ValueError is raised. |
| 1323 | |
| 1324 | If `filter` is given, it is passed to the underlying |
| 1325 | extraction function. |
| 1326 | """ |
| 1327 | sys.audit("shutil.unpack_archive", filename, extract_dir, format) |
| 1328 | |
| 1329 | if extract_dir is None: |
| 1330 | extract_dir = os.getcwd() |
| 1331 | |
| 1332 | extract_dir = os.fspath(extract_dir) |
| 1333 | filename = os.fspath(filename) |
| 1334 | |
| 1335 | if filter is None: |
| 1336 | filter_kwargs = {} |
| 1337 | else: |
| 1338 | filter_kwargs = {'filter': filter} |
| 1339 | if format is not None: |
| 1340 | try: |
| 1341 | format_info = _UNPACK_FORMATS[format] |
| 1342 | except KeyError: |
| 1343 | raise ValueError("Unknown unpack format '{0}'".format(format)) from None |
| 1344 | |
| 1345 | func = format_info[1] |
| 1346 | func(filename, extract_dir, **dict(format_info[2]), **filter_kwargs) |
| 1347 | else: |
| 1348 | # we need to look at the registered unpackers supported extensions |
| 1349 | format = _find_unpack_format(filename) |
| 1350 | if format is None: |
| 1351 | raise ReadError("Unknown archive format '{0}'".format(filename)) |
| 1352 | |
| 1353 | func = _UNPACK_FORMATS[format][1] |
| 1354 | kwargs = dict(_UNPACK_FORMATS[format][2]) | filter_kwargs |
| 1355 | func(filename, extract_dir, **kwargs) |
| 1356 | |
| 1357 | |
| 1358 | if hasattr(os, 'statvfs'): |
nothing calls this directly
no test coverage detected