Skip to content

Loading an existing scan with --from-json fails if the scan has no file info details #990

@pombredanne

Description

@pombredanne

$ ./scancode NOTICE -l --json-pp some.json
$ ./scancode some.json --from-json --output-csv to.csv

ERROR: failed to load codebase from scan: 'openibv1.json'
Traceback (most recent call last):
  File "/tmp/scancode-toolkit-master/src/scancode/cli.py", line 653, in scancode
    max_in_memory=max_in_memory
  File "/tmp/w421/scancode-toolkit-master/src/scancode/resource.py", line 1069, in __init__
    self._populate(plugin_attributes)
  File "/tmp/w421/scancode-toolkit-master/src/scancode/resource.py", line 1138, in _populate
    name, path, is_file, root_data = res_data(root)
  File "/tmp/w421/scancode-toolkit-master/src/scancode/resource.py", line 1122, in res_data
    name = file_data.pop('name')
  File "/usr/lib/python2.7/collections.py", line 143, in pop
    raise KeyError(key)
KeyError: u'name'

The patch is likely something like this:

diff --git a/src/scancode/resource.py b/src/scancode/resource.py
index 87c64ef..1c95ac4 100644
--- a/src/scancode/resource.py
+++ b/src/scancode/resource.py
@@ -1043,6 +1043,7 @@
 
 
 class VirtualCodebase(Codebase):
+
     def __init__(self, json_scan_location, plugin_attributes, temp_dir=scancode_temp_dir, max_in_memory=10000):
         """
         Initialize a new codebase loaded from `json_scan_location`, which
@@ -1117,13 +1118,16 @@
             name=b'ScannedResource', attrs=attributes, bases=(Resource,))
 
         def res_data(file_data):
-            name = file_data.pop('name')
             path = file_data.pop('path')
+
+            name = file_data.pop('name', None)
+            if not name:
+                name = file_name(path)
 
             file_data.pop('base_name', None)
             file_data.pop('extension', None)
 
-            file_type = file_data.pop('type')
+            file_type = file_data.pop('type', False)
             is_file = file_type == 'file'
 
             return name, path, is_file, file_data

and we need some tests

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions