Skip to content

Commit

Permalink
[colic] Add copyright flag for extraction of copyright information
Browse files Browse the repository at this point in the history
Signed-off-by: inishchith <[email protected]>
  • Loading branch information
inishchith committed Aug 6, 2019
1 parent 54589cb commit a2de446
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
17 changes: 11 additions & 6 deletions graal/backends/core/analyzers/scancode.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,26 @@ def __init__(self, exec_path, cli=False):
_ = subprocess.check_output([exec_path]).decode("utf-8")

def __analyze_scancode(self, file_path):
"""Add information about license using scancode
"""Add information about license and copyright using scancode
:param file_path: file path (in case of scancode)
"""
result = {'licenses': []}
result = {
'licenses': [],
'copyrights': [],
}
try:
msg = subprocess.check_output([self.exec_path, '--json-pp', '-', '--license', file_path]).decode("utf-8")
msg = subprocess.check_output(
[self.exec_path, '--json-pp', '-', '--license', '--copyright', file_path]).decode("utf-8")
except subprocess.CalledProcessError as e:
raise GraalError(cause="Scancode failed at %s, %s" % (file_path, e.output.decode("utf-8")))
finally:
subprocess._cleanup()

licenses_raw = json.loads(msg)
if 'files' in licenses_raw:
result['licenses'] = licenses_raw['files'][0]['licenses']
scancode_raw = json.loads(msg)
if 'files' in scancode_raw:
result['licenses'] = scancode_raw['files'][0]['licenses']
result['copyrights'] = scancode_raw['files'][0]['copyrights']

return result

Expand Down
3 changes: 2 additions & 1 deletion graal/backends/core/colic.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def analyze(self, file_path):
:returns a dict containing the results of the analysis, like the one below
{
'licenses': [..]
'licenses': [..],
'copyrights': [..]
}
"""
if self.kind == SCANCODE_CLI:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_colic.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,14 @@ def test_analyze(self):
analysis = license_analyzer.analyze(file_path)

self.assertIn('licenses', analysis)
self.assertIn('copyrights', analysis)

file_paths = [os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE)]
license_analyzer = LicenseAnalyzer(SCANCODE_CLI_PATH, kind=SCANCODE_CLI)
analysis = license_analyzer.analyze(file_paths)

self.assertIn('licenses', analysis[0])
self.assertIn('copyrights', analysis[0])


class TestCoLicCommand(unittest.TestCase):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_scancode.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def test_analyze_scancode(self):
result = scancode.analyze(**kwargs)

self.assertIn('licenses', result)
self.assertIn('copyrights', result)

@unittest.mock.patch('subprocess.check_output')
def test_analyze_error(self, check_output_mock):
Expand Down Expand Up @@ -86,6 +87,7 @@ def test_analyze_scancode_cli(self):
result = scancode_cli.analyze(**kwargs)

self.assertIn('licenses', result[0])
self.assertIn('copyrights', result[0])

def test_analyze_error(self):
"""Test whether an exception is thrown in case of error"""
Expand Down

0 comments on commit a2de446

Please sign in to comment.