Skip to content

Commit a303e54

Browse files
committed
Collect pubspec deps, authors and extra data #2110
Signed-off-by: Philippe Ombredanne <[email protected]>
1 parent 2ab4919 commit a303e54

File tree

1 file changed

+71
-26
lines changed

1 file changed

+71
-26
lines changed

src/packagedcode/pubspec.py

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ def logger_debug(*args):
4343
TODO:
4444
- license is only in a LICENSE file
4545
https://dart.dev/tools/pub/publishing#preparing-to-publish
46+
See https://dart.dev/tools/pub/publishing#important-files
4647
47-
- Deps need to be fleshed out
48+
API has theses URLs:
49+
is limited and only returns all versions of a package
50+
- feeds https://pub.dev/feed.atom
51+
- all packages, paginated: https://pub.dev/api/packages
52+
- one package, all version: https://pub.dev/api/packages/painter
53+
- one version: https://pub.dev/api/packages/painter/versions/0.3.1
4854
49-
API is limited and only returns all versions of a package
50-
See https://pub.dev/feed.atom
51-
See https://github.com/dart-lang/pub/blob/master/doc/repository-spec-v2.md
52-
See https://dart.dev/tools/pub/publishing#important-files
55+
See https://github.com/dart-lang/pub/blob/master/doc/repository-spec-v2.md
5356
"""
5457

5558

@@ -65,7 +68,7 @@ class PubspecPackage(models.Package):
6568

6669
@classmethod
6770
def recognize(cls, location):
68-
yield parse(location)
71+
yield parse_pub(location)
6972

7073
def repository_homepage_url(self, baseurl=default_web_baseurl):
7174
return f'{baseurl}/{self.name}/versions/{self.version}'
@@ -79,13 +82,13 @@ def repository_download_url(self, baseurl=default_download_baseurl):
7982
return f'{baseurl}/{self.name}/versions/{self.version}.tar.gz'
8083

8184
def api_data_url(self, baseurl=default_api_baseurl):
82-
return f'{baseurl}/{self.name}'
85+
return f'{baseurl}/{self.name}/versions/{self.version}'
8386

8487
def compute_normalized_license(self):
8588
return compute_normalized_license(self.declared_license)
8689

8790

88-
def compute_normalized_license(declared_license, location):
91+
def compute_normalized_license(declared_license, location=None):
8992
"""
9093
Return a normalized license expression string detected from a list of
9194
declared license items.
@@ -108,7 +111,7 @@ def compute_normalized_license(declared_license, location):
108111
return combine_expressions(detected_licenses)
109112

110113

111-
def parse(location):
114+
def parse_pub(location, compute_normalized_license=False):
112115
"""
113116
Return a PubspecPackage constructed from the pubspec.yaml file at ``location``
114117
or None.
@@ -117,7 +120,11 @@ def parse(location):
117120
return
118121
with open(location) as inp:
119122
package_data = saneyaml.load(inp.read())
120-
return build_package(package_data)
123+
124+
package = build_package(package_data)
125+
if package and compute_normalized_license:
126+
package.compute_normalized_license()
127+
return package
121128

122129

123130
def is_pubspec(location):
@@ -137,17 +144,26 @@ def collect_deps(data, dependency_field_name, is_runtime=True, is_optional=False
137144

138145
dependencies = data.get(dependency_field_name) or {}
139146
for name, version in dependencies.items():
140-
purl = PackageURL(type='pubspec', name=name)
147+
if isinstance(version, dict) and 'sdk' in version:
148+
# {'sdk': 'flutter'} type of deps....
149+
# which is a wart that we keep as a requiremnet
150+
version = ', '.join(': '.join([k, str(v)]) for k, v in version.items())
151+
141152
if version.replace('.', '').isdigit():
142-
# version is pinned exactly
143-
purl.version = version
153+
# version is pinned exactly if it is only made of dots and digits
154+
purl = PackageURL(type='pubspec', name=name, version=version)
155+
is_resolved = True
156+
else:
157+
purl = PackageURL(type='pubspec', name=name)
158+
is_resolved = False
144159

145160
yield models.DependentPackage(
146161
purl=purl.to_string(),
147162
requirement=version,
148163
scope=dependency_field_name,
149164
is_runtime=is_runtime,
150-
is_optional=is_runtime,
165+
is_optional=is_optional,
166+
is_resolved=is_resolved,
151167
)
152168

153169

@@ -161,20 +177,22 @@ def build_package(pubspec_data):
161177
homepage_url = pubspec_data.get('homepage')
162178
declared_license = pubspec_data.get('license')
163179
vcs_url = pubspec_data.get('repository')
164-
165-
# TODO: use these in extra data?
166-
# issue_tracker_url = pubspec_data.get('issue_tracker')
167-
# documentation_url = pubspec_data.get('documentation')
180+
download_url = pubspec_data.get('archive_url')
168181

169182
# Author and authors are deprecated
170-
# author = pubspec_data.get('author')
171-
# authors = pubspec_data.get('authors') or []
172-
173-
# TODO: this is some kind of SDK dep
174-
# environment = pubspec_data.get('environment')
175-
176-
# FIXME: what to do with these?
177-
# dependencies_overrides = pubspec_get('dependencies_overrides')
183+
authors = []
184+
author = pubspec_data.get('author')
185+
if author:
186+
authors.append(author)
187+
authors.extend(pubspec_data.get('authors') or [])
188+
189+
parties = []
190+
for auth in authors:
191+
parties.append(models.Party(
192+
type=models.party_person,
193+
role='author',
194+
name=auth
195+
))
178196

179197
package_dependencies = []
180198
dependencies = collect_deps(
@@ -193,14 +211,41 @@ def build_package(pubspec_data):
193211
)
194212
package_dependencies.extend(dev_dependencies)
195213

214+
env_dependencies = collect_deps(
215+
pubspec_data,
216+
'environment',
217+
is_runtime=True,
218+
is_optional=False,
219+
)
220+
package_dependencies.extend(env_dependencies)
221+
222+
extra_data = {}
223+
224+
def add_to_extra_if_present(_key):
225+
_value = pubspec_data.get(_key)
226+
if _value:
227+
extra_data[_key] = _value
228+
229+
add_to_extra_if_present('issue_tracker')
230+
add_to_extra_if_present('documentation')
231+
add_to_extra_if_present('dependencies_overrides')
232+
add_to_extra_if_present('executables')
233+
add_to_extra_if_present('publish_to')
234+
196235
package = PubspecPackage(
197236
name=name,
198237
version=version,
199238
vcs_url=vcs_url,
200239
description=description,
201240
declared_license=declared_license,
241+
parties=parties,
202242
homepage_url=homepage_url,
203243
dependencies=package_dependencies,
244+
extra_data=extra_data,
204245
)
246+
247+
if not download_url:
248+
package.download_url = package.repository_download_url()
249+
205250
return package
206251

0 commit comments

Comments
 (0)