diff --git a/.evergreen/mongodl.py b/.evergreen/mongodl.py index e3185150..ef72c6c9 100755 --- a/.evergreen/mongodl.py +++ b/.evergreen/mongodl.py @@ -18,6 +18,7 @@ import enum import hashlib import json +import logging import os import platform import re @@ -47,6 +48,9 @@ cast, ) +LOGGER = logging.getLogger(__name__) +logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s") + SSL_CONTEXT = ssl.create_default_context() try: import certifi @@ -483,9 +487,9 @@ def _import_json_data(self, data: "Any") -> None: data=json.dumps(data), ) if missing: - print("Missing targets in DISTRO_ID_TO_TARGET:", file=sys.stderr) + LOGGER.error("Missing targets in DISTRO_ID_TO_TARGET:") for item in missing: - print(f" - {item}", file=sys.stderr) + LOGGER.error(f" - {item}") if os.environ.get("VALIDATE_DISTROS") == "1": sys.exit(1) @@ -815,9 +819,7 @@ def _dl_component( no_download: bool, latest_build_branch: "str|None", ) -> ExpandResult: - print( - f"Download {component} {version}-{edition} for {target}-{arch}", file=sys.stderr - ) + LOGGER.info(f"Download {component} {version}-{edition} for {target}-{arch}") if version == "latest-build": dl_url = _latest_build_url( target, arch, edition, component, latest_build_branch @@ -841,6 +843,7 @@ def _dl_component( ) if no_download: + # This must go to stdout to be consumed by the calling program. print(dl_url) return None cached = cache.download_file(dl_url).path @@ -893,8 +896,8 @@ def _expand_archive( Expand the archive members from 'ar' into 'dest'. If 'pattern' is not-None, only extracts members that match the pattern. """ - print(f"Extract from: [{ar.name}]", file=sys.stderr) - print(f" into: [{dest}]", file=sys.stderr) + LOGGER.debug(f"Extract from: [{ar.name}]") + LOGGER.debug(f" into: [{dest}]") if ar.suffix == ".zip": n_extracted = _expand_zip(ar, dest, pattern, strip_components, test=test) elif ar.suffix == ".tgz": @@ -904,33 +907,27 @@ def _expand_archive( verb = "would be" if test else "were" if n_extracted == 0: if pattern and strip_components: - print( + LOGGER.warning( f"NOTE: No files {verb} extracted. Likely all files {verb} " - f'excluded by "--only={pattern}" and/or "--strip-components={strip_components}"', - file=sys.stderr, + f'excluded by "--only={pattern}" and/or "--strip-components={strip_components}"' ) elif pattern: - print( + LOGGER.warning( f"NOTE: No files {verb} extracted. Likely all files {verb} " - f'excluded by the "--only={pattern}" filter', - file=sys.stderr, + f'excluded by the "--only={pattern}" filter' ) elif strip_components: - print( + LOGGER.warning( f"NOTE: No files {verb} extracted. Likely all files {verb} " - f'excluded by "--strip-components={strip_components}"', - file=sys.stderr, + f'excluded by "--strip-components={strip_components}"' ) else: - print(f"NOTE: No files {verb} extracted. Empty archive?", file=sys.stderr) + LOGGER.warning(f"NOTE: No files {verb} extracted. Empty archive?") return ExpandResult.Empty if n_extracted == 1: - print( - "One file {v} extracted".format(v="would be" if test else "was"), - file=sys.stderr, - ) + LOGGER.info(f"One file {verb} extracted") return ExpandResult.Okay - print(f"{n_extracted} files {verb} extracted", file=sys.stderr) + LOGGER.info(f"{n_extracted} files {verb} extracted") return ExpandResult.Okay @@ -990,18 +987,18 @@ def _maybe_extract_member( :return: Zero if the file was excluded by filters, one otherwise. """ relpath = PurePath(relpath) - print(" | {:-<65} |".format(str(relpath) + " "), end="", file=sys.stderr) + LOGGER.debug(" | {:-<65} |".format(str(relpath) + " ")) if len(relpath.parts) <= strip: # Not enough path components - print(" (Excluded by --strip-components)", file=sys.stderr) + LOGGER.debug(" (Excluded by --strip-components)") return 0 if not _test_pattern(relpath, PurePath(pattern) if pattern else None): # Doesn't match our pattern - print(" (excluded by pattern)", file=sys.stderr) + LOGGER.debug(" (excluded by pattern)") return 0 stripped = _pathjoin(relpath.parts[strip:]) dest = Path(out) / stripped - print(f"\n -> [{dest}]", file=sys.stderr) + LOGGER.debug(f"-> [{dest}]") if test: # We are running in test-only mode: Do not do anything return 1 @@ -1026,6 +1023,12 @@ def main(argv=None): default=default_cache_dir(), help="Directory where download caches and metadata will be stored", ) + parser.add_argument( + "--verbose", "-v", action="store_true", help="Whether to log at the DEBUG level" + ) + parser.add_argument( + "--quiet", "-q", action="store_true", help="Whether to log at the WARNING level" + ) grp = parser.add_argument_group("List arguments") grp.add_argument( "--list", @@ -1139,6 +1142,11 @@ def main(argv=None): if arch == "auto": arch = infer_arch() + if args.verbose: + LOGGER.setLevel(logging.DEBUG) + elif args.quiet: + LOGGER.setLevel(logging.WARNING) + if args.list: _print_list(cache.db, version, target, arch, args.edition, args.component) return diff --git a/.evergreen/mongosh_dl.py b/.evergreen/mongosh_dl.py index 54deeeb4..0d53d6be 100755 --- a/.evergreen/mongosh_dl.py +++ b/.evergreen/mongosh_dl.py @@ -7,6 +7,7 @@ import argparse import json +import logging import os import re import shlex @@ -16,8 +17,12 @@ import urllib.request from pathlib import Path +LOGGER = logging.getLogger(__name__) +logging.basicConfig(level=logging.INFO, format="%(levelname)-8s %(message)s") + HERE = Path(__file__).absolute().parent sys.path.insert(0, str(HERE)) +from mongodl import LOGGER as DL_LOGGER from mongodl import SSL_CONTEXT, ExpandResult, _expand_archive, infer_arch @@ -57,7 +62,7 @@ def _get_latest_version_git(): ) for line in reversed(output.decode("utf-8").splitlines()): if re.match("^v\d+\.\d+\.\d+$", line): - print("Found version", line, file=sys.stderr) + LOGGER.debug("Found version %s", line) return line.replace("v", "").strip() @@ -71,7 +76,7 @@ def _download( test: bool, no_download: bool, ) -> int: - print(f"Download {version} mongosh for {target}-{arch}", file=sys.stderr) + LOGGER.info(f"Download {version} mongosh for {target}-{arch}") if version == "latest": version = _get_latest_version() if arch == "x86_64": @@ -89,6 +94,7 @@ def _download( else: suffix = ".zip" dl_url = f"https://downloads.mongodb.com/compass/mongosh-{version}-{target}-{arch}{suffix}" + # This must go to stdout to be consumed by the calling program. print(dl_url) if no_download: @@ -114,6 +120,12 @@ def main(argv=None): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) + parser.add_argument( + "--verbose", "-v", action="store_true", help="Whether to log at the DEBUG level" + ) + parser.add_argument( + "--quiet", "-q", action="store_true", help="Whether to log at the WARNING level" + ) dl_grp = parser.add_argument_group( "Download arguments", description="Select what to download and extract. " @@ -187,6 +199,12 @@ def main(argv=None): arch = infer_arch() out = args.out or Path.cwd() out = out.absolute() + if args.verbose: + LOGGER.setLevel(logging.DEBUG) + DL_LOGGER.setLevel(logging.DEBUG) + elif args.quiet: + LOGGER.setLevel(logging.WARNING) + DL_LOGGER.setLevel(logging.WARNING) result = _download( out, version=args.version,