Skip to content

Commit b5a126a

Browse files
committed
Align CLI and summary code with packagedcode
* Adopt new handler style and top level packages Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 296176a commit b5a126a

37 files changed

+31901
-1869
lines changed

src/summarycode/classify.py

Lines changed: 9 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10-
11-
1210
from commoncode.datautils import Boolean
13-
from commoncode.fileset import get_matches
1411
from plugincode.pre_scan import PreScanPlugin
1512
from plugincode.pre_scan import pre_scan_impl
16-
from plugincode.post_scan import PostScanPlugin
17-
from plugincode.post_scan import post_scan_impl
1813
from commoncode.cliutils import PluggableCommandLineOption
1914
from commoncode.cliutils import PRE_SCAN_GROUP
2015

@@ -25,6 +20,7 @@
2520
# Tracing flag
2621
TRACE = False
2722

23+
2824
def logger_debug(*args):
2925
pass
3026

@@ -43,13 +39,11 @@ def emit(self, record):
4339
except Exception:
4440
self.handleError(record)
4541

46-
4742
logger = logging.getLogger(__name__)
4843
logger.handlers = [ClickHandler()]
4944
logger.propagate = False
5045
logger.setLevel(logging.DEBUG)
5146

52-
5347
def logger_debug(*args):
5448
return logger.debug(' '.join(isinstance(a, str) and a or repr(a) for a in args))
5549

@@ -138,97 +132,6 @@ def get_relative_path(root_path, path):
138132
return path[len(root_path):].lstrip('/')
139133

140134

141-
@post_scan_impl
142-
class PackageTopAndKeyFilesTagger(PostScanPlugin):
143-
"""
144-
Tag resources as key or top level based on Package-type specific settings.
145-
"""
146-
147-
sort_order = 0
148-
149-
def is_enabled(self, classify, **kwargs):
150-
return classify
151-
152-
def process_codebase(self, codebase, classify, **kwargs):
153-
"""
154-
Tag resource as key or top level files based on Package type-specific
155-
rules.
156-
"""
157-
if not classify:
158-
logger_debug('PackageTopAndKeyFilesTagger: return')
159-
return
160-
161-
from packagedcode import get_package_class
162-
163-
if codebase.has_single_resource:
164-
# What if we scanned a single file and we do not have a root proper?
165-
return
166-
167-
root_path = codebase.root.path
168-
169-
has_package_data = hasattr(codebase.root, 'package_data')
170-
if not has_package_data:
171-
# FIXME: this is not correct... we may still have cases where this
172-
# is wrong: e.g. a META-INF directory and we may not have a package
173-
return
174-
175-
176-
for resource in codebase.walk(topdown=True):
177-
package_data_all = resource.package_data or []
178-
179-
if not package_data_all:
180-
continue
181-
if not resource.has_children():
182-
continue
183-
184-
descendants = None
185-
186-
for package_data in package_data_all:
187-
package_class = get_package_class(package_data)
188-
extra_root_dirs = package_class.extra_root_dirs()
189-
extra_key_files = package_class.extra_key_files()
190-
if TRACE:
191-
logger_debug('PackageTopAndKeyFilesTagger: extra_root_dirs:', extra_root_dirs)
192-
logger_debug('PackageTopAndKeyFilesTagger: extra_key_files:', extra_key_files)
193-
194-
if not (extra_root_dirs or extra_key_files):
195-
# FIXME: this is not correct!
196-
# we may still have other files under the actual root.
197-
continue
198-
199-
if not descendants:
200-
descendants = {
201-
get_relative_path(root_path, r.path): r
202-
for r in resource.descendants(codebase)}
203-
204-
if TRACE:
205-
logger_debug('PackageTopAndKeyFilesTagger: descendants')
206-
for rpath, desc in descendants.items():
207-
logger_debug('rpath:', rpath, 'desc:', desc)
208-
209-
for rpath, desc in descendants.items():
210-
if extra_root_dirs and get_matches(rpath, extra_root_dirs):
211-
if TRACE:
212-
logger_debug('PackageTopAndKeyFilesTagger: get_matches for:', rpath, desc)
213-
desc.is_top_level = True
214-
if desc.is_file:
215-
set_classification_flags(desc)
216-
desc.save(codebase)
217-
218-
for child in desc.children(codebase):
219-
if TRACE:
220-
logger_debug('PackageTopAndKeyFilesTagger: set is_top_level for:', child)
221-
222-
child.is_top_level = True
223-
if child.is_file:
224-
set_classification_flags(child)
225-
child.save(codebase)
226-
227-
if extra_key_files and get_matches(rpath, extra_key_files):
228-
desc.is_key_file = True
229-
desc.save(codebase)
230-
231-
232135
LEGAL_STARTS_ENDS = (
233136
'copying',
234137
'copyright',
@@ -251,7 +154,6 @@ def process_codebase(self, codebase, classify, **kwargs):
251154
'patents',
252155
)
253156

254-
255157
_MANIFEST_ENDS = {
256158
'.about': 'ABOUT file',
257159
'/bower.json': 'bower',
@@ -298,10 +200,8 @@ def process_codebase(self, codebase, classify, **kwargs):
298200

299201
}
300202

301-
302203
MANIFEST_ENDS = tuple(_MANIFEST_ENDS)
303204

304-
305205
README_STARTS_ENDS = (
306206
'readme',
307207
)
@@ -323,17 +223,19 @@ def check_resource_name_start_and_end(resource, STARTS_ENDS):
323223

324224

325225
def set_classification_flags(resource,
326-
_LEGAL=LEGAL_STARTS_ENDS,
327-
_MANIF=MANIFEST_ENDS,
328-
_README=README_STARTS_ENDS):
226+
_LEGAL=LEGAL_STARTS_ENDS,
227+
_MANIF=MANIFEST_ENDS,
228+
_README=README_STARTS_ENDS,
229+
):
329230
"""
330231
Set classification flags on the `resource` Resource
331232
"""
332233
path = resource.path.lower()
333234

334235
resource.is_legal = is_legal = check_resource_name_start_and_end(resource, _LEGAL)
335236
resource.is_readme = is_readme = check_resource_name_start_and_end(resource, _README)
336-
resource.is_manifest = is_manifest = path.endswith(_MANIF)
337-
resource.is_key_file = (resource.is_top_level
338-
and (is_readme or is_legal or is_manifest))
237+
# FIXME: this will never be picked up as this is NOT available in a pre-scan plugin
238+
has_package_data = bool(getattr(resource, 'package_data', False))
239+
resource.is_manifest = is_manifest = path.endswith(_MANIF) or has_package_data
240+
resource.is_key_file = (resource.is_top_level and (is_readme or is_legal or is_manifest))
339241
return resource

src/summarycode/plugin_consolidate.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from commoncode.cliutils import PluggableCommandLineOption
1818
from commoncode.cliutils import POST_SCAN_GROUP
1919
from commoncode.text import python_safe_name
20-
from packagedcode import get_package_instance
21-
from packagedcode.build import BaseBuildManifestPackageData
20+
from packagedcode import models
2221
from packagedcode.utils import combine_expressions
2322
from plugincode.post_scan import PostScanPlugin
2423
from plugincode.post_scan import post_scan_impl
@@ -240,11 +239,11 @@ def get_consolidated_packages(codebase):
240239
"""
241240
for resource in codebase.walk(topdown=False):
242241
for package_data in resource.packages:
243-
package = get_package_instance(package_data)
242+
package = models.PackageData.from_dict(scan_data)(package_data)
244243
package_root = package.get_package_root(resource, codebase)
245244
package_root.extra_data['package_root'] = True
246245
package_root.save(codebase)
247-
is_build_file = isinstance(package, BaseBuildManifestPackageData)
246+
is_build_file = isinstance(package, )
248247
package_resources = list(package.get_package_resources(package_root, codebase))
249248
package_license_expression = package.license_expression
250249
package_copyright = package.copyright

0 commit comments

Comments
 (0)