Skip to content

Commit

Permalink
fix: Fix extracted files permissions
Browse files Browse the repository at this point in the history
This commit fixes the extracted file permissions
when using .extract/.extract_files in addition
to when using the unpack cli command.

The fix is by overriding the _extract_member
method of zip file, and adding permissions fix
there.

The permissions fix was moved from the unpack to
the new method.
  • Loading branch information
gilfree committed Apr 3, 2024
1 parent 0a4f40e commit fb55460
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/wheel/cli/unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ def unpack(path: str, dest: str = ".") -> None:
namever = wf.parsed_filename.group("namever")
destination = Path(dest) / namever
print(f"Unpacking to: {destination}...", end="", flush=True)
for zinfo in wf.filelist:
wf.extract(zinfo, destination)

# Set permissions to the same values as they were set in the archive
# We have to do this manually due to
# https://github.com/python/cpython/issues/59999
permissions = zinfo.external_attr >> 16 & 0o777
destination.joinpath(zinfo.filename).chmod(permissions)
wf.extractall(destination)

print("OK")
13 changes: 13 additions & 0 deletions src/wheel/wheelfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import stat
import time
from io import StringIO, TextIOWrapper
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo

from wheel.cli import WheelError
Expand Down Expand Up @@ -194,3 +195,15 @@ def close(self):
self.writestr(self.record_path, data.getvalue())

ZipFile.close(self)

def _extract_member(self, member, targetpath, pwd):
targetpath = super()._extract_member(member, targetpath, pwd)
if not isinstance(member, ZipInfo):
member = self.getinfo(member)

# Set permissions to the same values as they were set in the archive
# We have to do this manually due to
# https://github.com/python/cpython/issues/59999
permissions = member.external_attr >> 16 & 0o777
Path(targetpath).chmod(permissions)
return targetpath

0 comments on commit fb55460

Please sign in to comment.