From 637dff67623c903588505de63e817079c2e0428a Mon Sep 17 00:00:00 2001 From: Kent Pitman Date: Wed, 20 Sep 2023 14:00:05 -0400 Subject: [PATCH] Allow --brief, --debug, and --conda-prefix to be specified on command line of run-license-checker. --- dcicutils/license_utils.py | 16 +++++++++++++--- dcicutils/scripts/run_license_checker.py | 19 +++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/dcicutils/license_utils.py b/dcicutils/license_utils.py index d37e9a3f9..b14ffa82e 100644 --- a/dcicutils/license_utils.py +++ b/dcicutils/license_utils.py @@ -71,6 +71,16 @@ class LicenseOptions: VERBOSE = environ_bool("LICENSE_UTILS_VERBOSE", default=True) # Specific additional debugging output DEBUG = environ_bool("LICENSE_UTILS_DEBUG", default=False) + CONDA_PREFIX = os.environ.get("CONDA_LICENSE_CHECKER_PREFIX", os.environ.get("CONDA_PREFIX", "")) + + @classmethod + @contextlib.contextmanager + def selected_options(cls, verbose=VERBOSE, debug=DEBUG, conda_prefix=CONDA_PREFIX): + """ + Allows a script, for example, to specify overrides for these options dynamically. + """ + with local_attrs(cls, VERBOSE=verbose, DEBUG=debug, CONDA_PREFIX=conda_prefix): + yield class LicenseFramework: @@ -296,7 +306,7 @@ class CondaLicenseFramework(LicenseFramework): @classmethod def get_dependencies(cls): - prefix = os.environ.get("CONDA_LICENSE_CHECKER_PREFIX", os.environ.get("CONDA_PREFIX", "")) + prefix = LicenseOptions.CONDA_PREFIX result = [] filespec = os.path.join(prefix, "conda-meta/*.json") files = glob.glob(filespec) @@ -423,8 +433,8 @@ def parse_simple_license_file(cls, *, filename): lines = [] for i, line in enumerate(fp): line = line.strip(' \t\n\r') - if LicenseOptions.DEBUG: # pragma: no cover - this is just for debugging - PRINT(str(i).rjust(3), line) + # if LicenseOptions.DEBUG: # pragma: no cover - this is just for debugging + # PRINT(str(i).rjust(3), line) m = cls.COPYRIGHT_LINE.match(line) if line[:1].isupper() else None if not m: lines.append(line) diff --git a/dcicutils/scripts/run_license_checker.py b/dcicutils/scripts/run_license_checker.py index 33ff4b90c..f2324c0cf 100644 --- a/dcicutils/scripts/run_license_checker.py +++ b/dcicutils/scripts/run_license_checker.py @@ -2,7 +2,7 @@ from dcicutils.command_utils import script_catch_errors, ScriptFailure from dcicutils.lang_utils import there_are, conjoined_list -from dcicutils.license_utils import LicenseCheckerRegistry, LicenseChecker, LicenseCheckFailure +from dcicutils.license_utils import LicenseOptions, LicenseCheckerRegistry, LicenseChecker, LicenseCheckFailure from dcicutils.misc_utils import PRINT, get_error_message from typing import Optional, Type @@ -25,10 +25,17 @@ def main(): help=f"The name of a checker to run. " + there_are(ALL_CHECKER_NAMES, kind='available checker', show=True, joiner=conjoined_list, punctuate=True)) + parser.add_argument("--brief", '-b', default=False, action="store_true", + help="Requests brief output.") + parser.add_argument("--debug", '-q', default=False, action="store_true", + help="Requests additional debugging output.") + parser.add_argument("--conda-prefix", "--conda_prefix", "--cp", default=LicenseOptions.CONDA_PREFIX, + help=(f"Overrides the CONDA_PREFIX (default {LicenseOptions.CONDA_PREFIX!r}).")) + args = parser.parse_args() with script_catch_errors(): - run_license_checker(name=args.name) + run_license_checker(name=args.name, verbose=not args.brief, debug=args.debug, conda_prefix=args.conda_prefix) def show_help_for_choosing_license_checker(): @@ -52,7 +59,10 @@ def show_help_for_choosing_license_checker(): PRINT("") -def run_license_checker(name: Optional[str]): +def run_license_checker(name: Optional[str], + verbose=LicenseOptions.VERBOSE, + debug=LicenseOptions.DEBUG, + conda_prefix=LicenseOptions.CONDA_PREFIX): if name is None: show_help_for_choosing_license_checker() else: @@ -61,6 +71,7 @@ def run_license_checker(name: Optional[str]): except Exception as e: raise ScriptFailure(str(e)) try: - checker_class.validate() + with LicenseOptions.selected_options(verbose=verbose, debug=debug, conda_prefix=conda_prefix): + checker_class.validate() except LicenseCheckFailure as e: raise ScriptFailure(get_error_message(e))