Skip to content

Commit 1fc0a12

Browse files
committed
Add basic working command
This is not resolving anything yet, but all the basic parts are in place. Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent f079bcf commit 1fc0a12

11 files changed

Lines changed: 664 additions & 599 deletions

src/_packagedcode/models.py

Lines changed: 226 additions & 233 deletions
Large diffs are not rendered by default.

src/_packagedcode/pypi.py

Lines changed: 288 additions & 273 deletions
Large diffs are not rendered by default.

src/_packagedcode/utils.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,21 @@
1212
"""
1313

1414
PLAIN_URLS = (
15-
'https://',
16-
'http://',
15+
"https://",
16+
"http://",
1717
)
1818

1919
VCS_URLS = (
20-
'git://',
21-
'git+git://',
22-
'git+https://',
23-
'git+http://',
24-
25-
'hg://',
26-
'hg+http://',
27-
'hg+https://',
28-
29-
'svn://',
30-
'svn+https://',
31-
'svn+http://',
20+
"git://",
21+
"git+git://",
22+
"git+https://",
23+
"git+http://",
24+
"hg://",
25+
"hg+http://",
26+
"hg+https://",
27+
"svn://",
28+
"svn+https://",
29+
"svn+http://",
3230
)
3331

3432

@@ -77,34 +75,35 @@ def normalize_vcs_url(repo_url, vcs_tool=None):
7775
if repo_url.startswith(VCS_URLS + PLAIN_URLS):
7876
return repo_url
7977

80-
if repo_url.startswith('git@'):
81-
tool, _, right = repo_url.partition('@')
82-
if ':' in repo_url:
83-
host, _, repo = right.partition(':')
78+
if repo_url.startswith("git@"):
79+
tool, _, right = repo_url.partition("@")
80+
if ":" in repo_url:
81+
host, _, repo = right.partition(":")
8482
else:
8583
# git@github.com/Filirom1/npm2aur.git
86-
host, _, repo = right.partition('/')
84+
host, _, repo = right.partition("/")
8785

88-
if any(r in host for r in ('bitbucket', 'gitlab', 'github')):
89-
scheme = 'https'
86+
if any(r in host for r in ("bitbucket", "gitlab", "github")):
87+
scheme = "https"
9088
else:
91-
scheme = 'git'
89+
scheme = "git"
9290

93-
return '%(scheme)s://%(host)s/%(repo)s' % locals()
91+
return "%(scheme)s://%(host)s/%(repo)s" % locals()
9492

9593
# FIXME: where these URL schemes come from??
96-
if repo_url.startswith(('bitbucket:', 'gitlab:', 'github:', 'gist:')):
94+
if repo_url.startswith(("bitbucket:", "gitlab:", "github:", "gist:")):
9795
hoster_urls = {
98-
'bitbucket': 'https://bitbucket.org/%(repo)s',
99-
'github': 'https://github.com/%(repo)s',
100-
'gitlab': 'https://gitlab.com/%(repo)s',
101-
'gist': 'https://gist.github.com/%(repo)s', }
102-
hoster, _, repo = repo_url.partition(':')
96+
"bitbucket": "https://bitbucket.org/%(repo)s",
97+
"github": "https://github.com/%(repo)s",
98+
"gitlab": "https://gitlab.com/%(repo)s",
99+
"gist": "https://gist.github.com/%(repo)s",
100+
}
101+
hoster, _, repo = repo_url.partition(":")
103102
return hoster_urls[hoster] % locals()
104103

105-
if len(repo_url.split('/')) == 2:
104+
if len(repo_url.split("/")) == 2:
106105
# implicit github, but that's only on NPM?
107-
return f'https://github.com/{repo_url}'
106+
return f"https://github.com/{repo_url}"
108107

109108
return repo_url
110109

@@ -113,14 +112,14 @@ def build_description(summary, description):
113112
"""
114113
Return a description string from a summary and description
115114
"""
116-
summary = (summary or '').strip()
117-
description = (description or '').strip()
115+
summary = (summary or "").strip()
116+
description = (description or "").strip()
118117

119118
if not description:
120119
description = summary
121120
else:
122121
if summary and summary not in description:
123-
description = '\n'.join([summary , description])
122+
description = "\n".join([summary, description])
124123

125124
return description
126125

@@ -164,7 +163,7 @@ def find_root_resource(path, resource, codebase):
164163
"""
165164
if not resource.path.endswith(path):
166165
return
167-
for _seg in path.split('/'):
166+
for _seg in path.split("/"):
168167
resource = resource.parent(codebase)
169168
if not resource:
170169
return
@@ -176,6 +175,7 @@ def yield_dependencies_from_package_data(package_data, datafile_path, package_ui
176175
Yield a Dependency for each dependency from ``package_data.dependencies``
177176
"""
178177
from _packagedcode import models
178+
179179
dependent_packages = package_data.dependencies
180180
if dependent_packages:
181181
yield from models.Dependency.from_dependent_packages(
@@ -191,6 +191,7 @@ def yield_dependencies_from_package_resource(resource, package_uid=None):
191191
Yield a Dependency for each dependency from each package from``resource.package_data``
192192
"""
193193
from _packagedcode import models
194+
194195
for pkg_data in resource.package_data:
195196
pkg_data = models.PackageData.from_dict(pkg_data)
196197
yield from yield_dependencies_from_package_data(pkg_data, resource.path, package_uid)

src/python_inspector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
66
# See https://github.com/nexB/scancode-toolkit for support or download.
77
# See https://aboutcode.org for more information about nexB OSS projects.
8-
#
8+
#

src/python_inspector/cli_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ class FileOptionType(click.File):
2020
"""
2121

2222
def convert(self, value, param, ctx):
23-
known_opts = set(chain.from_iterable(
24-
p.opts for p in ctx.command.params if isinstance(p, click.Option)))
23+
known_opts = set(
24+
chain.from_iterable(p.opts for p in ctx.command.params if isinstance(p, click.Option))
25+
)
2526
if value in known_opts:
2627
self.fail(
27-
'Illegal file name conflicting with an option name: '
28-
f'{ os.fsdecode(value)}. '
28+
"Illegal file name conflicting with an option name: "
29+
f"{ os.fsdecode(value)}. "
2930
'Use the special "-" file name to print results on screen/stdout.',
3031
param,
3132
ctx,

src/python_inspector/dependencies.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,42 @@
99
# See https://aboutcode.org for more information about nexB OSS projects.
1010
#
1111

12+
from packageurl import PackageURL
13+
from packaging.requirements import Requirement
14+
from pip_requirements_parser import InstallRequirement
15+
1216
from _packagedcode import models
1317
from _packagedcode.pypi import PipRequirementsFileHandler
14-
from pip_requirements_parser import InstallRequirement
15-
from packaging.requirements import Requirement
16-
from packageurl import PackageURL
1718

1819
"""
1920
Utilities to resolve dependencies .
2021
"""
2122

23+
TRACE = False
24+
2225

2326
def get_dependencies_from_requirements(requirements_file="requirements.txt", *args, **kwargs):
2427
"""
2528
Yield DependentPackage for each requirement in a `requirement`
2629
file.
2730
"""
28-
for packages_data in PipRequirementsFileHandler.parse(location=requirements_file):
29-
for package_data in packages_data:
30-
for dependent_package in package_data.dependencies:
31-
yield dependent_package
31+
for package_data in PipRequirementsFileHandler.parse(location=requirements_file):
32+
for dependent_package in package_data.dependencies:
33+
if TRACE:
34+
print(
35+
"dependent_package.extracted_requirement:",
36+
dependent_package.extracted_requirement,
37+
)
38+
yield dependent_package
3239

3340

3441
def get_dependency(specifier, *args, **kwargs):
3542
"""
3643
Return a DependentPackage given a requirement ``specifier`` string.
3744
3845
For example:
39-
>>> assert get_dependency("foo==1.2.3") == ("foo", "1.2.3")
40-
>>> assert get_dependency("fooA==1.2.3.DEV1") == ("fooa", "1.2.3.dev1")
46+
>>> dep = get_dependency("foo==1.2.3")
47+
>>> assert dep.purl == "pkg:pypi/foo@1.2.3"
4148
"""
4249
specifier = specifier and "".join(specifier.lower().split())
4350
assert specifier, f"specifier is required but empty:{specifier!r}"
@@ -50,14 +57,14 @@ def get_dependency(specifier, *args, **kwargs):
5057
requirement_line=specifier,
5158
)
5259

53-
scope = 'install'
60+
scope = "install"
5461
is_runtime = True
5562
is_optional = False
5663

5764
if ir.name:
5865
# will be None if not pinned
5966
version = ir.get_pinned_version
60-
purl = PackageURL(type='pypi', name=ir.name, version=version)
67+
purl = PackageURL(type="pypi", name=ir.name, version=version).to_string()
6168

6269
return models.DependentPackage(
6370
purl=purl,

0 commit comments

Comments
 (0)