Skip to content

Commit

Permalink
Merge pull request #197 from HexDecimal/fix-71
Browse files Browse the repository at this point in the history
Expand globs in simple script arguments
  • Loading branch information
HexDecimal authored Dec 12, 2023
2 parents a17dc82 + 62f0a50 commit 8648115
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
],
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.fixAll": "always"
},
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
Expand Down
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ rules on making a good Changelog.

## [Unreleased]

### Changed

- Handle glob paths in more cases for `delocate-wheel`, `delocate-path`,
`delocate-listdeps`, and `delocate-addplat`.
[#71](https://github.com/matthew-brett/delocate/issues/71)

### Fixed

- No longer picks a random directory when multiple directories end with the
Expand Down
19 changes: 18 additions & 1 deletion delocate/cmd/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"""
from __future__ import annotations

import glob
import logging
import os
import sys
from argparse import ArgumentParser, Namespace
from typing import Callable, List
from pathlib import Path
from typing import Callable, Iterable, Iterator, List

from typing_extensions import Literal, TypedDict

Expand Down Expand Up @@ -114,3 +116,18 @@ def copy_filter_exclude(name: str) -> bool:
"ignore_missing": args.ignore_missing_dependencies,
"sanitize_rpaths": args.sanitize_rpaths,
}


def glob_paths(paths: Iterable[str]) -> Iterator[str]:
"""Iterate over the expanded paths of potential glob paths.
Does not try to glob paths which match existing files.
"""
for path in paths:
if Path(path).exists():
yield path # Don't try to expand paths when their target exists
continue
expanded_paths = glob.glob(path)
if not expanded_paths:
raise FileNotFoundError(path)
yield from expanded_paths
7 changes: 4 additions & 3 deletions delocate/cmd/delocate_addplat.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from os.path import expanduser, realpath
from os.path import join as exists

from delocate.cmd.common import common_parser, verbosity_config
from delocate.cmd.common import common_parser, glob_paths, verbosity_config
from delocate.wheeltools import WheelToolsError, add_platforms

parser = ArgumentParser(description=__doc__, parents=[common_parser])
Expand Down Expand Up @@ -94,7 +94,8 @@
def main() -> None:
args = parser.parse_args()
verbosity_config(args)
multi = len(args.wheels) > 1
wheels = list(glob_paths(args.wheels))
multi = len(wheels) > 1
if args.wheel_dir:
wheel_dir = expanduser(args.wheel_dir)
if not exists(wheel_dir):
Expand All @@ -110,7 +111,7 @@ def main() -> None:
]
if len(plat_tags) == 0:
raise RuntimeError("Need at least one --osx-ver or --plat-tag")
for wheel in args.wheels:
for wheel in wheels:
if multi or args.verbose:
print(
"Setting platform tags {0} for wheel {1}".format(
Expand Down
7 changes: 4 additions & 3 deletions delocate/cmd/delocate_listdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from os.path import sep as psep

from delocate import wheel_libs
from delocate.cmd.common import common_parser, verbosity_config
from delocate.cmd.common import common_parser, glob_paths, verbosity_config
from delocate.delocating import filter_system_libs
from delocate.libsana import stripped_lib_dict, tree_libs_from_directory

Expand Down Expand Up @@ -40,8 +40,9 @@
def main() -> None:
args = parser.parse_args()
verbosity_config(args)
multi = len(args.paths) > 1
for path in args.paths:
paths = list(glob_paths(args.paths))
multi = len(paths) > 1
for path in paths:
if multi:
print(path + ":")
indent = " "
Expand Down
6 changes: 4 additions & 2 deletions delocate/cmd/delocate_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
common_parser,
delocate_parser,
delocate_values,
glob_paths,
verbosity_config,
)

Expand All @@ -38,8 +39,9 @@
def main() -> None:
args = parser.parse_args()
verbosity_config(args)
multi = len(args.paths) > 1
for path in args.paths:
paths = list(glob_paths(args.paths))
multi = len(paths) > 1
for path in paths:
if multi:
print(path)
# evaluate paths relative to the path we are working on
Expand Down
8 changes: 5 additions & 3 deletions delocate/cmd/delocate_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Overwrites the wheel in-place by default
"""
# vim: ft=python
from __future__ import absolute_import, division, print_function
from __future__ import annotations

import os
from argparse import ArgumentParser
Expand All @@ -17,6 +17,7 @@
common_parser,
delocate_parser,
delocate_values,
glob_paths,
verbosity_config,
)

Expand Down Expand Up @@ -65,7 +66,8 @@
def main() -> None:
args = parser.parse_args()
verbosity_config(args)
multi = len(args.wheels) > 1
wheels = list(glob_paths(args.wheels))
multi = len(wheels) > 1
if args.wheel_dir:
wheel_dir = expanduser(args.wheel_dir)
if not exists(wheel_dir):
Expand All @@ -80,7 +82,7 @@ def main() -> None:
else:
require_archs = args.require_archs

for wheel in args.wheels:
for wheel in wheels:
if multi or args.verbose:
print("Fixing: " + wheel)
if wheel_dir:
Expand Down
34 changes: 34 additions & 0 deletions delocate/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,37 @@ def test_sanitize_command(tmp_path: Path, script_runner: ScriptRunner) -> None:
assert "libs/" not in set(
get_rpaths(str(unpack_dir / "fakepkg/subpkg/module2.abi3.so"))
)


@pytest.mark.xfail( # type: ignore[misc]
sys.platform != "darwin", reason="Needs macOS linkage."
)
def test_glob(
tmp_path: Path, plat_wheel: PlatWheel, script_runner: ScriptRunner
) -> None:
# Test implicit globbing by passing "*.whl" without shell=True
script_runner.run(["delocate-listdeps", "*.whl"], check=True, cwd=tmp_path)
zip2dir(plat_wheel.whl, tmp_path / "plat")

result = script_runner.run(
["delocate-wheel", "*.whl", "-v"], check=True, cwd=tmp_path
)
assert Path(plat_wheel.whl).name in result.stdout
assert "*.whl" not in result.stdout
assert not Path(tmp_path, "*.whl").exists()

# Delocate literal file "*.whl" instead of expanding glob
shutil.copyfile(plat_wheel.whl, tmp_path / "*.whl")
result = script_runner.run(
["delocate-wheel", "*.whl", "-v"], check=True, cwd=tmp_path
)
assert Path(plat_wheel.whl).name not in result.stdout
assert "*.whl" in result.stdout

Path(plat_wheel.whl).unlink()
Path(tmp_path, "*.whl").unlink()
result = script_runner.run(["delocate-wheel", "*.whl"], cwd=tmp_path)
assert result.returncode == 1
assert "FileNotFoundError:" in result.stderr

script_runner.run(["delocate-path", "*/"], check=True, cwd=tmp_path)

0 comments on commit 8648115

Please sign in to comment.