Skip to content

Commit

Permalink
Modify wheel#unzip to preserve file execute permissions. (bazelbuild#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaCrestone authored Jun 10, 2020
1 parent 536653b commit 229b03c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
4 changes: 1 addition & 3 deletions extract_wheels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ def main() -> None:

pip_args = [sys.executable, "-m", "pip", "wheel", "-r", args.requirements]
# Assumes any errors are logged by pip so do nothing. This command will fail if pip fails
subprocess.check_output(
pip_args
)
subprocess.check_output(pip_args)

extras = requirements.parse_extras(args.requirements)

Expand Down
29 changes: 29 additions & 0 deletions extract_wheels/lib/wheel.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
"""Utility class to inspect an extracted wheel directory"""
import glob
import os
import stat
import zipfile
from typing import Dict, Optional, Set

import pkg_resources
import pkginfo


def current_umask() -> int:
"""Get the current umask which involves having to set it temporarily."""
mask = os.umask(0)
os.umask(mask)
return mask


def set_extracted_file_to_default_mode_plus_executable(path: str) -> None:
"""
Make file present at path have execute for user/group/world
(chmod +x) is no-op on windows per python docs
"""
os.chmod(path, (0o777 & ~current_umask() | 0o111))


class Wheel:
"""Representation of the compressed .whl file"""

Expand Down Expand Up @@ -43,6 +59,19 @@ def dependencies(self, extras_requested: Optional[Set[str]] = None) -> Set[str]:
def unzip(self, directory: str) -> None:
with zipfile.ZipFile(self.path, "r") as whl:
whl.extractall(directory)
# The following logic is borrowed from Pip:
# https://github.com/pypa/pip/blob/cc48c07b64f338ac5e347d90f6cb4efc22ed0d0b/src/pip/_internal/utils/unpacking.py#L240
for info in whl.infolist():
name = info.filename
# Do not attempt to modify directories.
if name.endswith("/") or name.endswith("\\"):
continue
mode = info.external_attr >> 16
# if mode and regular file and any execute permissions for
# user/group/world?
if mode and stat.S_ISREG(mode) and mode & 0o111:
name = os.path.join(directory, name)
set_extracted_file_to_default_mode_plus_executable(name)


def get_dist_info(wheel_dir: str) -> str:
Expand Down

0 comments on commit 229b03c

Please sign in to comment.