diff options
author | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-03-14 17:13:37 +0000 |
---|---|---|
committer | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-03-14 17:13:37 +0000 |
commit | 4882320b58c36a46532559894a9bd943b00adf0f (patch) | |
tree | 145ad9a6446b9f13931e9f00253c710ca2b75ece | |
parent | 069735fa1e2cfc8161474f3b23bf19dacf6c51ca (diff) | |
download | python-setuptools-git-4882320b58c36a46532559894a9bd943b00adf0f.tar.gz |
Extract reusable _unpack_zipfile_obj from archive_utils
-rw-r--r-- | setuptools/archive_util.py | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/setuptools/archive_util.py b/setuptools/archive_util.py index 73b2db75..d8e10c13 100644 --- a/setuptools/archive_util.py +++ b/setuptools/archive_util.py @@ -100,29 +100,37 @@ def unpack_zipfile(filename, extract_dir, progress_filter=default_filter): raise UnrecognizedFormat("%s is not a zip file" % (filename,)) with zipfile.ZipFile(filename) as z: - for info in z.infolist(): - name = info.filename + _unpack_zipfile_obj(z, extract_dir, progress_filter) - # don't extract absolute paths or ones with .. in them - if name.startswith('/') or '..' in name.split('/'): - continue - target = os.path.join(extract_dir, *name.split('/')) - target = progress_filter(name, target) - if not target: - continue - if name.endswith('/'): - # directory - ensure_directory(target) - else: - # file - ensure_directory(target) - data = z.read(info.filename) - with open(target, 'wb') as f: - f.write(data) - unix_attributes = info.external_attr >> 16 - if unix_attributes: - os.chmod(target, unix_attributes) +def _unpack_zipfile_obj(zipfile_obj, extract_dir, progress_filter=default_filter): + """Internal/private API used by other parts of setuptools. + Similar to ``unpack_zipfile``, but receives an already opened :obj:`zipfile.ZipFile` + object instead of a filename. + """ + for info in zipfile_obj.infolist(): + name = info.filename + + # don't extract absolute paths or ones with .. in them + if name.startswith('/') or '..' in name.split('/'): + continue + + target = os.path.join(extract_dir, *name.split('/')) + target = progress_filter(name, target) + if not target: + continue + if name.endswith('/'): + # directory + ensure_directory(target) + else: + # file + ensure_directory(target) + data = zipfile_obj.read(info.filename) + with open(target, 'wb') as f: + f.write(data) + unix_attributes = info.external_attr >> 16 + if unix_attributes: + os.chmod(target, unix_attributes) def _resolve_tar_file_or_dir(tar_obj, tar_member_obj): |