Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-74696: Do not change the current working directory in shutil.make_archive() if possible#93160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits Select commit Hold shift + click to select a range
d9f680d gh-74696: Do not change the current working directory in shutil.make_…
serhiy-storchaka 21cd33d Add comments for _ARCHIVE_FORMATS and _UNPACK_FORMATS.
serhiy-storchaka fb50a41 Update Lib/shutil.py
serhiy-storchaka 2d497e3 Minor doc tweaks
ambv 1ccb177 Merge branch 'main' into shutil-make_archive-chdir
serhiy-storchaka 927d2d4 Add tests.
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -897,7 +897,7 @@ def _get_uid(name): | ||
| return None | ||
| def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, | ||
| owner=None, group=None, logger=None): | ||
| owner=None, group=None, logger=None, root_dir=None): | ||
| """Create a (possibly compressed) tar file from all the files under | ||
| 'base_dir'. | ||
| @@ -954,14 +954,20 @@ def _set_uid_gid(tarinfo): | ||
| if not dry_run: | ||
| tar = tarfile.open(archive_name, 'w|%s' % tar_compression) | ||
| arcname = base_dir | ||
| if root_dir is not None: | ||
| base_dir = os.path.join(root_dir, base_dir) | ||
| try: | ||
| tar.add(base_dir, filter=_set_uid_gid) | ||
| tar.add(base_dir, arcname, filter=_set_uid_gid) | ||
| finally: | ||
| tar.close() | ||
| if root_dir is not None: | ||
| archive_name = os.path.abspath(archive_name) | ||
| return archive_name | ||
| def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None): | ||
| def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, | ||
| logger=None, owner=None, group=None, root_dir=None): | ||
| """Create a zip file from all the files under 'base_dir'. | ||
| The output zip file will be named 'base_name' + ".zip". Returns the | ||
| @@ -985,42 +991,60 @@ def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None): | ||
| if not dry_run: | ||
| with zipfile.ZipFile(zip_filename, "w", | ||
| compression=zipfile.ZIP_DEFLATED) as zf: | ||
| path = os.path.normpath(base_dir) | ||
| if path != os.curdir: | ||
| zf.write(path, path) | ||
| arcname = os.path.normpath(base_dir) | ||
| if root_dir is not None: | ||
| base_dir = os.path.join(root_dir, base_dir) | ||
| base_dir = os.path.normpath(base_dir) | ||
| if arcname != os.curdir: | ||
| zf.write(base_dir, arcname) | ||
| if logger is not None: | ||
| logger.info("adding '%s'", path) | ||
| logger.info("adding '%s'", base_dir) | ||
| for dirpath, dirnames, filenames in os.walk(base_dir): | ||
| arcdirpath = dirpath | ||
| if root_dir is not None: | ||
| arcdirpath = os.path.relpath(arcdirpath, root_dir) | ||
| arcdirpath = os.path.normpath(arcdirpath) | ||
| for name in sorted(dirnames): | ||
| path = os.path.normpath(os.path.join(dirpath, name)) | ||
| zf.write(path, path) | ||
| path = os.path.join(dirpath, name) | ||
| arcname = os.path.join(arcdirpath, name) | ||
| zf.write(path, arcname) | ||
| if logger is not None: | ||
| logger.info("adding '%s'", path) | ||
| for name in filenames: | ||
| path = os.path.normpath(os.path.join(dirpath, name)) | ||
| path = os.path.join(dirpath, name) | ||
| path = os.path.normpath(path) | ||
| if os.path.isfile(path): | ||
| zf.write(path, path) | ||
| arcname = os.path.join(arcdirpath, name) | ||
| zf.write(path, arcname) | ||
| if logger is not None: | ||
| logger.info("adding '%s'", path) | ||
| if root_dir is not None: | ||
| zip_filename = os.path.abspath(zip_filename) | ||
| return zip_filename | ||
| # Maps the name of the archive format to a tuple containing: | ||
| # * the archiving function | ||
| # * extra keyword arguments | ||
| # * description | ||
| # * does it support the root_dir argument? | ||
| _ARCHIVE_FORMATS ={ | ||
merwok marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"), | ||
| 'tar': (_make_tarball, [('compress', None)], | ||
| "uncompressed tar file", True), | ||
| } | ||
| if _ZLIB_SUPPORTED: | ||
| _ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gzip')], | ||
| "gzip'ed tar-file") | ||
| _ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file") | ||
| "gzip'ed tar-file", True) | ||
| _ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file", True) | ||
| if _BZ2_SUPPORTED: | ||
| _ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')], | ||
| "bzip2'ed tar-file") | ||
| "bzip2'ed tar-file", True) | ||
| if _LZMA_SUPPORTED: | ||
| _ARCHIVE_FORMATS['xztar'] = (_make_tarball, [('compress', 'xz')], | ||
| "xz'ed tar-file") | ||
| "xz'ed tar-file", True) | ||
| def get_archive_formats(): | ||
| """Returns a list of supported formats for archiving and unarchiving. | ||
| @@ -1051,7 +1075,7 @@ def register_archive_format(name, function, extra_args=None, description=''): | ||
| if not isinstance(element, (tuple, list)) or len(element) !=2: | ||
| raise TypeError('extra_args elements are : (arg_name, value)') | ||
| _ARCHIVE_FORMATS[name] = (function, extra_args, description) | ||
| _ARCHIVE_FORMATS[name] = (function, extra_args, description, False) | ||
| def unregister_archive_format(name): | ||
| del _ARCHIVE_FORMATS[name] | ||
| @@ -1075,36 +1099,38 @@ def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, | ||
| uses the current owner and group. | ||
| """ | ||
| sys.audit("shutil.make_archive", base_name, format, root_dir, base_dir) | ||
| save_cwd = os.getcwd() | ||
| if root_dir is not None: | ||
| if logger is not None: | ||
| logger.debug("changing into '%s'", root_dir) | ||
| base_name = os.path.abspath(base_name) | ||
| if not dry_run: | ||
| os.chdir(root_dir) | ||
| if base_dir is None: | ||
| base_dir = os.curdir | ||
| kwargs ={'dry_run': dry_run, 'logger': logger} | ||
| try: | ||
| format_info = _ARCHIVE_FORMATS[format] | ||
| except KeyError: | ||
| raise ValueError("unknown archive format '%s'" % format) from None | ||
| kwargs ={'dry_run': dry_run, 'logger': logger, | ||
| 'owner': owner, 'group': group} | ||
| func = format_info[0] | ||
| for arg, val in format_info[1]: | ||
| kwargs[arg] = val | ||
| if format != 'zip': | ||
| kwargs['owner'] = owner | ||
| kwargs['group'] = group | ||
| if base_dir is None: | ||
| base_dir = os.curdir | ||
| support_root_dir = format_info[3] | ||
| save_cwd = None | ||
| if root_dir is not None: | ||
| if support_root_dir: | ||
| kwargs['root_dir'] = root_dir | ||
| else: | ||
| save_cwd = os.getcwd() | ||
| if logger is not None: | ||
| logger.debug("changing into '%s'", root_dir) | ||
| base_name = os.path.abspath(base_name) | ||
| if not dry_run: | ||
| os.chdir(root_dir) | ||
| try: | ||
| filename = func(base_name, base_dir, **kwargs) | ||
| finally: | ||
| if root_dir is not None: | ||
| if save_cwd is not None: | ||
| if logger is not None: | ||
| logger.debug("changing back to '%s'", save_cwd) | ||
| os.chdir(save_cwd) | ||
| @@ -1217,6 +1243,11 @@ def _unpack_tarfile(filename, extract_dir): | ||
| finally: | ||
| tarobj.close() | ||
| # Maps the name of the unpack format to a tuple containing: | ||
| # * extensions | ||
| # * the unpacking function | ||
| # * extra keyword arguments | ||
| # * description | ||
| _UNPACK_FORMATS ={ | ||
| 'tar': (['.tar'], _unpack_tarfile, [], "uncompressed tar file"), | ||
| 'zip': (['.zip'], _unpack_zipfile, [], "ZIP file"), | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2022-05-24-11-19-04.gh-issue-74696.-cnf-A.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| :func:`shutil.make_archive` no longer temporarily changes the current | ||
| working directory during creation of standard ``.zip`` or tar archives. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.