Skip to content

Commit 1277580

Browse files
Create PackageManifest classes for pubspec
Creates PackageManifest classes for pubspec manifests and overrides the methods for detection and PackageManifest creation. Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent a866432 commit 1277580

15 files changed

Lines changed: 3089 additions & 2052 deletions

src/packagedcode/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
build.MetadataBzl,
105105
msi.MsiInstallerPackage,
106106
windows.MicrosoftUpdateManifestPackage,
107-
pubspec.PubspecPackage,
107+
pubspec.PubspecYAML,
108+
pubspec.PubspecLock
108109
]
109110

110111
PACKAGE_MANIFESTS_BY_TYPE = {

src/packagedcode/pubspec.py

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,13 @@ def logger_debug(*args):
5858

5959

6060
@attr.s()
61-
class PubspecPackage(models.Package, models.PackageManifest):
62-
file_patterns = ('pubspec.yaml', 'pubspec.lock',)
63-
extensions = ('.yaml', '.lock',)
61+
class PubspecPackage(models.Package):
6462
default_type = 'pubspec'
6563
default_primary_language = 'dart'
6664
default_web_baseurl = 'https://pub.dev/packages'
6765
default_download_baseurl = 'https://pub.dartlang.org/packages'
6866
default_api_baseurl = 'https://pub.dev/api/packages'
6967

70-
@classmethod
71-
def recognize(cls, location):
72-
if is_pubspec_yaml(location):
73-
yield parse_pub(location)
74-
elif is_pubspec_lock(location):
75-
yield parse_lock(location)
76-
7768
def repository_homepage_url(self, baseurl=default_web_baseurl):
7869
return f'{baseurl}/{self.name}/versions/{self.version}'
7970

@@ -115,20 +106,33 @@ def compute_normalized_license(declared_license, location=None):
115106
return combine_expressions(detected_licenses)
116107

117108

118-
def parse_pub(location, compute_normalized_license=False):
119-
"""
120-
Return a PubspecPackage constructed from the pubspec.yaml file at ``location``
121-
or None.
122-
"""
123-
if not is_pubspec_yaml(location):
124-
return
125-
with open(location) as inp:
126-
package_data = saneyaml.load(inp.read())
109+
@attr.s()
110+
class PubspecYAML(PubspecPackage, models.PackageManifest):
127111

128-
package = build_package(package_data)
129-
if package and compute_normalized_license:
130-
package.compute_normalized_license()
131-
return package
112+
file_patterns = ('pubspec.yaml',)
113+
extensions = ('.yaml',)
114+
manifest_type = 'pubspecyaml'
115+
116+
@classmethod
117+
def is_manifest(cls, location):
118+
"""
119+
Return True if the file at ``location`` is likely a manifest of this type.
120+
"""
121+
return file_endswith(location, 'pubspec.yaml')
122+
123+
@classmethod
124+
def recognize(cls, location, compute_normalized_license=False):
125+
"""
126+
Yield one or more Package manifest objects given a file ``location`` pointing to a
127+
package archive, manifest or similar.
128+
"""
129+
with open(location) as inp:
130+
package_data = saneyaml.load(inp.read())
131+
132+
package = build_package(cls, package_data)
133+
if package and compute_normalized_license:
134+
package.compute_normalized_license()
135+
yield package
132136

133137

134138
def file_endswith(location, endswith):
@@ -138,26 +142,30 @@ def file_endswith(location, endswith):
138142
return filetype.is_file(location) and location.endswith(endswith)
139143

140144

141-
def is_pubspec_yaml(location):
142-
return file_endswith(location, 'pubspec.yaml')
143-
144-
145-
def is_pubspec_lock(location):
146-
return file_endswith(location, 'pubspec.lock')
145+
@attr.s()
146+
class PubspecLock(PubspecPackage, models.PackageManifest):
147147

148+
file_patterns = ('pubspec.lock',)
149+
extensions = ('.lock',)
150+
manifest_type = 'pubspeclock'
148151

149-
def parse_lock(location):
150-
"""
151-
Yield PubspecPackages dependencies constructed from the pubspec.lock file at
152-
``location``.
153-
"""
154-
if not is_pubspec_lock(location):
155-
return
152+
@classmethod
153+
def is_manifest(cls, location):
154+
"""
155+
Return True if the file at ``location`` is likely a manifest of this type.
156+
"""
157+
return file_endswith(location, 'pubspec.lock')
156158

157-
with open(location) as inp:
158-
locks_data = saneyaml.load(inp.read())
159+
@classmethod
160+
def recognize(cls, location):
161+
"""
162+
Yield one or more Package manifest objects given a file ``location`` pointing to a
163+
package archive, manifest or similar.
164+
"""
165+
with open(location) as inp:
166+
locks_data = saneyaml.load(inp.read())
159167

160-
return PubspecPackage(dependencies=list(collect_locks(locks_data)))
168+
yield cls(dependencies=list(collect_locks(locks_data)))
161169

162170

163171
def collect_locks(locks_data):
@@ -299,7 +307,7 @@ def build_dep(name, version, scope, is_runtime=True, is_optional=False):
299307
return dep
300308

301309

302-
def build_package(pubspec_data):
310+
def build_package(cls, pubspec_data):
303311
"""
304312
Return a package object from a package data mapping or None
305313
"""
@@ -364,7 +372,7 @@ def add_to_extra_if_present(_key):
364372
add_to_extra_if_present('executables')
365373
add_to_extra_if_present('publish_to')
366374

367-
package = PubspecPackage(
375+
package = cls(
368376
name=name,
369377
version=version,
370378
vcs_url=vcs_url,

tests/packagedcode/data/plugin/help.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,15 @@ Package: pods
301301

302302
--------------------------------------------
303303
Package: pubspec
304-
class: packagedcode.pubspec:PubspecPackage
305-
file_patterns: pubspec.yaml, pubspec.lock
306-
extensions: .yaml, .lock
304+
class: packagedcode.pubspec:PubspecYAML
305+
file_patterns: pubspec.yaml
306+
extensions: .yaml
307+
308+
--------------------------------------------
309+
Package: pubspec
310+
class: packagedcode.pubspec:PubspecLock
311+
file_patterns: pubspec.lock
312+
extensions: .lock
307313

308314
--------------------------------------------
309315
Package: pypi

0 commit comments

Comments
 (0)