-
-
Notifications
You must be signed in to change notification settings - Fork 798
Packagedcode to handle Python(requirements.txt) #2078
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 6 commits into
aboutcode-org:develop
from
rpotter12:requirements-parser
Jul 13, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b58242
implementation to handle requirements files
rpotter12 83f29f9
tests for sample requirements files
rpotter12 c5bfe41
improve docstrings
rpotter12 2fa0bcf
check PEP8 rules
rpotter12 91755d4
use valid dependency operator
rpotter12 1ee7309
add check for multi equility operator
rpotter12 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,7 +83,7 @@ def logger_debug(*args): | |
|
|
||
| @attr.s() | ||
| class PythonPackage(models.Package): | ||
| metafiles = ('metadata.json', '*setup.py', 'PKG-INFO', '*.whl', '*.egg') | ||
| metafiles = ('metadata.json', '*setup.py', 'PKG-INFO', '*.whl', '*.egg', '*requirements*.txt', '*requirements*.in') | ||
| extensions = ('.egg', '.whl', '.pyz', '.pex',) | ||
| default_type = 'pypi' | ||
| default_primary_language = 'Python' | ||
|
|
@@ -112,6 +112,8 @@ def parse(location): | |
| file_name = fileutils.file_name(location) | ||
| parsers = { | ||
| 'setup.py': parse_setup_py, | ||
| 'requirements.txt': parse_requirements_txt, | ||
| 'requirements.in': parse_requirements_txt, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also note that for the future, I have submitted this PR upstream pyupio/dparse#48
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| 'metadata.json': parse_metadata, | ||
| 'PKG-INFO': parse_pkg_info, | ||
| '.whl': parse_wheel, | ||
|
|
@@ -187,12 +189,29 @@ def parse_dependencies(location, package): | |
| package.dependencies = dependencies | ||
|
|
||
|
|
||
| dependency_type_by_extensions = { | ||
| ('.txt', '.in'): 'requirements.txt', | ||
| } | ||
|
|
||
|
|
||
| def get_dependency_type(file_name, dependency_type_by_extensions=dependency_type_by_extensions): | ||
| """ | ||
| Return the type of a dependency as a string or None given a `file_name` string. | ||
| """ | ||
| for extensions, dependency_type in dependency_type_by_extensions.items(): | ||
| if file_name.endswith(extensions): | ||
| return dependency_type | ||
|
|
||
|
|
||
| def parse_with_dparse(location): | ||
| is_dir = filetype.is_dir(location) | ||
| if is_dir: | ||
| return | ||
| file_name = fileutils.file_name(location) | ||
| if file_name not in (filetypes.requirements_txt, | ||
|
|
||
| dependency_type = get_dependency_type(file_name) | ||
|
|
||
| if dependency_type not in (filetypes.requirements_txt, | ||
| filetypes.conda_yml, | ||
| filetypes.tox_ini, | ||
| filetypes.pipfile, | ||
|
|
@@ -204,27 +223,54 @@ def parse_with_dparse(location): | |
| mode = 'r' | ||
| with open(location, mode) as f: | ||
| content = f.read() | ||
| df = dparse.parse(content, file_type=file_name) | ||
| df_dependencies = df.dependencies | ||
| if not df_dependencies: | ||
| return | ||
| package_dependencies = [] | ||
| for df_dependency in df_dependencies: | ||
| specs = df_dependency.specs | ||
| requirement = None | ||
| if specs: | ||
| requirement = str(specs) | ||
| package_dependencies.append( | ||
| models.DependentPackage( | ||
| purl=PackageURL( | ||
| type='pypi', name=df_dependency.name).to_string(), | ||
| scope='dependencies', | ||
| is_runtime=True, | ||
| is_optional=False, | ||
| requirement=requirement, | ||
| ) | ||
|
|
||
| df = dparse.parse(content, file_type=dependency_type) | ||
| df_dependencies = df.dependencies | ||
|
|
||
| if not df_dependencies: | ||
| return | ||
|
|
||
| package_dependencies = [] | ||
| for df_dependency in df_dependencies: | ||
| specs = list(df_dependency.specs._specs) | ||
| is_resolved = False | ||
| requirement = None | ||
| purl = PackageURL( | ||
| type='pypi', | ||
| name=df_dependency.name | ||
| ).to_string() | ||
| if specs: | ||
| requirement = str(df_dependency.specs) | ||
| for spec in specs: | ||
| operator = spec.operator | ||
| version = spec.version | ||
| if any(operator == element for element in ('==', '===')): | ||
| is_resolved = True | ||
| purl = PackageURL( | ||
| type='pypi', | ||
| name=df_dependency.name, | ||
| version=version | ||
| ).to_string() | ||
| package_dependencies.append( | ||
| models.DependentPackage( | ||
| purl=purl, | ||
| scope='dependencies', | ||
| is_runtime=True, | ||
| is_optional=False, | ||
| is_resolved=is_resolved, | ||
| requirement=requirement | ||
| ) | ||
| return package_dependencies | ||
| ) | ||
|
|
||
| return package_dependencies | ||
|
|
||
|
|
||
| def parse_requirements_txt(location): | ||
| """ | ||
| Return a package built from requirements.txt. | ||
| """ | ||
| package_dependencies = parse_with_dparse(location) | ||
| return PythonPackage(dependencies=package_dependencies) | ||
|
|
||
|
|
||
| def parse_setup_py(location): | ||
|
|
@@ -260,7 +306,7 @@ def parse_setup_py(location): | |
| if isinstance(kw.value, ast.Str): | ||
| setup_args[arg_name] = kw.value.s | ||
| if isinstance(kw.value, ast.List): | ||
| # We collect the elements of a list if the element is not a function call | ||
| # We collect the elements of a list if the element is not a function call | ||
| setup_args[arg_name] = [elt.s for elt in kw.value.elts if not isinstance(elt, ast.Call)] | ||
|
|
||
| description = build_description( | ||
|
|
@@ -342,7 +388,7 @@ def parse_metadata(location): | |
| parties.append(models.Party(type=models.party_person, name=name, role='contact')) | ||
|
|
||
| description = build_description( | ||
| infos.get('summary') , | ||
| infos.get('summary'), | ||
| infos.get('description') | ||
| ) | ||
|
|
||
|
|
||
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
60 changes: 60 additions & 0 deletions
60
tests/packagedcode/data/pypi/requirements_txt/sample1/output.expected.json
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,60 @@ | ||
| { | ||
| "type": "pypi", | ||
| "namespace": null, | ||
| "name": null, | ||
| "version": null, | ||
| "qualifiers": {}, | ||
| "subpath": null, | ||
| "primary_language": "Python", | ||
| "description": null, | ||
| "release_date": null, | ||
| "parties": [], | ||
| "keywords": [], | ||
| "homepage_url": null, | ||
| "download_url": null, | ||
| "size": null, | ||
| "sha1": null, | ||
| "md5": null, | ||
| "sha256": null, | ||
| "sha512": null, | ||
| "bug_tracking_url": null, | ||
| "code_view_url": null, | ||
| "vcs_url": null, | ||
| "copyright": null, | ||
| "license_expression": null, | ||
| "declared_license": null, | ||
| "notice_text": null, | ||
| "root_path": null, | ||
| "dependencies": [ | ||
| { | ||
| "purl": "pkg:pypi/setuptools", | ||
| "requirement": ">=32.0.0", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": false | ||
| }, | ||
| { | ||
| "purl": "pkg:pypi/nose", | ||
| "requirement": ">=1.3.7", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": false | ||
| }, | ||
| { | ||
| "purl": "pkg:pypi/chardet", | ||
| "requirement": ">=3.0.4", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": false | ||
| } | ||
| ], | ||
| "contains_source_code": null, | ||
| "source_packages": [], | ||
| "purl": null, | ||
| "repository_homepage_url": null, | ||
| "repository_download_url": null, | ||
| "api_data_url": null | ||
| } |
3 changes: 3 additions & 0 deletions
3
tests/packagedcode/data/pypi/requirements_txt/sample1/requirements.txt
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,3 @@ | ||
| setuptools>=32.0.0 | ||
| nose>=1.3.7 | ||
| chardet>=3.0.4 |
60 changes: 60 additions & 0 deletions
60
tests/packagedcode/data/pypi/requirements_txt/sample2/output.expected.json
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,60 @@ | ||
| { | ||
| "type": "pypi", | ||
| "namespace": null, | ||
| "name": null, | ||
| "version": null, | ||
| "qualifiers": {}, | ||
| "subpath": null, | ||
| "primary_language": "Python", | ||
| "description": null, | ||
| "release_date": null, | ||
| "parties": [], | ||
| "keywords": [], | ||
| "homepage_url": null, | ||
| "download_url": null, | ||
| "size": null, | ||
| "sha1": null, | ||
| "md5": null, | ||
| "sha256": null, | ||
| "sha512": null, | ||
| "bug_tracking_url": null, | ||
| "code_view_url": null, | ||
| "vcs_url": null, | ||
| "copyright": null, | ||
| "license_expression": null, | ||
| "declared_license": null, | ||
| "notice_text": null, | ||
| "root_path": null, | ||
| "dependencies": [ | ||
| { | ||
| "purl": "pkg:pypi/aiohttp@3.6.2", | ||
| "requirement": "==3.6.2", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": true | ||
| }, | ||
| { | ||
| "purl": "pkg:pypi/async-timeout@3.0.1", | ||
| "requirement": "==3.0.1", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": true | ||
| }, | ||
| { | ||
| "purl": "pkg:pypi/attrs@19.3.0", | ||
| "requirement": "==19.3.0", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": true | ||
| } | ||
| ], | ||
| "contains_source_code": null, | ||
| "source_packages": [], | ||
| "purl": null, | ||
| "repository_homepage_url": null, | ||
| "repository_download_url": null, | ||
| "api_data_url": null | ||
| } |
3 changes: 3 additions & 0 deletions
3
tests/packagedcode/data/pypi/requirements_txt/sample2/sample-requirements.txt
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,3 @@ | ||
| aiohttp==3.6.2 | ||
| async-timeout==3.0.1 | ||
| attrs==19.3.0 |
60 changes: 60 additions & 0 deletions
60
tests/packagedcode/data/pypi/requirements_txt/sample3/output.expected.json
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,60 @@ | ||
| { | ||
| "type": "pypi", | ||
| "namespace": null, | ||
| "name": null, | ||
| "version": null, | ||
| "qualifiers": {}, | ||
| "subpath": null, | ||
| "primary_language": "Python", | ||
| "description": null, | ||
| "release_date": null, | ||
| "parties": [], | ||
| "keywords": [], | ||
| "homepage_url": null, | ||
| "download_url": null, | ||
| "size": null, | ||
| "sha1": null, | ||
| "md5": null, | ||
| "sha256": null, | ||
| "sha512": null, | ||
| "bug_tracking_url": null, | ||
| "code_view_url": null, | ||
| "vcs_url": null, | ||
| "copyright": null, | ||
| "license_expression": null, | ||
| "declared_license": null, | ||
| "notice_text": null, | ||
| "root_path": null, | ||
| "dependencies": [ | ||
| { | ||
| "purl": "pkg:pypi/prompt-toolkit", | ||
| "requirement": ">=1.0.14", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": false | ||
| }, | ||
| { | ||
| "purl": "pkg:pypi/pygments@2.2.0", | ||
| "requirement": "==2.2.0", | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": true | ||
| }, | ||
| { | ||
| "purl": "pkg:pypi/regex", | ||
| "requirement": null, | ||
| "scope": "dependencies", | ||
| "is_runtime": true, | ||
| "is_optional": false, | ||
| "is_resolved": false | ||
| } | ||
| ], | ||
| "contains_source_code": null, | ||
| "source_packages": [], | ||
| "purl": null, | ||
| "repository_homepage_url": null, | ||
| "repository_download_url": null, | ||
| "api_data_url": null | ||
| } |
3 changes: 3 additions & 0 deletions
3
tests/packagedcode/data/pypi/requirements_txt/sample3/requirements-dev.txt
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,3 @@ | ||
| prompt_toolkit>=1.0.14 | ||
| Pygments==2.2.0 | ||
| regex |
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.