1
1
# Copyright 2016-2018 Dirk Thomas
2
2
# Licensed under the Apache License, Version 2.0
3
3
4
+ import argparse
4
5
import sys
5
6
6
7
from colcon_core .package_selection import logger
7
8
from colcon_core .package_selection import PackageSelectionExtensionPoint
8
9
from colcon_core .plugin_system import satisfies_version
9
10
10
11
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
+
11
28
class DependenciesPackageSelection (PackageSelectionExtensionPoint ):
12
29
"""Select packages based on their dependencies."""
13
30
@@ -25,6 +42,11 @@ def add_arguments(self, *, parser): # noqa: D102
25
42
'--packages-above' , nargs = '*' , metavar = 'PKG_NAME' ,
26
43
help = 'Only process a subset of packages and packages which '
27
44
'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' )
28
50
29
51
parser .add_argument (
30
52
'--packages-select-by-dep' , nargs = '*' , metavar = 'DEP_NAME' ,
@@ -50,6 +72,12 @@ def check_parameters(self, args, pkg_names): # noqa: D102
50
72
"Package '{name}' specified with --packages-above "
51
73
'was not found'
52
74
.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 ()))
53
81
54
82
def select_packages (self , args , decorators ): # noqa: D102
55
83
if args .packages_up_to :
@@ -77,6 +105,23 @@ def select_packages(self, args, decorators): # noqa: D102
77
105
.format_map (locals ()))
78
106
decorator .selected = False
79
107
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
+
80
125
if args .packages_select_by_dep :
81
126
deps = set (args .packages_select_by_dep )
82
127
for decorator in decorators :
0 commit comments