Skip to content

Commit c53a320

Browse files
cottsaydirk-thomas
authored andcommitted
Add --packages-above-depth selection mechanism (#22)
* Add `--packages-above-depth` selection mechanism This package selection argument selects the given packages and packages which depend on them, proceeding out to the given depth. Passing a depth of `0` yields the same behavior as `--packages-select`, while passign a sufficiently large value yeilds the same behavior as `--packages-above`. * nitpick: line wrapping * PR feedback
1 parent 4ee78b0 commit c53a320

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

colcon_package_selection/package_selection/dependencies.py

+45
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
# Copyright 2016-2018 Dirk Thomas
22
# Licensed under the Apache License, Version 2.0
33

4+
import argparse
45
import sys
56

67
from colcon_core.package_selection import logger
78
from colcon_core.package_selection import PackageSelectionExtensionPoint
89
from colcon_core.plugin_system import satisfies_version
910

1011

12+
class _DepthAndPackageNames(argparse.Action):
13+
"""Action to assign an integer depth optional package names."""
14+
15+
def __call__(self, parser, namespace, values, option_string=None):
16+
assert len(values) >= 1
17+
try:
18+
values[0] = int(values[0])
19+
if values[0] < 0:
20+
raise ValueError()
21+
except ValueError:
22+
raise argparse.ArgumentError(
23+
self, 'the first parameter must be a non-negative integer for '
24+
'the depth')
25+
setattr(namespace, self.dest, values)
26+
27+
1128
class DependenciesPackageSelection(PackageSelectionExtensionPoint):
1229
"""Select packages based on their dependencies."""
1330

@@ -25,6 +42,11 @@ def add_arguments(self, *, parser): # noqa: D102
2542
'--packages-above', nargs='*', metavar='PKG_NAME',
2643
help='Only process a subset of packages and packages which '
2744
'recursively depend on them')
45+
parser.add_argument(
46+
'--packages-above-depth', nargs='+',
47+
metavar=('DEPTH', 'PKG_NAME'), action=_DepthAndPackageNames,
48+
help='Only process a subset of packages and packages which '
49+
'recursively depend on them up to a given depth')
2850

2951
parser.add_argument(
3052
'--packages-select-by-dep', nargs='*', metavar='DEP_NAME',
@@ -50,6 +72,12 @@ def check_parameters(self, args, pkg_names): # noqa: D102
5072
"Package '{name}' specified with --packages-above "
5173
'was not found'
5274
.format_map(locals()))
75+
for name in (args.packages_above_depth or [])[1:]:
76+
if name not in pkg_names:
77+
sys.exit(
78+
"Package '{name}' specified with "
79+
'--packages-above-depth was not found'
80+
.format_map(locals()))
5381

5482
def select_packages(self, args, decorators): # noqa: D102
5583
if args.packages_up_to:
@@ -77,6 +105,23 @@ def select_packages(self, args, decorators): # noqa: D102
77105
.format_map(locals()))
78106
decorator.selected = False
79107

108+
if args.packages_above_depth and len(args.packages_above_depth) > 1:
109+
depth = args.packages_above_depth[0]
110+
select_pkgs = set(args.packages_above_depth[1:])
111+
for decorator in decorators:
112+
if decorator.descriptor.name in select_pkgs:
113+
continue
114+
if not [
115+
d for d in set(decorator.recursive_dependencies)
116+
if d in select_pkgs and d.metadata['depth'] <= depth
117+
]:
118+
if decorator.selected:
119+
pkg = decorator.descriptor
120+
logger.info(
121+
"Skipping package '{pkg.name}' in '{pkg.path}'"
122+
.format_map(locals()))
123+
decorator.selected = False
124+
80125
if args.packages_select_by_dep:
81126
deps = set(args.packages_select_by_dep)
82127
for decorator in decorators:

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ keywords = colcon
2626

2727
[options]
2828
install_requires =
29-
colcon-core
29+
colcon-core>=0.3.19
3030
packages = find:
3131
tests_require =
3232
flake8

stdeb.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[colcon-package-selection]
22
No-Python2:
3-
Depends3: python3-colcon-core
3+
Depends3: python3-colcon-core (>= 0.3.19)
44
Suite: xenial bionic stretch buster
55
X-Python3-Version: >= 3.5

test/spell_check.words

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apache
2+
argparse
23
colcon
34
deps
45
descs

0 commit comments

Comments
 (0)