Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --exclude option to delocate-wheel #106

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Releases

* [Unreleased]

* Dependencies can be manually excluded with the ``--exclude <name>`` flag.
[#106](https://github.com/matthew-brett/delocate/pull/106)

* [0.10.5] - 2023-11-14

* Fixed `UnicodeDecodeError` when an archive has non-ASCII characters.
Expand Down
118 changes: 118 additions & 0 deletions delocate/cmd/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
""" Code shared among multiple commands.

All functions in this module are private.
"""
from __future__ import annotations

import logging
import os
import sys
from optparse import Option, OptionParser, Values
from typing import Callable, List

from delocate.delocating import filter_system_libs
from typing_extensions import Literal, TypedDict

logger = logging.getLogger(__name__)


def verbosity_args(parser: OptionParser) -> None:
"""Logging arguments shared by all commands."""
parser.add_options(
[
Option(
"-v",
"--verbose",
action="count",
help=(
"Show a more verbose report of progress and failure."
" Additional flags show even more info, up to -vv."
),
default=0,
),
]
)


def verbosity_config(opts: Values) -> None:
"""Configure logging from parsed verbosity arguments."""
logging.basicConfig(
level={0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}.get(
opts.verbose, logging.DEBUG
)
)


def delocate_args(parser: OptionParser):
"""Arguments shared by delocate-path and delocate-wheel commands."""
parser.add_options(
[
Option(
"-d",
"--dylibs-only",
action="store_true",
help="Only analyze files with known dynamic library extensions",
),
Option(
"-e",
"--exclude",
action="append",
default=[],
type="string",
help=(
"Exclude any libraries where path includes the given string"
),
),
Option(
"--executable-path",
action="store",
type="string",
default=os.path.dirname(sys.executable),
help=(
"The path used to resolve @executable_path in dependencies"
),
),
Option(
"--ignore-missing-dependencies",
action="store_true",
help=(
"Skip dependencies which couldn't be found and delocate "
"as much as possible"
),
),
]
)


class DelocateArgs(TypedDict):
"""Common kwargs for delocate_path and delocate_wheel."""

copy_filt_func: Callable[[str], bool]
executable_path: str
lib_filt_func: Callable[[str], bool] | Literal["dylibs-only"] | None
ignore_missing: bool


def delocate_values(opts: Values) -> DelocateArgs:
"""Return the common kwargs for delocate_path and delocate_wheel."""

exclude_files: List[str] = opts.exclude

def copy_filter_exclude(name: str) -> bool:
"""Returns False if name is excluded, uses normal rules otherwise."""
for exclude_str in exclude_files:
if exclude_str in name:
logger.info(
"%s excluded because of exclude %r rule.",
name,
exclude_str,
)
return False
return filter_system_libs(name)

return {
"copy_filt_func": copy_filter_exclude,
"executable_path": opts.executable_path,
"lib_filt_func": "dylibs-only" if opts.dylibs_only else None,
"ignore_missing": opts.ignore_missing_dependencies,
}
13 changes: 4 additions & 9 deletions delocate/cmd/delocate_addplat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
from os.path import realpath

from delocate import __version__
from delocate.cmd.common import verbosity_args, verbosity_config
from delocate.wheeltools import WheelToolsError, add_platforms


def main():
def main() -> None:
parser = OptionParser(
usage="%s WHEEL_FILENAME\n\n" % sys.argv[0] + __doc__,
version="%prog " + __version__,
)
verbosity_args(parser)
parser.add_option(
Option(
"-p",
Expand Down Expand Up @@ -108,15 +110,8 @@ def main():
),
)
)
parser.add_option(
Option(
"-v",
"--verbose",
action="store_true",
help="Show more verbose report of progress and failure",
)
)
(opts, wheels) = parser.parse_args()
verbosity_config(opts)
if len(wheels) < 1:
parser.print_help()
sys.exit(1)
Expand Down
13 changes: 4 additions & 9 deletions delocate/cmd/delocate_fuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
from os.path import join as pjoin

from delocate import __version__
from delocate.cmd.common import verbosity_args, verbosity_config
from delocate.fuse import fuse_wheels


def main():
def main() -> None:
parser = OptionParser(
usage="%s WHEEL1 WHEEL2\n\n" % sys.argv[0] + __doc__,
version="%prog " + __version__,
)
verbosity_args(parser)
parser.add_option(
Option(
"-w",
Expand All @@ -32,15 +34,8 @@ def main():
),
)
)
parser.add_option(
Option(
"-v",
"--verbose",
action="store_true",
help="Show libraries copied during fix",
)
)
(opts, wheels) = parser.parse_args()
verbosity_config(opts)
if len(wheels) != 2:
parser.print_help()
sys.exit(1)
Expand Down
5 changes: 4 additions & 1 deletion delocate/cmd/delocate_listdeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
from os.path import sep as psep

from delocate import __version__, wheel_libs
from delocate.cmd.common import verbosity_args, verbosity_config
from delocate.delocating import filter_system_libs
from delocate.libsana import stripped_lib_dict, tree_libs_from_directory


def main():
def main() -> None:
parser = OptionParser(
usage="%s WHEEL_OR_PATH_TO_ANALYZE\n\n" % sys.argv[0] + __doc__,
version="%prog " + __version__,
)
verbosity_args(parser)
parser.add_options(
[
Option(
Expand All @@ -37,6 +39,7 @@ def main():
]
)
(opts, paths) = parser.parse_args()
verbosity_config(opts)
if len(paths) < 1:
parser.print_help()
sys.exit(1)
Expand Down
13 changes: 4 additions & 9 deletions delocate/cmd/delocate_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
from os.path import join as pjoin

from delocate import __version__, patch_wheel
from delocate.cmd.common import verbosity_args, verbosity_config


def main():
def main() -> None:
parser = OptionParser(
usage="%s WHEEL_FILENAME PATCH_FNAME\n\n" % sys.argv[0] + __doc__,
version="%prog " + __version__,
)
verbosity_args(parser)
parser.add_option(
Option(
"-w",
Expand All @@ -32,15 +34,8 @@ def main():
),
)
)
parser.add_option(
Option(
"-v",
"--verbose",
action="store_true",
help="Print input and output wheels",
)
)
(opts, args) = parser.parse_args()
verbosity_config(opts)
if len(args) != 2:
parser.print_help()
sys.exit(1)
Expand Down
39 changes: 11 additions & 28 deletions delocate/cmd/delocate_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
from optparse import Option, OptionParser

from delocate import __version__, delocate_path
from delocate.cmd.common import (
delocate_args,
delocate_values,
verbosity_args,
verbosity_config,
)


def main():
def main() -> None:
parser = OptionParser(
usage="%s PATH_TO_ANALYZE\n\n" % sys.argv[0] + __doc__,
version="%prog " + __version__,
)
verbosity_args(parser)
delocate_args(parser)
parser.add_options(
[
Option(
Expand All @@ -25,39 +33,16 @@ def main():
type="string",
help="Output subdirectory path to copy library dependencies",
),
Option(
"-d",
"--dylibs-only",
action="store_true",
help="Only analyze files with known dynamic library extensions",
),
Option(
"--executable-path",
action="store",
type="string",
default=os.path.dirname(sys.executable),
help=(
"The path used to resolve @executable_path in dependencies"
),
),
Option(
"--ignore-missing-dependencies",
action="store_true",
help=(
"Skip dependencies which couldn't be found and delocate "
"as much as possible"
),
),
]
)
(opts, paths) = parser.parse_args()
verbosity_config(opts)
if len(paths) < 1:
parser.print_help()
sys.exit(1)

if opts.lib_path is None:
opts.lib_path = ".dylibs"
lib_filt_func = "dylibs-only" if opts.dylibs_only else None
multi = len(paths) > 1
for path in paths:
if multi:
Expand All @@ -67,9 +52,7 @@ def main():
delocate_path(
path,
lib_path,
lib_filt_func,
executable_path=opts.executable_path,
ignore_missing=opts.ignore_missing_dependencies,
**delocate_values(opts),
)


Expand Down
Loading