Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamp to scan summary #925 #945

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 2 additions & 1 deletion tests/formattedcode/test_output_jsonlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def check_jsonlines_scan(expected_file, result_file, regen=False):
"""
result = _load_jsonlines_result(result_file)
remove_variable_data(result)

result[0]['header'].pop('scan_start', None)

if regen:
with open(expected_file, 'wb') as reg:
json.dump(result, reg, indent=2, separators=(',', ': '))
Expand Down
6 changes: 5 additions & 1 deletion tests/scancode/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,11 @@ def test_scan_quiet_to_stdout_only_echoes_json_results():

# outputs to file or stdout should be identical
result1_output = open(result_file).read()
assert result1_output == result_to_stdout.output
json_result1_output = json.loads(result1_output)
json_result_to_stdout = json.loads(result_to_stdout.output)
json_result_to_stdout.pop('scan_start', None)
json_result1_output.pop('scan_start', None)
assert json_result1_output == json_result_to_stdout


def test_scan_verbose_to_stdout_does_not_echo_ansi_escapes():
Expand Down