Skip to content

Commit

Permalink
Allow --brief, --debug, and --conda-prefix to be specified on command…
Browse files Browse the repository at this point in the history
… line of run-license-checker.
  • Loading branch information
netsettler committed Sep 20, 2023
1 parent 7858fa6 commit 637dff6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
16 changes: 13 additions & 3 deletions dcicutils/license_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 15 additions & 4 deletions dcicutils/scripts/run_license_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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():
Expand All @@ -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:
Expand All @@ -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))

0 comments on commit 637dff6

Please sign in to comment.