-
-
Notifications
You must be signed in to change notification settings - Fork 791
Add support for external licenses in scans #2979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pombredanne
merged 45 commits into
aboutcode-org:develop
from
kji22:external-licenses-480
Oct 28, 2022
Merged
Changes from 41 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
05d9197
Add support for external licenses in scans #480
kji22 4989205
Add documentation for new ``--dir`` CLI option
kji22 0cbfff1
Enable using installed licenses in scans #2994
kji22 3376b9c
Add CI job to test detecting installed license
kji22 681436d
Add documentation for installed license plugins
kji22 e741ffd
Enable installed rules to be used in detection
kji22 afae692
Move `licensedcode_test_utils` into main wheel
kji22 f576dfa
Add Windows and MacOS images to Azure pipelines
kji22 a68c4cc
Add rule and license validation when index is made
kji22 789cf76
Add SPDX license key to example licenses
kji22 e7809ee
Refactor CLI option for external licenses
kji22 cdf627f
revise documentation for --additional-license-directory
kji22 7e66c9a
fix docstrings
kji22 9679feb
refactor API to not use additional_directories except when reindexing
kji22 6aae7e2
Always consider multiple directories when generating index
kji22 f13ed1d
Ensure licenses are unique when loading licenses from multiple direct…
kji22 9b03eea
add callback for --additional-license-directory and include additiona…
kji22 40f3be9
fix help.txt to include --additional-license-directory
kji22 983024e
fix docs
kji22 c8391d0
fix basic-options.rst
kji22 1bc43af
add check in cli.py to see if cached directories file actually exists
kji22 fc7b967
fix expected test results directory path
kji22 597c616
fix underline in docs
kji22 85001c1
fix expected results for external and installed license tests
kji22 7497009
put license installation into posix azure pipeline
kji22 d6068c6
remove setuptools and wheel from setup.py
kji22 16513ff
change from scan to reindex licenses in license library validation test
kji22 17df9d0
Add is_builtin field to Licenses and Rules and modify url output
kji22 3762ca5
fix methods based on previous changes
kji22 ba9740b
add new license provider plugin for additional licenses
kji22 ba11f05
Test that additional license plugin works
pombredanne 61c3283
Merge latest develop
pombredanne a4ebbe0
Use new "scanplugins" pytest marker
pombredanne 1db9437
Add CHANGELOG entry
pombredanne 8df0e27
fix expected scan results after installed license CI change
kji22 f53886f
Reorganize additional license tests
AyanSinhaMahapatra 5361052
Move reindex licenses to a seperate script
AyanSinhaMahapatra a477e54
Merge branch 'develop' into external-licenses-480
AyanSinhaMahapatra 6412039
Add external licenses info in header
AyanSinhaMahapatra 6e14d8a
Add is_builtin flag to matched_rule data
AyanSinhaMahapatra 044f60d
Do not return empty strings in license data
AyanSinhaMahapatra f201faa
Add --only-builtin falg for scancode-reindex-licenses
AyanSinhaMahapatra 095c8ed
Update docs for external licenses
AyanSinhaMahapatra 54fb102
Refactor external licenses code
AyanSinhaMahapatra f2b1e13
Improve CHANGELOG.rst
pombredanne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,4 @@ | |
|
|
||
| add_new_license | ||
| add_new_license_detection_rule | ||
| install_new_license_plugin | ||
139 changes: 139 additions & 0 deletions
139
docs/source/how-to-guides/install_new_license_plugin.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| .. _install_new_license_plugin: | ||
|
|
||
| How to Install External License Plugins to Use in License Detection | ||
|
AyanSinhaMahapatra marked this conversation as resolved.
Outdated
|
||
| =================================================================== | ||
|
|
||
| Users can install external licenses and rules in the form of plugins. These | ||
| licenses and rules are then used in license detection. | ||
|
|
||
| How to create a plugin containing external licenses and/or rules | ||
| ---------------------------------------------------------------- | ||
|
|
||
| To create a plugin with external licenses or rules, we must create a Python package | ||
| containing the license and/or rule files. Python packages can have many different | ||
| file structures. You can find an example package in | ||
| ``tests/licensedcode/data/example_external_licenses/licenses_to_install1``. | ||
|
|
||
| This is the basic structure of the example plugin:: | ||
|
|
||
| licenses_to_install1/ | ||
| ├── src/ | ||
| │ └── licenses_to_install1/ | ||
| │ ├── licenses/ | ||
| │ │ ├── example-installed-1.LICENSE | ||
| │ │ └── example-installed-1.yaml | ||
| | ├── rules/ | ||
| │ │ ├── example-installed-1.RULE | ||
| │ │ └── example-installed-1.yaml | ||
| │ └── __init__.py | ||
| ├── gpl-1.0.LICENSE | ||
| ├── MANIFEST.in | ||
| ├── setup.cfg | ||
| └── setup.py | ||
|
|
||
| Key points to note | ||
| ------------------ | ||
|
|
||
| Entry points definition in ``setup.py`` | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| First, in ``setup.py``, you must provide an entry point called ``scancode_location_provider``. | ||
| This allows ScanCode Toolkit to discover the plugin and use it in license detection. | ||
| Here is the definition of ``entry_points`` in ``setup.py``:: | ||
|
|
||
| entry_points={ | ||
| 'scancode_location_provider': [ | ||
| 'licenses_to_install1 = licenses_to_install1:LicensesToInstall1Paths', | ||
| ], | ||
| }, | ||
|
|
||
| The ``scancode_location_provider`` entry point maps to a list with information about the plugin. | ||
| The variable ``licenses_to_install1`` is the name of the entry point. All entry point names | ||
| **must** start with the prefix ``licenses``, or else ScanCode Toolkit will not use them in | ||
| license detection. | ||
|
|
||
| Directory structure | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| ``licenses_to_install1`` is set to ``licenses_to_install1:LicensesToInstall1Paths``. | ||
| Note that in ``src``, we have another directory called ``licenses_to_install1`` and in | ||
| ``licenses_to_install1/__init__.py``, we define the class ``LicensesToInstall1Paths``. | ||
| These two values make up the entry point definition. | ||
|
|
||
| ``LicensesToInstall1Paths`` is a subclass of ``LocationProviderPlugin`` and | ||
| implements the method ``get_locations()``. The class you define in ``__init__.py`` | ||
| must also subclass ``LocationProviderPlugin`` and implement this method. | ||
|
|
||
| Finally, the same directory containing the class definition must also contain the | ||
| licenses and/or rules. Licenses must be contained in a directory called ``licenses`` and rules | ||
| must be contained in a directory called ``rules``. | ||
|
|
||
| See :ref:`add_new_license_for_det` and :ref:`add_new_license_det_rule` to understand | ||
| the structure of license and rule files, respectively. | ||
|
|
||
| After creating this plugin, you can upload it to PyPI so that others can use it, or you can | ||
| leave it as a local directory. | ||
|
|
||
| Installing and using the plugin | ||
| ------------------------------- | ||
| To use the plugin in license detection, all you need to do is install it using ``pip``. | ||
| Once it is installed, the contained licenses and rules will automatically be used in | ||
| license detection assuming the plugin follows the correct directory structure conventions. | ||
|
|
||
| Writing tests for new installed licenses | ||
| ---------------------------------------- | ||
|
|
||
| Look at ``tests/licensedcode/data/example_external_licenses/licenses_to_install1`` to see | ||
| an example of a plugin with tests. The tests are contained in the ``tests`` directory:: | ||
|
|
||
| licenses_to_install1/ | ||
| ├── src/ | ||
| │ └── licenses_to_install1/ | ||
| │ ├── licenses/ | ||
| │ │ ├── example-installed-1.LICENSE | ||
| │ │ └── example-installed-1.yaml | ||
| │ ├── rules/ | ||
| │ │ ├── example-installed-1.RULE | ||
| │ │ └── example-installed-1.yaml | ||
| │ └── __init__.py/ | ||
| ├── tests/ | ||
| │ ├── data/ | ||
| │ │ ├── example-installed-1.txt | ||
| │ │ └── example-installed-1.txt.yml | ||
| │ └── test_detection_datadriven.py | ||
| ├── gpl-1.0.LICENSE | ||
| ├── MANIFEST.in | ||
| ├── setup.cfg | ||
| └── setup.py | ||
|
|
||
| To write your own tests, first make sure ``setup.py`` includes ``scancode-toolkit`` | ||
| as a dependency:: | ||
|
|
||
| ... | ||
| install_requires=[ | ||
| 'scancode-toolkit', | ||
| ], | ||
| ... | ||
|
|
||
| Then you can define a test class and call the ``build_tests`` method defined in | ||
| ``licensedcode_test_utils``, passing in the test directory and the test class as parameters:: | ||
|
|
||
| TEST_DIR = abspath(join(dirname(__file__), 'data')) | ||
|
|
||
|
|
||
| class TestLicenseDataDriven1(unittest.TestCase): | ||
| pass | ||
|
|
||
|
|
||
| licensedcode_test_utils.build_tests( | ||
| TEST_DIR, | ||
| clazz=TestLicenseDataDriven1, regen=scancode_config.REGEN_TEST_FIXTURES) | ||
|
|
||
| The ``tests/data`` directory contains a pair of files for each license: | ||
| a license text file and a YAML file specifying the expected license expressions from the test. | ||
|
|
||
| Finally, to run the test, do the following: | ||
|
|
||
| 1. Create a virtual environment to install the package into. | ||
| 2. Install the package using ``pip``, e.g. ``pip install ./licenses_to_install1``. | ||
| 3. Run the tests, e.g. ``py.test tests/test_detection_datadriven.py``. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .. note:: | ||
|
|
||
| All the Core Options are independent options, i.e. They don't depend on other options. | ||
| All the Core Options except for ``--additional-license-directory`` are independent options, i.e. | ||
|
AyanSinhaMahapatra marked this conversation as resolved.
Outdated
|
||
| They don't depend on other options. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.