Skip to content

Commit

Permalink
Add timestamp to scan summary and json output file #925
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Mittal <[email protected]>
  • Loading branch information
techytushar committed Feb 28, 2018
1 parent e634022 commit da96172
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/formattedcode/output_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def process_codebase(self, codebase, output_json, files_count,
files_count=files_count,
scancode_version=scancode_version,
scancode_notice=scancode_notice,
scan_start = codebase.scan_start,
pretty_options=pretty_options,
pretty=False)

Expand Down Expand Up @@ -92,18 +93,21 @@ def process_codebase(self, codebase, output_json_pp, files_count,
files_count=files_count,
scancode_version=scancode_version,
scancode_notice=scancode_notice,
scan_start = codebase.scan_start,
pretty_options=pretty_options,
pretty=True)


def write_json(results, output_file, files_count,
scancode_version, scancode_notice,
pretty_options, pretty=False):
scan_start, pretty_options,
pretty=False):

scan = OrderedDict([
('scancode_notice', scancode_notice),
('scancode_version', scancode_version),
('scancode_options', pretty_options),
('scan_start', scan_start),
('files_count', files_count),
('files', results),
])
Expand Down
1 change: 1 addition & 0 deletions src/formattedcode/output_jsonlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def process_codebase(self, codebase, output_json_lines, files_count,
('scancode_notice', scancode_notice),
('scancode_version', scancode_version),
('scancode_options', pretty_options),
('scan_start', codebase.scan_start),
('files_count', files_count)
]))

Expand Down
6 changes: 5 additions & 1 deletion src/scancode/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ def scancode(ctx, input, # NOQA
# TODO: this is weird: may be the timings should NOt be stored on the
# codebase, since they exist in abstract of it??
codebase.timings.update(setup_timings)

codebase.scan_start = scan_start
codebase.timings['inventory'] = time() - inventory_start
files_count, dirs_count, size_count = codebase.compute_counts()
codebase.summary['initial:files_count'] = files_count
Expand Down Expand Up @@ -1202,6 +1202,10 @@ def display_summary(codebase, scan_names, processes, verbose):
'%(final_size_count)s' % locals())

echo_stderr('Timings:')

timestamp = codebase.scan_start
echo_stderr(' scan_start: %(timestamp)s'% locals())

for name, value, in codebase.timings.items():
if value > 0.1:
echo_stderr(' %(name)s: %(value).2fs' % locals())
Expand Down
2 changes: 1 addition & 1 deletion src/scancode/cli_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def check_json_scan(expected_file, result_file, regen=False,
convenient for updating tests expectations. But use with caution.
"""
scan_results = load_json_result(result_file, strip_dates, clean_errs)

scan_results.pop('scan_start',None)
if regen:
with open(expected_file, 'wb') as reg:
json.dump(scan_results, reg, indent=2, separators=(',', ': '))
Expand Down
3 changes: 3 additions & 0 deletions src/scancode/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ def __init__(self, location, resource_class=None,
# This is populated automatically.
self.timings = OrderedDict()

# stores the timestamp when the scan started as string
self.scan_start = None

# list of errors from collecting the codebase details (such as
# unreadable file, etc).
self.errors = []
Expand Down
8 changes: 8 additions & 0 deletions tests/formattedcode/test_output_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from commoncode.testcase import FileDrivenTesting
from scancode.cli_test_utils import check_json_scan
from scancode.cli_test_utils import run_scan_click
from scancode.cli_test_utils import load_json_result

test_env = FileDrivenTesting()
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
Expand Down Expand Up @@ -71,3 +72,10 @@ def test_scan_output_does_not_truncate_copyright_with_json_to_stdout():
run_scan_click(args)
expected = test_env.get_test_loc('json/tree/expected.json')
check_json_scan(test_env.get_test_loc(expected), result_file, strip_dates=True)

def test_scan_output_for_timestamp():
test_dir = test_env.get_test_loc('json/simple')
result_file = test_env.get_temp_file('json')
run_scan_click(['-clip', test_dir, '--json', result_file])
result_json = load_json_result(result_file)
assert "scan_start" in result_json

0 comments on commit da96172

Please sign in to comment.