Skip to content

Commit 8251767

Browse files
Refactor the group decorator for pipeline steps as optional_step … (#1442)
* Refactor the `group` decorator for pipeline steps as `optional_step` #1426 Signed-off-by: tdruez <tdruez@nexb.com> * Add optional groups explicitly in pipeline tests Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com> * Update documentation references of group to option Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com> --------- Signed-off-by: tdruez <tdruez@nexb.com> Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com> Co-authored-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent aa857b2 commit 8251767

File tree

16 files changed

+102
-63
lines changed

16 files changed

+102
-63
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ v34.9.0 (2024-11-14)
1818
"policies.yml" files, or global app settings.
1919
https://github.com/aboutcode-org/scancode.io/issues/386
2020

21+
- Refactor the ``group`` decorator for pipeline steps as ``optional_step``.
22+
The steps decorated as optional are not included by default anymore.
23+
https://github.com/aboutcode-org/scancode.io/issues/386
24+
2125
- Add a new ``PublishToFederatedCode`` pipeline (addon) to push scan result
2226
to FederatedCode.
2327
https://github.com/nexB/scancode.io/pull/1400

aboutcode/pipeline/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ class PrintMessages(BasePipeline):
2424
PrintMessages().execute()
2525
```
2626

27-
### Groups and steps selection
27+
### Options and steps selection
2828

2929
```python
3030
from aboutcode.pipeline import BasePipeline
31-
from aboutcode.pipeline import group
31+
from aboutcode.pipeline import optional_step
32+
3233

3334
class PrintMessages(BasePipeline):
3435
@classmethod
@@ -38,7 +39,7 @@ class PrintMessages(BasePipeline):
3839
def step1(self):
3940
print("Message from step1")
4041

41-
@group("foo")
42+
@optional_step("foo")
4243
def step2(self):
4344
print("Message from step2")
4445

aboutcode/pipeline/__init__.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import logging
2424
import traceback
25+
import warnings
2526
from datetime import datetime
2627
from datetime import timezone
2728
from pydoc import getdoc
@@ -53,25 +54,24 @@ def get_steps(cls, groups=None):
5354
"""
5455
Return the list of steps defined in the ``steps`` class method.
5556
56-
If the optional ``groups`` parameter is provided, only include steps labeled
57-
with groups that intersect with the provided list. If a step has no groups or
58-
if ``groups`` is not specified, include the step in the result.
57+
By default, all steps decorated with ``optional_step`` are not included.
58+
A list of optional steps can be included using the ``groups`` parameter.
5959
"""
6060
if not callable(cls.steps):
6161
raise TypeError("Use a ``steps(cls)`` classmethod to declare the steps.")
6262

6363
steps = cls.steps()
64+
groups = groups or []
6465

6566
if initial_steps := cls.get_initial_steps():
6667
steps = (*initial_steps, *steps)
6768

68-
if groups is not None:
69-
steps = tuple(
70-
step
71-
for step in steps
72-
if not getattr(step, "groups", [])
73-
or set(getattr(step, "groups")).intersection(groups)
74-
)
69+
steps = tuple(
70+
step
71+
for step in steps
72+
if not getattr(step, "groups", [])
73+
or set(getattr(step, "groups")).intersection(groups)
74+
)
7575

7676
return steps
7777

@@ -123,7 +123,7 @@ def get_available_groups(cls):
123123
return sorted(
124124
set(
125125
group_name
126-
for step in cls.get_steps()
126+
for step in cls.steps()
127127
for group_name in getattr(step, "groups", [])
128128
)
129129
)
@@ -219,8 +219,8 @@ class BasePipeline(PipelineDefinition, PipelineRun):
219219
"""
220220

221221

222-
def group(*groups):
223-
"""Mark a function as part of a particular group."""
222+
def optional_step(*groups):
223+
"""Mark a step function as optional and part of a group."""
224224

225225
def decorator(obj):
226226
if hasattr(obj, "groups"):
@@ -232,6 +232,17 @@ def decorator(obj):
232232
return decorator
233233

234234

235+
def group(*groups):
236+
"""Backward compatibility."""
237+
warnings.warn(
238+
"The `group` decorator is deprecated and will be "
239+
"removed in a future release. Use `optional_step` instead.",
240+
DeprecationWarning,
241+
stacklevel=2,
242+
)
243+
return optional_step(*groups)
244+
245+
235246
def humanize_time(seconds):
236247
"""Convert the provided ``seconds`` number into human-readable time."""
237248
message = f"{seconds:.0f} seconds"

docs/built-in-pipelines.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Built-in Pipelines
66
Pipelines in ScanCode.io are Python scripts that facilitate code analysis by
77
executing a sequence of steps. The platform provides the following built-in pipelines:
88

9+
.. note::
10+
Some pipelines have optional steps which are enabled only when they are
11+
selected explicitly.
12+
913
.. tip::
1014
If you are unsure which pipeline suits your requirements best, check out the
1115
:ref:`faq_which_pipeline` section for guidance.

docs/command-line-interface.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Optional arguments:
103103
- ``--pipeline PIPELINES`` Pipelines names to add on the project.
104104

105105
.. tip::
106-
Use the "pipeline_name:group1,group2" syntax to select steps groups:
106+
Use the "pipeline_name:option1,option2" syntax to select optional steps:
107107

108108
``--pipeline map_deploy_to_develop:Java,JavaScript``
109109

@@ -230,7 +230,7 @@ add the docker pipeline to your project::
230230
$ scanpipe add-pipeline --project foo analyze_docker_image
231231

232232
.. tip::
233-
Use the "pipeline_name:group1,group2" syntax to select steps groups:
233+
Use the "pipeline_name:option1,option2" syntax to select optional steps:
234234

235235
``--pipeline map_deploy_to_develop:Java,JavaScript``
236236

@@ -417,7 +417,7 @@ For example, running the ``inspect_packages`` pipeline on a manifest file:
417417
418418
$ run inspect_packages path/to/package.json > results.json
419419
420-
.. tip:: Use the "pipeline_name:group1,group2" syntax to select steps groups::
420+
.. tip:: Use the "pipeline_name:option1,option2" syntax to select optional steps::
421421

422422
$ run inspect_packages:StaticResolver package.json > results.json
423423

docs/rest-api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Using cURL:
136136

137137
.. tip::
138138

139-
Use the "pipeline_name:group1,group2" syntax to select steps groups:
139+
Use the "pipeline_name:option1,option2" syntax to select optional steps:
140140

141141
``"pipeline": "map_deploy_to_develop:Java,JavaScript"``
142142

@@ -293,7 +293,7 @@ Data:
293293
- ``execute_now``: ``true`` or ``false``
294294

295295
.. tip::
296-
Use the "pipeline_name:group1,group2" syntax to select steps groups:
296+
Use the "pipeline_name:option1,option2" syntax to select optional steps:
297297

298298
``"pipeline": "map_deploy_to_develop:Java,JavaScript"``
299299

scanpipe/management/commands/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def validate_copy_from(copy_from):
229229

230230
def extract_group_from_pipelines(pipelines):
231231
"""
232-
Add support for the ":group1,group2" suffix in pipeline data.
232+
Add support for the ":option1,option2" suffix in pipeline data.
233233
234234
For example: "map_deploy_to_develop:Java,JavaScript"
235235
"""

scanpipe/management/commands/create-project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def add_arguments(self, parser):
4242
help=(
4343
"Pipelines names to add to the project. "
4444
"The pipelines are added and executed based on their given order. "
45-
'Groups can be provided using the "pipeline_name:group1,group2" syntax.'
45+
'Groups can be provided using the "pipeline_name:option1,option2" '
46+
"syntax."
4647
),
4748
)
4849
parser.add_argument(

scanpipe/management/commands/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def add_arguments(self, parser):
4242
help=(
4343
"One or more pipeline to run. "
4444
"The pipelines executed based on their given order. "
45-
'Groups can be provided using the "pipeline_name:group1,group2" syntax.'
45+
'Groups can be provided using the "pipeline_name:option1,option2"'
46+
" syntax."
4647
),
4748
)
4849
parser.add_argument(

scanpipe/pipelines/deploy_to_develop.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
2121
# Visit https://github.com/aboutcode-org/scancode.io for support and download.
2222

23-
from aboutcode.pipeline import group
23+
from aboutcode.pipeline import optional_step
2424
from scanpipe import pipes
2525
from scanpipe.pipelines import Pipeline
2626
from scanpipe.pipes import d2d
@@ -168,35 +168,35 @@ def match_archives_to_purldb(self):
168168
logger=self.log,
169169
)
170170

171-
@group("Java")
171+
@optional_step("Java")
172172
def find_java_packages(self):
173173
"""Find the java package of the .java source files."""
174174
d2d.find_java_packages(self.project, logger=self.log)
175175

176-
@group("Java")
176+
@optional_step("Java")
177177
def map_java_to_class(self):
178178
"""Map a .class compiled file to its .java source."""
179179
d2d.map_java_to_class(project=self.project, logger=self.log)
180180

181-
@group("Java")
181+
@optional_step("Java")
182182
def map_jar_to_source(self):
183183
"""Map .jar files to their related source directory."""
184184
d2d.map_jar_to_source(project=self.project, logger=self.log)
185185

186-
@group("JavaScript")
186+
@optional_step("JavaScript")
187187
def map_javascript(self):
188188
"""
189189
Map a packed or minified JavaScript, TypeScript, CSS and SCSS
190190
to its source.
191191
"""
192192
d2d.map_javascript(project=self.project, logger=self.log)
193193

194-
@group("Elf")
194+
@optional_step("Elf")
195195
def map_elf(self):
196196
"""Map ELF binaries to their sources."""
197197
d2d.map_elfs(project=self.project, logger=self.log)
198198

199-
@group("Go")
199+
@optional_step("Go")
200200
def map_go(self):
201201
"""Map Go binaries to their sources."""
202202
d2d.map_go_paths(project=self.project, logger=self.log)
@@ -225,22 +225,22 @@ def match_resources_to_purldb(self):
225225
logger=self.log,
226226
)
227227

228-
@group("JavaScript")
228+
@optional_step("JavaScript")
229229
def map_javascript_post_purldb_match(self):
230230
"""Map minified javascript file based on existing PurlDB match."""
231231
d2d.map_javascript_post_purldb_match(project=self.project, logger=self.log)
232232

233-
@group("JavaScript")
233+
@optional_step("JavaScript")
234234
def map_javascript_path(self):
235235
"""Map javascript file based on path."""
236236
d2d.map_javascript_path(project=self.project, logger=self.log)
237237

238-
@group("JavaScript")
238+
@optional_step("JavaScript")
239239
def map_javascript_colocation(self):
240240
"""Map JavaScript files based on neighborhood file mapping."""
241241
d2d.map_javascript_colocation(project=self.project, logger=self.log)
242242

243-
@group("JavaScript")
243+
@optional_step("JavaScript")
244244
def map_thirdparty_npm_packages(self):
245245
"""Map thirdparty package using package.json metadata."""
246246
d2d.map_thirdparty_npm_packages(project=self.project, logger=self.log)

0 commit comments

Comments
 (0)