diff --git a/src/formattedcode/output_json.py b/src/formattedcode/output_json.py index 23751d107de..95b3146a4b3 100644 --- a/src/formattedcode/output_json.py +++ b/src/formattedcode/output_json.py @@ -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) @@ -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), ]) diff --git a/src/formattedcode/output_jsonlines.py b/src/formattedcode/output_jsonlines.py index 38f15235fb8..36316a07f61 100644 --- a/src/formattedcode/output_jsonlines.py +++ b/src/formattedcode/output_jsonlines.py @@ -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) ])) diff --git a/src/scancode/cli.py b/src/scancode/cli.py index 06760d4fda9..4fa4443d58f 100644 --- a/src/scancode/cli.py +++ b/src/scancode/cli.py @@ -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 @@ -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()) diff --git a/src/scancode/cli_test_utils.py b/src/scancode/cli_test_utils.py index 11137b01aa3..24acb0b6d25 100644 --- a/src/scancode/cli_test_utils.py +++ b/src/scancode/cli_test_utils.py @@ -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=(',', ': ')) diff --git a/src/scancode/resource.py b/src/scancode/resource.py index 50e7450b919..fdb2450c683 100644 --- a/src/scancode/resource.py +++ b/src/scancode/resource.py @@ -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 = [] diff --git a/tests/formattedcode/test_output_json.py b/tests/formattedcode/test_output_json.py index e49e85023ce..36bd645fccd 100644 --- a/tests/formattedcode/test_output_json.py +++ b/tests/formattedcode/test_output_json.py @@ -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') @@ -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