Skip to content

Commit ddc9d2a

Browse files
authored
Merge pull request #2979 from KevinJi22/external-licenses-480
Add support for external licenses in scans Philippe Ombredanne <[email protected]>
2 parents ded56e9 + f2b1e13 commit ddc9d2a

File tree

120 files changed

+2488
-812
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+2488
-812
lines changed

CHANGELOG.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@ License detection:
4646
matches in a larger license detecion. This remove a larger number of false
4747
positive or ambiguous license detections.
4848

49-
5049
- The data structure of the JSON output has changed for licenses. We now
5150
return match details once for each matched license expression rather than
5251
once for each license in a matched expression. There is a new top-level
5352
"license_references" attribute that contains the data details for each
5453
detected license only once. This data can contain the reference license text
5554
as an option.
5655

56+
- We can now detect licenses using custom license texts and license rules.
57+
These can be provided as a one off in a directory or packaged as a plugin
58+
for consistent reuse and deployment.
59+
60+
- There is a new "scancode-reindex-licenses" command that replace the
61+
"scancode --reindex-licenses" command line option which has been
62+
removed. This new command supports simpler reindexing using custom
63+
license texts and license rules contributed by plugins or stored in an
64+
additional directory.
5765
v31.2.1 - 2022-10-05
5866
----------------------------------
5967

azure-pipelines.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
--ignore=tests/licensedcode/test_detection_datadriven2.py \
3434
--ignore=tests/licensedcode/test_detection_datadriven3.py \
3535
--ignore=tests/licensedcode/test_detection_datadriven4.py \
36+
--ignore=tests/licensedcode/test_additional_license.py \
3637
tests/licensedcode
3738
3839
license_datadriven1_2: |
@@ -78,6 +79,18 @@ jobs:
7879
venv/bin/pytest -n 3 -vvs --test-suite=all \
7980
tests/licensedcode/test_zzzz_cache.py
8081
82+
# this test runs in isolation because it modifies the actual
83+
# license index with additional licenses provided by a plugin
84+
# and we use the special --test-suite=plugins marker for these
85+
# tests
86+
additional_license_combined: |
87+
venv/bin/pip install tests/licensedcode/data/additional_licenses/additional_plugin_1/
88+
venv/bin/pip install tests/licensedcode/data/additional_licenses/additional_plugin_2/
89+
venv/bin/scancode-reindex-licenses \
90+
--additional-directory tests/licensedcode/data/additional_licenses/additional_dir/
91+
venv/bin/pytest -vvs --test-suite=plugins \
92+
tests/licensedcode/test_additional_license.py
93+
8194
- template: etc/ci/azure-posix.yml
8295
parameters:
8396
job_name: ubuntu18_cpython

conftest.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
################################################################################
3939
SLOW_TEST = 'scanslow'
4040
VALIDATION_TEST = 'scanvalidate'
41+
PLUGINS_TEST = 'scanplugins'
4142

4243

4344
def pytest_configure(config):
@@ -53,8 +54,14 @@ def pytest_configure(config):
5354
': Mark a ScanCode test as a validation test, super slow, long running test.',
5455
)
5556

57+
config.addinivalue_line(
58+
'markers',
59+
PLUGINS_TEST +
60+
': Mark a ScanCode test as a special CI test to tests installing additional plugins.',
61+
)
62+
5663

57-
TEST_SUITES = 'standard', 'all', 'validate'
64+
TEST_SUITES = ('standard', 'all', 'validate', 'plugins',)
5865

5966

6067
def pytest_addoption(parser):
@@ -72,9 +79,11 @@ def pytest_addoption(parser):
7279
help='Select which test suite to run: '
7380
'"standard" runs the standard test suite designed to run reasonably fast. '
7481
'"all" runs "standard" and "slow" (long running) tests. '
75-
'"validate" runs all the tests. '
82+
'"validate" runs all the tests, except the "plugins" tests. '
83+
'"plugins" runs special plugins tests. Needs extra setup, and is used only in the CI. '
7684
'Use the @pytest.mark.scanslow marker to mark a test as "slow" test. '
7785
'Use the @pytest.mark.scanvalidate marker to mark a test as a "validate" test.'
86+
'Use the @pytest.mark.scanplugins marker to mark a test as a "plugins" test.'
7887
)
7988

8089
################################################################################
@@ -87,13 +96,19 @@ def pytest_collection_modifyitems(config, items):
8796
test_suite = config.getvalue('test_suite')
8897
run_everything = test_suite == 'validate'
8998
run_slow_test = test_suite in ('all', 'validate')
99+
run_only_plugins = test_suite == 'plugins'
90100

91101
tests_to_run = []
92102
tests_to_skip = []
93103

94104
for item in items:
95105
is_validate = bool(item.get_closest_marker(VALIDATION_TEST))
96106
is_slow = bool(item.get_closest_marker(SLOW_TEST))
107+
is_plugins = bool(item.get_closest_marker(PLUGINS_TEST))
108+
109+
if is_plugins and not run_only_plugins:
110+
tests_to_skip.append(item)
111+
continue
97112

98113
if is_validate and not run_everything:
99114
tests_to_skip.append(item)

docs/source/cli-reference/core-options.rst

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,6 @@ Comparing Progress Message Options
6969

7070
----
7171

72-
``--reindex-licenses`` Option
73-
-----------------------------
74-
75-
ScanCode maintains a license index to search for and detect licenses. When Scancode is
76-
configured for the first time, a license index is built and used in every scan thereafter.
77-
78-
This ``--reindex-licenses`` option rebuilds the license index. Running a scan with this option
79-
displays the following message to the terminal in addition to what it normally shows::
80-
81-
Checking and rebuilding the license index...
82-
83-
..
84-
[ToDo] Research and Write Better
85-
86-
----
87-
8872
``--from-json`` Option
8973
----------------------
9074

docs/source/cli-reference/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
help-text-options
99
list-options
1010
simple-examples
11+
other-commands
1112
basic-options
1213
core-options
1314
output-format

docs/source/cli-reference/list-options.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ available in the command line.
3030

3131
----
3232

33+
.. include:: /rst_snippets/scancode-reindex-licenses.rst
34+
35+
----
36+
3337
.. include:: /rst_snippets/core_options.rst
3438

3539
----
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
Other available CLIs
2+
====================
3+
4+
.. _other_cli:
5+
6+
----
7+
8+
.. include:: /rst_snippets/scancode-reindex-licenses.rst
9+
10+
----
11+
12+
.. include:: /rst_snippets/extract.rst
13+
14+
----
15+
16+
``scancode-reindex-licenses`` command
17+
-------------------------------------
18+
19+
ScanCode maintains a license index to search for and detect licenses. When Scancode is
20+
configured for the first time, a license index is built and used in every scan thereafter.
21+
22+
This ``scancode-reindex-licenses`` command rebuilds the license index. Running this command
23+
displays the following message to the terminal::
24+
25+
Checking and rebuilding the license index...
26+
27+
This has several CLI options as follows:
28+
29+
``--additional-directory`` Option:
30+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
32+
The ``--additional-directory`` option allows the user to include additional directories
33+
of licenses to use in license detection.
34+
35+
This command only needs to be run once for each set of additional directories, in all subsequent
36+
runs of Scancode with the same directories all the licenses in the directories will be cached
37+
and used in License detection. But reindexing removes these directories, if they aren't
38+
reintroduced as additional directories.
39+
40+
The directory structure should look something like this::
41+
42+
additional_license_directory/
43+
├── licenses/
44+
│ ├── example-installed-1.LICENSE
45+
│ └── example-installed-1.yaml
46+
├── rules/
47+
│ ├── example-installed-1.RULE
48+
│ └── example-installed-1.yaml
49+
50+
Here is an example of reindexing the license cache using the ``--additional-directory PATH`` option
51+
with a single directory::
52+
53+
scancode-reindex-licenses --additional-directory tests/licensedcode/data/additional_licenses/additional_dir/
54+
55+
You can also include multiple directories like so::
56+
57+
scancode-reindex-licenses --additional-directory /home/user/external_licenses/external1 --additional-directory /home/user/external_licenses/external2
58+
59+
If you want to continue running scans with ``/home/user/external_licenses/external1`` and
60+
``/home/user/external_licenses/external2``, you can simply run scans after the command above
61+
reindexing with those directories and they will be included. ::
62+
63+
scancode -l --license-text --json-pp output.json samples
64+
65+
However, if you wanted to run a scan with a new set of directories, such as
66+
``home/user/external_licenses/external1`` and ``home/user/external_licenses/external3``, you would
67+
need to reindex the license index with those directories as parameters::
68+
69+
scancode --additional-directory /home/user/external_licenses/external1 --additional-directory /home/user/external_licenses/external3
70+
71+
.. include:: /rst_snippets/note_snippets/additional_directory_is_temp.rst
72+
73+
74+
.. note::
75+
76+
You can also install external licenses through a plugin for
77+
better reproducibility and distribution of those license/rules
78+
for use in conjunction with scancode-toolkit licenses.
79+
See :ref:`install_new_license_plugin`
80+
81+
82+
``--only-builtin`` Option:
83+
^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
85+
Rebuild the license index excluding any additional license directory or additional
86+
license plugins which were added previously, i.e. with only builtin scancode license and rules.
87+
88+
This is applicable when there are additional license plugins installed already and you want to
89+
reindex the licenses without these licenses from the additional plugins.
90+
91+
.. note::
92+
93+
Running the ``--only-builtin`` command won't get rid of the installed license plugins, it
94+
would just reindex without the licenses from these plugins for once. Another reindex afterwards
95+
without this option would bring back the licenses from the plugins again in the index.
96+
97+
98+
``--all-languages`` Option:
99+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
100+
101+
Rebuild the license index including texts all languages (and not only
102+
English) and exit. This is an EXPERIMENTAL option.

docs/source/cli-reference/output-format.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ following options.
183183
"text_url": "http://fedoraproject.org/wiki/Licensing:MIT#Old_Style",
184184
"reference_url": "https://enterprise.dejacode.com/urn/urn:dje:license:mit-old-style",
185185
"spdx_license_key": null,
186-
"spdx_url": "",
186+
"spdx_url": null,
187187
"start_line": 9,
188188
"end_line": 15,
189189
"matched_rule": {

docs/source/cli-reference/synopsis.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ A sample JSON output for an individual file will look like::
234234
"text_url": "http://fedoraproject.org/wiki/Licensing:MIT#Old_Style",
235235
"reference_url": "https://enterprise.dejacode.com/urn/urn:dje:license:mit-old-style",
236236
"spdx_license_key": null,
237-
"spdx_url": "",
237+
"spdx_url": null,
238238
"start_line": 9,
239239
"end_line": 15,
240240
"matched_rule": {

docs/source/how-to-guides/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
add_new_license
1010
add_new_license_detection_rule
11+
install_new_license_plugin

0 commit comments

Comments
 (0)