Skip to content

Commit

Permalink
Transfer execute bits (fixes #135)
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-cbarber committed Apr 13, 2024
1 parent 79c5a51 commit b5a53e4
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# whl2conda changes

## [24.2.0] - *in progress*
## [24.4.0] - *in progress*
### Bug fixes
* Transfer executable file permissions from wheel (#135)
* Correct typos in documentation.

## [24.1.2] - 2024-1-28
Expand Down
8 changes: 8 additions & 0 deletions doc/guide/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ in dependencies and probably will not occur that often in practice.
We handle this by simplying changinge `===` to `==` but
since this will often not work we also issue a warning.

## File permissions are not copied when run on Windows

Executable file permissions are copied from the wheel when conversion
is run on MacOS or Linux but not Windows. When converting packages that
contain scripts with execute permissions (intended for use on Linux/MacOS),
make sure to avoid Windows when doing the conversion
(see issue [issue 135](https://github.com/zuzukin/whl2conda/issues/135))

## Cannot convert from sdist

Conversion from python sdist distributions is not currently supported.
Expand Down
2 changes: 1 addition & 1 deletion src/whl2conda/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24.2.0
24.4.0
10 changes: 2 additions & 8 deletions src/whl2conda/api/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
from typing import Any, NamedTuple, Optional, Sequence

# third party
from wheel.wheelfile import WheelFile
from conda_package_handling.api import create as create_conda_pkg

# this project
from ..__about__ import __version__
from ..impl.prompt import bool_input
from ..impl.pyproject import CondaPackageFormat
from ..impl.wheel import unpack_wheel
from .stdrename import load_std_renames

__all__ = [
Expand Down Expand Up @@ -296,7 +296,6 @@ class Wheel2CondaConverter:
wheel_path: Path
out_dir: Path
dry_run: bool = False
wheel: Optional[WheelFile]
out_format: CondaPackageFormat = CondaPackageFormat.V2
overwrite: bool = False
keep_pip_dependencies: bool = False
Expand Down Expand Up @@ -885,13 +884,8 @@ def translate_version_spec(self, pip_version: str) -> str:

def _extract_wheel(self, temp_dir: Path) -> Path:
self.logger.info("Reading %s", self.wheel_path)
wheel = WheelFile(self.wheel_path)
wheel_dir = temp_dir / "wheel-files"
wheel.extractall(wheel_dir)
if self.logger.getEffectiveLevel() <= logging.DEBUG:
for wheel_file in wheel_dir.glob("**/*"):
if wheel_file.is_file():
self._debug("Extracted %s", wheel_file.relative_to(wheel_dir))
unpack_wheel(self.wheel_path, wheel_dir, logger=self.logger)
return wheel_dir

def _warn(self, msg, *args):
Expand Down
2 changes: 1 addition & 1 deletion src/whl2conda/cli/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def run(self) -> None:
finally:
self._cleanup()
end = time.time()
print(f"Elapsed time: {end-start:f} seconds")
print(f"Elapsed time: {end - start:f} seconds")

def _render_recipe(self):
conda_bld = get_conda_bld_path()
Expand Down
51 changes: 51 additions & 0 deletions src/whl2conda/impl/wheel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2024 Christopher Barber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Utilities for working with wheel files
"""

import logging
from pathlib import Path
from typing import Optional, Union
from wheel.wheelfile import WheelFile

__all__ = ["unpack_wheel"]

def unpack_wheel(
wheel: Union[Path, str],
dest_dir: Union[Path|str],
*,
logger: Optional[logging.Logger] = None
) -> None:
"""
Unpack wheel into specified directory.
Args:
wheel: location of wheel file to unpack
dest_dir: destination directory.
"""
wheel_path = Path(wheel)
dest_path = Path(dest_dir)
dest_path.mkdir(parents=True, exist_ok=True)

logger = logger or logging.getLogger(__name__)

with WheelFile(wheel_path) as wf:
for zipinfo in wf.filelist:
extracted = Path(wf.extract(zipinfo, dest_path))
# copy file permissions (see https://github.com/python/cpython/issues/59999)
# has no effect on Windows
extracted.chmod(zipinfo.external_attr >> 16 & 0o777)
logger.debug("Extracted %s", extracted.relative_to(dest_path))
15 changes: 15 additions & 0 deletions test-projects/setup/scripts/myscript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 Christopher Barber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
print("hello world")
1 change: 1 addition & 0 deletions test-projects/setup/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@
],
extras_require={'bdev': ['black']},
packages=["mypkg"],
scripts=["scripts/myscript.py"],
)
5 changes: 2 additions & 3 deletions test/api/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

import conda_package_handling.api as cphapi
import pytest
from wheel.wheelfile import WheelFile

from whl2conda.__about__ import __version__
from whl2conda.api.converter import RequiresDistEntry, Wheel2CondaConverter
from whl2conda.impl.wheel import unpack_wheel


class PackageValidator:
Expand Down Expand Up @@ -145,8 +145,7 @@ def _unpack_wheel(self, wheel_path: Path) -> Path:
if wheel_path.is_dir():
shutil.copytree(wheel_path, unpack_dir, dirs_exist_ok=True)
else:
wheel = WheelFile(wheel_path)
wheel.extractall(unpack_dir)
unpack_wheel(wheel_path, unpack_dir)

return unpack_dir

Expand Down
2 changes: 1 addition & 1 deletion test/cli/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def teardown(self) -> None:
"""Make sure all test cases have been run."""
for i, case in enumerate(self.cases):
if not case.was_run:
pytest.fail(f"Case #{i+1} was not run")
pytest.fail(f"Case #{i + 1} was not run")


@pytest.fixture
Expand Down
44 changes: 44 additions & 0 deletions test/impl/test_wheel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2024 Christopher Barber
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Unit tests for the whl2conda.impl.wheel module
"""
import platform
import stat
from pathlib import Path

import pytest

from whl2conda.impl.wheel import unpack_wheel

from ..test_packages import setup_wheel

def test_unpack_wheel(
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
setup_wheel: Path,
) -> None:
"""
Unit test for unpack_wheel
"""

unpack_wheel(setup_wheel, tmp_path)

if not platform.system() == "Windows":
# Regression case for #135
script_paths = list(tmp_path.rglob("**/scripts/*.py"))
assert script_paths
for script_path in script_paths:
assert script_path.stat().st_mode & stat.S_IXUSR

0 comments on commit b5a53e4

Please sign in to comment.