Skip to content

Commit 28970d1

Browse files
committed
Support generic invocation of package_discovery functions
Other function groups like augmentation, identification, and discovery, can be invoked with an explicit list of extensions instances. This supports scenarios where we want to use extensions other than the ones registered for colcon_core.
1 parent faee881 commit 28970d1

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

colcon_core/package_selection/__init__.py

+46-20
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ def select_packages(self, *, args, decorators):
7171
raise NotImplementedError()
7272

7373

74-
def add_arguments(parser):
74+
def add_arguments(
75+
parser, *, discovery_extensions=None, selection_extensions=None,
76+
):
7577
"""
7678
Add the command line arguments for the package selection extensions.
7779
@@ -80,9 +82,9 @@ def add_arguments(parser):
8082
8183
:param parser: The argument parser
8284
"""
83-
add_package_discovery_arguments(parser)
85+
add_package_discovery_arguments(parser, extensions=discovery_extensions)
8486

85-
_add_package_selection_arguments(parser)
87+
_add_package_selection_arguments(parser, extensions=selection_extensions)
8688

8789

8890
def get_package_selection_extensions(*, group_name=None):
@@ -101,15 +103,16 @@ def get_package_selection_extensions(*, group_name=None):
101103
return order_extensions_by_priority(extensions)
102104

103105

104-
def _add_package_selection_arguments(parser):
106+
def _add_package_selection_arguments(parser, *, extensions=None):
105107
"""
106108
Add the command line arguments for the package selection extensions.
107109
108110
:param parser: The argument parser
109111
"""
110-
package_selection_extensions = get_package_selection_extensions()
112+
if extensions is None:
113+
extensions = get_package_selection_extensions()
111114
group = parser.add_argument_group(title='Package selection arguments')
112-
for extension in package_selection_extensions.values():
115+
for extension in extensions.values():
113116
try:
114117
retval = extension.add_arguments(parser=group)
115118
assert retval is None, 'add_arguments() should return None'
@@ -125,7 +128,9 @@ def _add_package_selection_arguments(parser):
125128
def get_packages(
126129
args, *,
127130
additional_argument_names=None,
128-
direct_categories=None, recursive_categories=None
131+
direct_categories=None, recursive_categories=None,
132+
discovery_extensions=None, identification_extensions=None,
133+
augmentation_extensions=None, selection_extensions=None,
129134
):
130135
"""
131136
Get the selected package decorators in topological order.
@@ -146,12 +151,18 @@ def get_packages(
146151
package names
147152
"""
148153
descriptors = get_package_descriptors(
149-
args, additional_argument_names=additional_argument_names)
154+
args, additional_argument_names=additional_argument_names,
155+
discovery_extensions=discovery_extensions,
156+
identification_extensions=identification_extensions,
157+
augmentation_extensions=augmentation_extensions,
158+
selection_extensions=selection_extensions)
150159
decorators = topological_order_packages(
151160
descriptors,
152161
direct_categories=direct_categories,
153162
recursive_categories=recursive_categories)
154-
select_package_decorators(args, decorators)
163+
select_package_decorators(
164+
args, decorators,
165+
selection_extensions=selection_extensions)
155166

156167
# check for duplicate package names
157168
pkgs = [m.descriptor for m in decorators if m.selected]
@@ -169,7 +180,11 @@ def get_packages(
169180
return decorators
170181

171182

172-
def get_package_descriptors(args, *, additional_argument_names=None):
183+
def get_package_descriptors(
184+
args, *, additional_argument_names=None, discovery_extensions=None,
185+
identification_extensions=None, augmentation_extensions=None,
186+
selection_extensions=None,
187+
):
173188
"""
174189
Get the package descriptors.
175190
@@ -185,20 +200,28 @@ def get_package_descriptors(args, *, additional_argument_names=None):
185200
:py:class:`colcon_core.package_descriptor.PackageDescriptor`
186201
:rtype: set
187202
"""
188-
extensions = get_package_identification_extensions()
189-
descriptors = discover_packages(args, extensions)
203+
if identification_extensions is None:
204+
identification_extensions = get_package_identification_extensions()
205+
descriptors = discover_packages(
206+
args, identification_extensions,
207+
discovery_extensions=discovery_extensions)
190208

191209
pkg_names = {d.name for d in descriptors}
192-
_check_package_selection_parameters(args, pkg_names)
210+
_check_package_selection_parameters(
211+
args, pkg_names, selection_extensions=selection_extensions)
193212

194213
augment_packages(
195-
descriptors, additional_argument_names=additional_argument_names)
214+
descriptors, additional_argument_names=additional_argument_names,
215+
augmentation_extensions=augmentation_extensions)
196216
return descriptors
197217

198218

199-
def _check_package_selection_parameters(args, pkg_names):
200-
package_selection_extensions = get_package_selection_extensions()
201-
for extension in package_selection_extensions.values():
219+
def _check_package_selection_parameters(
220+
args, pkg_names, *, selection_extensions=None,
221+
):
222+
if selection_extensions is None:
223+
selection_extensions = get_package_selection_extensions()
224+
for extension in selection_extensions.values():
202225
try:
203226
retval = extension.check_parameters(args=args, pkg_names=pkg_names)
204227
assert retval is None, 'check_parameters() should return None'
@@ -211,7 +234,9 @@ def _check_package_selection_parameters(args, pkg_names):
211234
# skip failing extension, continue with next one
212235

213236

214-
def select_package_decorators(args, decorators):
237+
def select_package_decorators(
238+
args, decorators, *, selection_extensions=None,
239+
):
215240
"""
216241
Select the package decorators based on the command line arguments.
217242
@@ -222,8 +247,9 @@ def select_package_decorators(args, decorators):
222247
"""
223248
# filtering must happen after the topological ordering since otherwise
224249
# packages in the middle of the dependency graph might be missing
225-
package_selection_extensions = get_package_selection_extensions()
226-
for extension in package_selection_extensions.values():
250+
if selection_extensions is None:
251+
selection_extensions = get_package_selection_extensions()
252+
for extension in selection_extensions.values():
227253
try:
228254
retval = extension.select_packages(
229255
args=args, decorators=decorators)

0 commit comments

Comments
 (0)