From c8e8cae80d894b5e7971ae3bef1e6acebfccb201 Mon Sep 17 00:00:00 2001 From: William Deegan Date: Thu, 8 Jun 2023 10:07:25 -0700 Subject: Address mwichmann's comments on PR. pep8 naming, abstract base class Stats, quiet some linting warnings where the use is intentional --- SCons/Script/Main.py | 18 ++++++++++-------- SCons/Util/stats.py | 21 ++++++++++++--------- doc/man/scons.xml | 6 +++--- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/SCons/Script/Main.py b/SCons/Script/Main.py index 68a8183..861f57a 100644 --- a/SCons/Script/Main.py +++ b/SCons/Script/Main.py @@ -59,7 +59,7 @@ import SCons.Taskmaster import SCons.Util import SCons.Warnings import SCons.Script.Interactive -from SCons.Util.stats import COUNT_STATS, MEMORY_STATS, TIME_STATS, ENABLE_JSON, WriteJsonFile, JSON_OUTPUT_FILE +from SCons.Util.stats import COUNT_STATS, MEMORY_STATS, TIME_STATS, ENABLE_JSON, write_scons_stats_file, JSON_OUTPUT_FILE from SCons import __version__ as SConsVersion @@ -534,7 +534,8 @@ def DebugOptions(json=None): if not os.path.isdir(json_dir): os.makedirs(json_dir, exist_ok=True) # Now try to open file and see if you can.. - open(SCons.Util.stats.JSON_OUTPUT_FILE,'w') + with open(SCons.Util.stats.JSON_OUTPUT_FILE,'w') as js: + pass except OSError as e: raise SCons.Errors.UserError(f"Unable to create directory for JSON debug output file: {SCons.Util.stats.JSON_OUTPUT_FILE}") @@ -669,8 +670,10 @@ def _SConstruct_exists( return sfile return None + def _set_debug_values(options) -> None: - global print_memoizer, print_objects, print_stacktrace, print_time, print_action_timestamps + global print_memoizer, print_objects, print_stacktrace, print_time, \ + print_action_timestamps, ENABLE_JSON debug_values = options.debug @@ -720,9 +723,7 @@ def _set_debug_values(options) -> None: if "duplicate" in debug_values: SCons.Node.print_duplicate = True if "json" in debug_values: - SCons.Util.stats.ENABLE_JSON = True - if "json" in debug_values: - SCons.Util.stats.ENABLE_JSON = True + ENABLE_JSON = True def _create_path(plist): path = '.' @@ -1431,6 +1432,7 @@ def main() -> None: global OptionsParser global exit_status global first_command_start + global ENABLE_JSON # Check up front for a Python version we do not support. We # delay the check for deprecated Python versions until later, @@ -1524,8 +1526,8 @@ def main() -> None: TIME_STATS.total_times(total_time, sconscript_time, scons_time, ct) - if SCons.Util.stats.ENABLE_JSON: - WriteJsonFile() + if ENABLE_JSON: + write_scons_stats_file() sys.exit(exit_status) diff --git a/SCons/Util/stats.py b/SCons/Util/stats.py index d2f2574..3223c86 100644 --- a/SCons/Util/stats.py +++ b/SCons/Util/stats.py @@ -33,6 +33,8 @@ There are basically two types of stats. though it might be useful to query during a run. """ +from abc import ABC + import platform import json import sys @@ -40,21 +42,21 @@ from datetime import datetime import SCons.Debug -ALL_STATS = {} +all_stats = {} ENABLE_JSON = False JSON_OUTPUT_FILE = 'scons_stats.json' -def AddStatType(name, stat_object): +def add_stat_type(name, stat_object): """ Add a statistic type to the global collection """ - if name in ALL_STATS: + if name in all_stats: raise UserWarning(f'Stat type {name} already exists') else: - ALL_STATS[name] = stat_object + all_stats[name] = stat_object -class Stats: +class Stats(ABC): def __init__(self): self.stats = [] self.labels = [] @@ -151,14 +153,15 @@ MEMORY_STATS = MemStats() TIME_STATS = TimeStats() - -def WriteJsonFile(): +def write_scons_stats_file(): """ Actually write the JSON file with debug information. Depending which of : count, time, action-timestamps,memory their information will be written. """ - from SCons.Script import BUILD_TARGETS, COMMAND_LINE_TARGETS, ARGUMENTS, ARGLIST + # Have to import where used to avoid import loop + from SCons.Script import BUILD_TARGETS, COMMAND_LINE_TARGETS, ARGUMENTS, \ + ARGLIST # [import-outside-toplevel] # print(f"DUMPING JSON FILE: {JSON_OUTPUT_FILE}") json_structure = {} @@ -202,7 +205,7 @@ def WriteJsonFile(): with open(JSON_OUTPUT_FILE, 'w') as sf: - sf.write(json.dumps(json_structure, indent=4)) + sf.write(json.dumps(json_structure, indent=4)) # Local Variables: diff --git a/doc/man/scons.xml b/doc/man/scons.xml index dd70c21..80d16c0 100644 --- a/doc/man/scons.xml +++ b/doc/man/scons.xml @@ -932,10 +932,10 @@ $ scons --debug=includes foo.o json -Write info to a JSON file for any of the following debug options if they are enabled: memory, + Write info to a JSON file for any of the following debug options if they are enabled: memory, count, time, action-timestamps - The default output file is in the top level directory named scons_stats.json - The file name/path can be modified by using DebugOptions(json='path/to/file.json') + The default output file is scons_stats.json + The file name/path can be modified by using &f-link-DebugOptions; for example DebugOptions(json='path/to/file.json') $ scons --debug=memory,json foo.o -- cgit v0.12