From 37cccca4589a2da4aab53bcdede0bce510baeb9b Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 2 Mar 2023 18:44:46 -0600 Subject: [PATCH 1/9] Add start of pyhf debug --- src/pyhf/cli/cli.py | 18 ++++++++++++++++++ src/pyhf/utils.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/pyhf/cli/cli.py b/src/pyhf/cli/cli.py index a1a486fe54..734de8b6da 100644 --- a/src/pyhf/cli/cli.py +++ b/src/pyhf/cli/cli.py @@ -19,6 +19,13 @@ def _print_citation(ctx, param, value): ctx.exit() +def _debug(ctx, param, value): + if not value or ctx.resilient_parsing: + return + click.echo(utils.debug_os_info()) + ctx.exit() + + @click.group(context_settings=dict(help_option_names=['-h', '--help'])) @click.version_option(version=__version__) @click.option( @@ -31,6 +38,15 @@ def _print_citation(ctx, param, value): expose_value=False, is_eager=True, ) +@click.option( + "--debug", + help="Produce OS / environment information useful for filing a bug report", + default=False, + is_flag=True, + callback=_debug, + expose_value=False, + is_eager=True, +) def pyhf(): """Top-level CLI entrypoint.""" @@ -56,3 +72,5 @@ def pyhf(): pyhf.add_command(complete.cli) pyhf.add_command(contrib.cli) + +# pyhf.add_command(utils.cli) diff --git a/src/pyhf/utils.py b/src/pyhf/utils.py index 859f887913..caa4e87225 100644 --- a/src/pyhf/utils.py +++ b/src/pyhf/utils.py @@ -126,3 +126,40 @@ def citation(oneline=False): if oneline: data = ''.join(data.splitlines()) return data + + +# # Linux +# $ cat /etc/os-release +# NAME="Ubuntu" +# VERSION="20.04.2 LTS (Focal Fossa)" +# ID=ubuntu +# ID_LIKE=debian +# PRETTY_NAME="Ubuntu 20.04.2 LTS" +# VERSION_ID="20.04" +# HOME_URL="https://www.ubuntu.com/" +# SUPPORT_URL="https://help.ubuntu.com/" +# BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" +# PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" +# VERSION_CODENAME=focal +# UBUNTU_CODENAME=focal + + +def debug_os_info(): + """ + Produce OS / environment information useful for filing a bug report + + Example: + + >>> import pyhf + >>> pyhf.utils.debug_os_info() + + Returns: + os_info (:obj:`str`): The operating system and environment information + for the host machine. + """ + import os + from pyhf import __version__ + + pyhf_version = f"pyhf version: {__version__}\n" + summary = pyhf_version + "hi" + return summary From b330c4859b4b184817b7c9a53945424b567fb6e3 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 2 Mar 2023 23:27:48 -0600 Subject: [PATCH 2/9] Add debug info output --- src/pyhf/cli/cli.py | 2 +- src/pyhf/utils.py | 73 ++++++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/pyhf/cli/cli.py b/src/pyhf/cli/cli.py index 734de8b6da..c993f8a17f 100644 --- a/src/pyhf/cli/cli.py +++ b/src/pyhf/cli/cli.py @@ -22,7 +22,7 @@ def _print_citation(ctx, param, value): def _debug(ctx, param, value): if not value or ctx.resilient_parsing: return - click.echo(utils.debug_os_info()) + click.echo(utils.debug_info()) ctx.exit() diff --git a/src/pyhf/utils.py b/src/pyhf/utils.py index caa4e87225..b6abea9e63 100644 --- a/src/pyhf/utils.py +++ b/src/pyhf/utils.py @@ -1,10 +1,13 @@ -import json -import yaml -import click import hashlib +import json +import platform +import sys from gettext import gettext -import sys +import click +import yaml + +from pyhf import __version__ # importlib.resources.as_file wasn't added until Python 3.9 # c.f. https://docs.python.org/3.9/library/importlib.html#importlib.resources.as_file @@ -128,38 +131,54 @@ def citation(oneline=False): return data -# # Linux -# $ cat /etc/os-release -# NAME="Ubuntu" -# VERSION="20.04.2 LTS (Focal Fossa)" -# ID=ubuntu -# ID_LIKE=debian -# PRETTY_NAME="Ubuntu 20.04.2 LTS" -# VERSION_ID="20.04" -# HOME_URL="https://www.ubuntu.com/" -# SUPPORT_URL="https://help.ubuntu.com/" -# BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" -# PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" -# VERSION_CODENAME=focal -# UBUNTU_CODENAME=focal - - -def debug_os_info(): +def debug_info(): """ Produce OS / environment information useful for filing a bug report Example: >>> import pyhf - >>> pyhf.utils.debug_os_info() + >>> pyhf.utils.debug_info() Returns: os_info (:obj:`str`): The operating system and environment information for the host machine. """ - import os - from pyhf import __version__ - pyhf_version = f"pyhf version: {__version__}\n" - summary = pyhf_version + "hi" - return summary + os_version = "Cannot be determined" + if sys.platform == "linux": + try: + # platform.freedesktop_os_release added in Python 3.10 + # Remove when Python 3.9 support dropped + from platform import freedesktop_os_release + except ImportError: + # c.f. https://docs.python.org/3/library/platform.html#platform.freedesktop_os_release + from pathlib import Path + + def freedesktop_os_release(): + os_release_path = Path("/etc") / "os-release" + if os_release_path.exists(): + with open(os_release_path, encoding="utf8") as read_file: + os_release_file = read_file.read() + os_release_list = os_release_file.split("\n") + os_release_list.remove("") # Remove trailing line + return { + token.split("=")[0]: token.split("=")[1].replace('"', '') + for token in os_release_list + } + else: + raise OSError + + try: + os_release = freedesktop_os_release() + os_version = f"{os_release['NAME']} {os_release['VERSION']}" + except OSError: + os_version = "Cannot be determined" + elif sys.platform == "darwin": + os_version = f"macOS {platform.mac_ver()[0]}" + + os_info = f"* os version: {os_version}\n" + kernel_info = f"* kernel version: {platform.system()} {platform.release()} {platform.machine()}\n" + python_info = f"* python version: {platform.python_implementation()} {platform.python_version()} [{platform.python_compiler()}]\n" + pyhf_version = f"* pyhf version: {__version__}\n" + return os_info + kernel_info + python_info + pyhf_version From 49c89b53f5684c513c7296e525cb73ead0e7408f Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 2 Mar 2023 23:29:00 -0600 Subject: [PATCH 3/9] Add to __all__ --- src/pyhf/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pyhf/utils.py b/src/pyhf/utils.py index b6abea9e63..6f9f5f0c28 100644 --- a/src/pyhf/utils.py +++ b/src/pyhf/utils.py @@ -19,6 +19,7 @@ __all__ = [ "EqDelimStringParamType", "citation", + "debug_info", "digest", "options_from_eqdelimstring", ] From 4046d0d87e988704a9b0ec97e568213ae314abef Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Thu, 2 Mar 2023 23:53:07 -0600 Subject: [PATCH 4/9] Add utils CLI group --- src/pyhf/cli/cli.py | 26 +++++--------------------- src/pyhf/cli/utils.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 src/pyhf/cli/utils.py diff --git a/src/pyhf/cli/cli.py b/src/pyhf/cli/cli.py index c993f8a17f..007c29d78f 100644 --- a/src/pyhf/cli/cli.py +++ b/src/pyhf/cli/cli.py @@ -4,9 +4,9 @@ import click from pyhf import __version__ -from pyhf.cli import rootio, spec, infer, patchset, complete +from pyhf.cli import rootio, spec, infer, patchset, utils, complete from pyhf.contrib import cli as contrib -from pyhf import utils +from pyhf.utils import citation logging.basicConfig() log = logging.getLogger(__name__) @@ -15,14 +15,7 @@ def _print_citation(ctx, param, value): if not value or ctx.resilient_parsing: return - click.echo(utils.citation()) - ctx.exit() - - -def _debug(ctx, param, value): - if not value or ctx.resilient_parsing: - return - click.echo(utils.debug_info()) + click.echo(citation()) ctx.exit() @@ -38,15 +31,6 @@ def _debug(ctx, param, value): expose_value=False, is_eager=True, ) -@click.option( - "--debug", - help="Produce OS / environment information useful for filing a bug report", - default=False, - is_flag=True, - callback=_debug, - expose_value=False, - is_eager=True, -) def pyhf(): """Top-level CLI entrypoint.""" @@ -69,8 +53,8 @@ def pyhf(): pyhf.add_command(patchset.cli) +pyhf.add_command(utils.cli) + pyhf.add_command(complete.cli) pyhf.add_command(contrib.cli) - -# pyhf.add_command(utils.cli) diff --git a/src/pyhf/cli/utils.py b/src/pyhf/cli/utils.py new file mode 100644 index 0000000000..65bc9d56d2 --- /dev/null +++ b/src/pyhf/cli/utils.py @@ -0,0 +1,17 @@ +"""The pyhf utils CLI subcommand.""" +import logging + +import click +from pyhf import utils + +log = logging.getLogger(__name__) + + +@click.group(name="utils") +def cli(): + """Utils CLI group.""" + + +@cli.command() +def debug(): + click.echo(utils.debug_info()) From 52d8828d1dc11a0a1a1826eb64fdb4c853019aed Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Fri, 3 Mar 2023 00:39:07 -0600 Subject: [PATCH 5/9] Add output-file option --- src/pyhf/cli/utils.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/pyhf/cli/utils.py b/src/pyhf/cli/utils.py index 65bc9d56d2..1dbfa580d3 100644 --- a/src/pyhf/cli/utils.py +++ b/src/pyhf/cli/utils.py @@ -1,5 +1,6 @@ """The pyhf utils CLI subcommand.""" import logging +from pathlib import Path import click from pyhf import utils @@ -13,5 +14,21 @@ def cli(): @cli.command() -def debug(): - click.echo(utils.debug_info()) +@click.option( + "-o", + "--output-file", + help="The location of the output file. If not specified, prints to screen.", + default=None, +) +def debug(output_file): + debug_info = utils.debug_info() + + if output_file: + output_file = Path(output_file) + output_file.parent.mkdir(parents=True, exist_ok=True) + + with open(output_file, "w+", encoding="utf-8") as out_file: + out_file.write(debug_info) + log.debug(f"Written to {output_file}") + else: + click.echo(debug_info) From 787b535695d2292a2e3a36d43fc24fc3d6d5cf84 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Fri, 3 Mar 2023 00:44:53 -0600 Subject: [PATCH 6/9] Apply isort --- src/pyhf/cli/cli.py | 2 +- src/pyhf/cli/utils.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pyhf/cli/cli.py b/src/pyhf/cli/cli.py index 007c29d78f..88d4f07a1c 100644 --- a/src/pyhf/cli/cli.py +++ b/src/pyhf/cli/cli.py @@ -4,7 +4,7 @@ import click from pyhf import __version__ -from pyhf.cli import rootio, spec, infer, patchset, utils, complete +from pyhf.cli import complete, infer, patchset, rootio, spec, utils from pyhf.contrib import cli as contrib from pyhf.utils import citation diff --git a/src/pyhf/cli/utils.py b/src/pyhf/cli/utils.py index 1dbfa580d3..2b786a5795 100644 --- a/src/pyhf/cli/utils.py +++ b/src/pyhf/cli/utils.py @@ -3,6 +3,7 @@ from pathlib import Path import click + from pyhf import utils log = logging.getLogger(__name__) From 1729894b3700c0028858126dd04855435569fbb8 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Fri, 3 Mar 2023 01:10:00 -0600 Subject: [PATCH 7/9] Remove all trailing lines CentOS 7 has multiple --- src/pyhf/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pyhf/utils.py b/src/pyhf/utils.py index 6f9f5f0c28..a5f125a4ef 100644 --- a/src/pyhf/utils.py +++ b/src/pyhf/utils.py @@ -162,7 +162,8 @@ def freedesktop_os_release(): with open(os_release_path, encoding="utf8") as read_file: os_release_file = read_file.read() os_release_list = os_release_file.split("\n") - os_release_list.remove("") # Remove trailing line + # Remove all trailing lines + os_release_list = list(filter(("").__ne__, os_release_list)) return { token.split("=")[0]: token.split("=")[1].replace('"', '') for token in os_release_list From c9b5ed9fbee6ef4cc1c1f5f85facffd051b68e74 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Sun, 5 Mar 2023 23:57:22 -0600 Subject: [PATCH 8/9] rename to environment --- src/pyhf/cli/utils.py | 10 +++++----- src/pyhf/utils.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pyhf/cli/utils.py b/src/pyhf/cli/utils.py index 2b786a5795..7dc5a2a978 100644 --- a/src/pyhf/cli/utils.py +++ b/src/pyhf/cli/utils.py @@ -21,15 +21,15 @@ def cli(): help="The location of the output file. If not specified, prints to screen.", default=None, ) -def debug(output_file): - debug_info = utils.debug_info() +def environment(output_file): + environment_info = utils.environment_info() if output_file: output_file = Path(output_file) output_file.parent.mkdir(parents=True, exist_ok=True) with open(output_file, "w+", encoding="utf-8") as out_file: - out_file.write(debug_info) - log.debug(f"Written to {output_file}") + out_file.write(environment_info) + log.environment(f"Written to {output_file}") else: - click.echo(debug_info) + click.echo(environment_info) diff --git a/src/pyhf/utils.py b/src/pyhf/utils.py index a5f125a4ef..2e9f2ba8b3 100644 --- a/src/pyhf/utils.py +++ b/src/pyhf/utils.py @@ -19,7 +19,7 @@ __all__ = [ "EqDelimStringParamType", "citation", - "debug_info", + "environment_info", "digest", "options_from_eqdelimstring", ] @@ -132,14 +132,14 @@ def citation(oneline=False): return data -def debug_info(): +def environment_info(): """ Produce OS / environment information useful for filing a bug report Example: >>> import pyhf - >>> pyhf.utils.debug_info() + >>> pyhf.utils.environment_info() Returns: os_info (:obj:`str`): The operating system and environment information From daf692ed3933db7f8a9d067c5501e6a871bc35f2 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Mon, 6 Mar 2023 00:00:44 -0600 Subject: [PATCH 9/9] move position in __all__ --- src/pyhf/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyhf/utils.py b/src/pyhf/utils.py index 2e9f2ba8b3..49cf5147bc 100644 --- a/src/pyhf/utils.py +++ b/src/pyhf/utils.py @@ -19,8 +19,8 @@ __all__ = [ "EqDelimStringParamType", "citation", - "environment_info", "digest", + "environment_info", "options_from_eqdelimstring", ]