Skip to content

Commit fbb33bd

Browse files
Modify file level attribute packages to package_manifests
See #2694 Signed-off-by: Ayan Sinha <ayansmahapatra@gmail.com>
1 parent 891d99e commit fbb33bd

8 files changed

Lines changed: 95 additions & 91 deletions

File tree

src/packagedcode/maven.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def get_package_root(cls, manifest_resource, codebase):
6666
if manifest_resource.name.endswith(('pom.xml', '.pom',)):
6767
# the root is either the parent or further up for poms stored under
6868
# a META-INF dir
69-
package_data = manifest_resource.packages
69+
package_data = manifest_resource.package_manifests
7070
if not package_data:
7171
return manifest_resource
7272
package_data = package_data[0]

src/packagedcode/plugin_package.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class PackageScanner(ScanPlugin):
5050
"""
5151

5252
resource_attributes = {}
53-
resource_attributes['packages'] = attr.ib(default=attr.Factory(list), repr=False)
53+
resource_attributes['package_manifests'] = attr.ib(default=attr.Factory(list), repr=False)
5454

5555
sort_order = 6
5656

@@ -78,8 +78,8 @@ def get_scanner(self, **kwargs):
7878
"""
7979
Return a scanner callable to scan a Resource for packages.
8080
"""
81-
from scancode.api import get_package_info
82-
return get_package_info
81+
from scancode.api import get_package_manifest_info
82+
return get_package_manifest_info
8383

8484
def process_codebase(self, codebase, **kwargs):
8585
"""
@@ -102,25 +102,25 @@ def set_packages_root(resource, codebase):
102102
if not resource.is_file:
103103
return
104104

105-
packages = resource.packages
106-
if not packages:
105+
package_manifests = resource.package_manifests
106+
if not package_manifests:
107107
return
108108
# NOTE: we are dealing with a single file therefore there should be only be
109109
# a single package detected. But some package manifests can document more
110110
# than one package at a time such as multiple arches/platforms for a gempsec
111111
# or multiple sub package (with "%package") in an RPM .spec file.
112112

113113
modified = False
114-
for package in packages:
115-
package_instance = get_package_instance(package)
114+
for package_manifest in package_manifests:
115+
package_instance = get_package_instance(package_manifest)
116116
package_root = package_instance.get_package_root(resource, codebase)
117117
if not package_root:
118118
# this can happen if we scan a single resource that is a package package
119119
continue
120120
# What if the target resource (e.g. a parent) is the root and we are in stripped root mode?
121121
if package_root.is_root and codebase.strip_root:
122122
continue
123-
package['root_path'] = package_root.path
123+
package_manifest['root_path'] = package_root.path
124124
modified = True
125125

126126
if modified:

src/packagedcode/recognize.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def logger_debug(*args):
4343
"""
4444

4545

46-
def recognize_packages(location):
46+
def recognize_package_manifests(location):
4747
"""
48-
Return a list of Package object if any packages were recognized for this
48+
Return a list of Package objects if any package_manifests were recognized for this
4949
`location`, or None if there were no Packages found. Raises Exceptions on errors.
5050
"""
5151

@@ -67,7 +67,7 @@ def recognize_packages(location):
6767
'fname:', filename, 'ext:', extension,
6868
)
6969

70-
recognized_packages = []
70+
recognized_package_manifests = []
7171
for package_type in PACKAGE_TYPES:
7272
# Note: default to True if there is nothing to match against
7373
metafiles = package_type.metafiles
@@ -86,8 +86,8 @@ def recognize_packages(location):
8686
'recognize_packages: recognized.license_expression:',
8787
recognized.license_expression,
8888
)
89-
recognized_packages.append(recognized)
90-
return recognized_packages
89+
recognized_package_manifests.append(recognized)
90+
return recognized_package_manifests
9191

9292
type_matched = False
9393
if package_type.filetypes:
@@ -124,19 +124,19 @@ def recognize_packages(location):
124124
if TRACE:
125125
logger_debug('recognize_packages: recognized', recognized)
126126

127-
recognized_packages.append(recognized)
127+
recognized_package_manifests.append(recognized)
128128

129129
except NotImplementedError:
130130
# build a plain package if recognize is not yet implemented
131131
recognized = package_type()
132132
if TRACE:
133133
logger_debug('recognize_packages: recognized', recognized)
134134

135-
recognized_packages.append(recognized)
135+
recognized_package_manifests.append(recognized)
136136

137137
if SCANCODE_DEBUG_PACKAGE_API:
138138
raise
139139

140-
return recognized_packages
140+
return recognized_package_manifests
141141

142142
if TRACE: logger_debug('recognize_packages: no match for type:', package_type)

src/scancode/api.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,19 +287,22 @@ def _licenses_data_from_match(
287287
SCANCODE_DEBUG_PACKAGE_API = os.environ.get('SCANCODE_DEBUG_PACKAGE_API', False)
288288

289289

290-
def get_package_info(location, **kwargs):
290+
def get_package_manifest_info(location, **kwargs):
291291
"""
292292
Return a mapping of package manifest information detected in the
293293
file at `location`.
294294
295295
Note that all exceptions are caught if there are any errors while parsing a
296296
package manifest.
297297
"""
298-
from packagedcode.recognize import recognize_packages
298+
from packagedcode.recognize import recognize_package_manifests
299299
try:
300-
recognized_packages = recognize_packages(location)
301-
if recognized_packages:
302-
return dict(packages=[package.to_dict() for package in recognized_packages])
300+
recognized_package_manifests = recognize_package_manifests(location)
301+
if recognized_package_manifests:
302+
return dict(package_manifests=[
303+
package_manifest.to_dict()
304+
for package_manifest in recognized_package_manifests
305+
])
303306
except Exception as e:
304307
if TRACE:
305308
logger.error('get_package_info: {}: Exception: {}'.format(location, e))
@@ -310,7 +313,7 @@ def get_package_info(location, **kwargs):
310313
# attention: we are swallowing ALL exceptions here!
311314
pass
312315

313-
return dict(packages=[])
316+
return dict(package_manifests=[])
314317

315318

316319
def get_file_info(location, **kwargs):

src/summarycode/classify.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,25 @@ def process_codebase(self, codebase, classify, **kwargs):
169169

170170
root_path = codebase.root.path
171171

172-
has_packages = hasattr(codebase.root, 'packages')
173-
if not has_packages:
172+
has_package_manifests = hasattr(codebase.root, 'package_manifests')
173+
if not has_package_manifests:
174174
# FIXME: this is not correct... we may still have cases where this
175175
# is wrong: e.g. a META-INF directory and we may not have a package
176176
return
177177

178178

179179
for resource in codebase.walk(topdown=True):
180-
packages_info = resource.packages or []
180+
package_manifests_info = resource.package_manifests or []
181181

182-
if not packages_info:
182+
if not package_manifests_info:
183183
continue
184184
if not resource.has_children():
185185
continue
186186

187187
descendants = None
188188

189-
for package_info in packages_info:
190-
package_class = get_package_class(package_info)
189+
for package_manifest_info in package_manifests_info:
190+
package_class = get_package_class(package_manifest_info)
191191
extra_root_dirs = package_class.extra_root_dirs()
192192
extra_key_files = package_class.extra_key_files()
193193
if TRACE:

src/summarycode/plugin_consolidate.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,21 @@ def to_dict(self, **kwargs):
9898

9999

100100
@attr.s
101-
class ConsolidatedPackage(object):
102-
package = attr.ib()
101+
class ConsolidatedPackageManifest(object):
102+
package_manifest = attr.ib()
103103
consolidation = attr.ib()
104104

105105
def to_dict(self, **kwargs):
106-
package = self.package.to_dict()
107-
package.update(self.consolidation.to_dict())
108-
return package
106+
package_manifest = self.package_manifest.to_dict()
107+
package_manifest.update(self.consolidation.to_dict())
108+
return package_manifest
109109

110110

111111
@post_scan_impl
112112
class Consolidator(PostScanPlugin):
113113
"""
114114
A ScanCode post-scan plugin to return consolidated components and consolidated
115-
packages for different types of codebase summarization.
115+
package_manifests for different types of codebase summarization.
116116
117117
A consolidated component is a group of Resources that have the same origin.
118118
Currently, a ConsolidatedComponent is created for each detected copyright holder
@@ -127,7 +127,7 @@ class Consolidator(PostScanPlugin):
127127
"""
128128
codebase_attributes = dict(
129129
consolidated_components=attr.ib(default=attr.Factory(list)),
130-
consolidated_packages=attr.ib(default=attr.Factory(list))
130+
consolidated_package_manifests=attr.ib(default=attr.Factory(list))
131131
)
132132

133133
resource_attributes = dict(
@@ -140,9 +140,10 @@ class Consolidator(PostScanPlugin):
140140
PluggableCommandLineOption(('--consolidate',),
141141
is_flag=True, default=False,
142142
help='Group resources by Packages or license and copyright holder and '
143-
'return those groupings as a list of consolidated packages and '
143+
'return those groupings as a list of consolidated package_manifests and '
144144
'a list of consolidated components. '
145-
'This requires the scan to have/be run with the copyright, license, and package options active',
145+
'This requires the scan to have/be run with the copyright, license, and '
146+
'package options active',
146147
help_group=POST_SCAN_GROUP
147148
)
148149
]
@@ -151,12 +152,12 @@ def is_enabled(self, consolidate, **kwargs):
151152
return consolidate
152153

153154
def process_codebase(self, codebase, **kwargs):
154-
# Collect ConsolidatedPackages and ConsolidatedComponents
155+
# Collect ConsolidatedPackageManifests and ConsolidatedComponents
155156
# TODO: Have a "catch-all" Component for the things that we haven't grouped
156157
consolidations = []
157158
root = codebase.root
158-
if hasattr(root, 'packages') and hasattr(root, 'copyrights') and hasattr(root, 'licenses'):
159-
consolidations.extend(get_consolidated_packages(codebase))
159+
if hasattr(root, 'package_manifests') and hasattr(root, 'copyrights') and hasattr(root, 'licenses'):
160+
consolidations.extend(get_consolidated_package_manifests(codebase))
160161
if hasattr(root, 'copyrights') and hasattr(root, 'licenses'):
161162
consolidations.extend(get_holders_consolidated_components(codebase))
162163

@@ -166,24 +167,24 @@ def process_codebase(self, codebase, **kwargs):
166167
# Sort consolidations by holders for consistent ordering before enumeration
167168
consolidations = sorted(consolidations, key=lambda c: '_'.join(h.key for h in c.consolidation.core_holders))
168169

169-
# Add ConsolidatedPackages and ConsolidatedComponents to top-level codebase attributes
170-
codebase.attributes.consolidated_packages = consolidated_packages = []
170+
# Add ConsolidatedPackageManifests and ConsolidatedComponents to top-level codebase attributes
171+
codebase.attributes.consolidated_package_manifests = consolidated_package_manifests = []
171172
codebase.attributes.consolidated_components = consolidated_components = []
172173
identifier_counts = Counter()
173174
for index, c in enumerate(consolidations, start=1):
174175
# Skip consolidation if it does not have any Files
175176
if c.consolidation.files_count == 0:
176177
continue
177-
if isinstance(c, ConsolidatedPackage):
178+
if isinstance(c, ConsolidatedPackageManifest):
178179
# We use the purl as the identifier for ConsolidatedPackages
179-
purl = c.package.purl
180+
purl = c.package_manifest.purl
180181
identifier_counts[purl] += 1
181182
identifier = python_safe_name('{}_{}'.format(purl, identifier_counts[purl]))
182183
c.consolidation.identifier = identifier
183184
for resource in c.consolidation.resources:
184185
resource.consolidated_to.append(identifier)
185186
resource.save(codebase)
186-
consolidated_packages.append(c.to_dict())
187+
consolidated_package_manifests.append(c.to_dict())
187188
elif isinstance(c, ConsolidatedComponent):
188189
consolidation_identifier = c.consolidation.identifier
189190
if consolidation_identifier:
@@ -218,20 +219,20 @@ def process_codebase(self, codebase, **kwargs):
218219
resource.save(codebase)
219220

220221

221-
def get_consolidated_packages(codebase):
222+
def get_consolidated_package_manifests(codebase):
222223
"""
223-
Yield a ConsolidatedPackage for each detected package in the codebase
224+
Yield a ConsolidatedPackageManifest for each detected package_manifest in the codebase
224225
"""
225226
for resource in codebase.walk(topdown=False):
226-
for package_data in resource.packages:
227-
package = get_package_instance(package_data)
228-
package_root = package.get_package_root(resource, codebase)
227+
for package_manifest_data in resource.package_manifests:
228+
package_manifest = get_package_instance(package_manifest_data)
229+
package_root = package_manifest.get_package_root(resource, codebase)
229230
package_root.extra_data['package_root'] = True
230231
package_root.save(codebase)
231-
is_build_file = isinstance(package, BaseBuildManifestPackage)
232-
package_resources = list(package.get_package_resources(package_root, codebase))
233-
package_license_expression = package.license_expression
234-
package_copyright = package.copyright
232+
is_build_file = isinstance(package_manifest, BaseBuildManifestPackage)
233+
package_resources = list(package_manifest.get_package_resources(package_root, codebase))
234+
package_license_expression = package_manifest.license_expression
235+
package_copyright = package_manifest.copyright
235236

236237
package_holders = []
237238
if package_copyright:
@@ -273,14 +274,14 @@ def get_consolidated_packages(codebase):
273274
resources=package_resources,
274275
)
275276
if is_build_file:
276-
c.identifier = package.name
277+
c.identifier = package_manifest.name
277278
yield ConsolidatedComponent(
278279
type='build',
279280
consolidation=c
280281
)
281282
else:
282-
yield ConsolidatedPackage(
283-
package=package,
283+
yield ConsolidatedPackageManifest(
284+
package_manifest=package_manifest,
284285
consolidation=c
285286
)
286287

src/summarycode/score.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,31 +166,31 @@ def compute_license_score(codebase):
166166

167167
def get_declared_license_keys(codebase):
168168
"""
169-
Return a list of declared license keys found in packages and key files.
169+
Return a list of declared license keys found in package_manifests and key files.
170170
"""
171171
return (
172172
get_declared_license_keys_in_key_files(codebase) +
173-
get_declared_license_keys_in_packages(codebase)
173+
get_declared_license_keys_in_package_manifests(codebase)
174174
)
175175

176176

177-
def get_declared_license_keys_in_packages(codebase):
177+
def get_declared_license_keys_in_package_manifests(codebase):
178178
"""
179-
Return a list of declared license keys found in packages.
179+
Return a list of declared license keys found in package_manifests.
180180
181181
A package manifest (such as Maven POM file or an npm package.json file)
182182
contains structured declared license information. This is further normalized
183183
as a license_expression. We extract the list of licenses from the normalized
184184
license expressions.
185185
"""
186-
packages = chain.from_iterable(
187-
getattr(res, 'packages', []) or []
186+
package_manifests = chain.from_iterable(
187+
getattr(res, 'package_manifests', []) or []
188188
for res in codebase.walk(topdown=True))
189189

190190
licensing = Licensing()
191191
detected_good_licenses = []
192-
for package in packages:
193-
expression = package.get('license_expression')
192+
for package_manifest in package_manifests:
193+
expression = package_manifest.get('license_expression')
194194
if expression:
195195
exp = licensing.parse(
196196
expression, validate=False, strict=False, simple=True)

0 commit comments

Comments
 (0)