Skip to content

Commit 07e33f6

Browse files
committed
Create VirtualCodebase from multiple scans #1439
* This doesn't work yet Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 6544198 commit 07e33f6

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

src/scancode/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ def print_options(ctx, param, value):
320320

321321
@click.option('--from-json',
322322
is_flag=True,
323+
multiple=True,
323324
help='Load codebase from an existing JSON scan',
324325
help_group=CORE_GROUP, sort_order=25, cls=CommandLineOption)
325326

@@ -543,7 +544,7 @@ def echo_func(*_args, **_kwargs):
543544
elif len(input) == 1:
544545
# we received a single input path, so we treat this as a single path
545546
input = input[0] # NOQA
546-
else:
547+
elif not from_json:
547548
# we received a several input paths: we can handle this IFF they share
548549
# a common root directory and none is an absolute path
549550

src/scancode/resource.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,19 +1374,15 @@ def __init__(self, location,
13741374
self.resource_attributes = resource_attributes or OrderedDict()
13751375
self.resource_class = None
13761376
self.has_single_resource = False
1377+
self.location = location
13771378

13781379
scan_data = self._get_scan_data(location)
13791380
self._populate(scan_data)
13801381

1381-
def _get_scan_data(self, location):
1382+
def _get_scan_data_helper(self, location):
13821383
"""
1383-
Return scan data loaded from `location` that is either:
1384-
- a path string
1385-
- a JSON string
1386-
- a Python mapping
1384+
Return scan data loaded from `location`, which is a path string
13871385
"""
1388-
if isinstance(location, dict):
1389-
return location
13901386
try:
13911387
return json.loads(location, object_pairs_hook=OrderedDict)
13921388
except:
@@ -1398,6 +1394,24 @@ def _get_scan_data(self, location):
13981394
f, object_pairs_hook=OrderedDict, encoding='utf-8')
13991395
return scan_data
14001396

1397+
def _get_scan_data(self, location):
1398+
"""
1399+
Return scan data loaded from `location` that is either:
1400+
- a path string
1401+
- a JSON string
1402+
- a Python mapping
1403+
1404+
or `location` is a List that contains multiple paths to scans that are to be joined together.
1405+
"""
1406+
if isinstance(location, dict):
1407+
return location
1408+
if isinstance(location, (list, tuple,)):
1409+
scan_data = OrderedDict()
1410+
for loc in location:
1411+
scan_data.update(self._get_scan_data_helper(loc))
1412+
return scan_data
1413+
return self._get_scan_data_helper(location)
1414+
14011415
def _create_empty_resource_data(self):
14021416
"""
14031417
Return a dictionary of Resource fields and their default values.
@@ -1527,9 +1541,14 @@ def _populate(self, scan_data):
15271541
##########################################################
15281542
# Create root resource without setting root data just yet. If we run into the root data
15291543
# while we iterate through `resources_data`, we fill in the data then.
1530-
sample_resource_path = sample_resource_data['path']
1531-
sample_resource_path = sample_resource_path.strip('/')
1532-
root_path = sample_resource_path.split('/')[0]
1544+
1545+
multiple_input = isinstance(self.location, (list, tuple,)) and len(self.location) > 1
1546+
if multiple_input:
1547+
root_path = 'virtual_root'
1548+
else:
1549+
sample_resource_path = sample_resource_data['path']
1550+
sample_resource_path = sample_resource_path.strip('/')
1551+
root_path = sample_resource_path.split('/')[0]
15331552
root_name = root_path
15341553
root_is_file = False
15351554
root_data = self._create_empty_resource_data()
@@ -1541,6 +1560,8 @@ def _populate(self, scan_data):
15411560

15421561
for resource_data in resources_data:
15431562
path = resource_data.get('path')
1563+
if multiple_input:
1564+
path = os.path.join(root_path, path)
15441565
name = resource_data.get('name', None)
15451566
if not name:
15461567
name = file_name(path)

0 commit comments

Comments
 (0)