From 34c740bd4c4d1c3b311a7ee23bb9857e8bb3727d Mon Sep 17 00:00:00 2001 From: Ludovico Bianchi Date: Wed, 15 May 2024 08:43:43 -0500 Subject: [PATCH 1/6] Add CLI options --- getinfo.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/getinfo.py b/getinfo.py index 6633f2c..4b5fca2 100644 --- a/getinfo.py +++ b/getinfo.py @@ -1,3 +1,4 @@ +import argparse import collections import datetime import json @@ -663,6 +664,25 @@ def main(): root = Module(target=__name__, prefix="getinfo_") populate(root, DATA) _display(DATA) +def _build_cli_parser() -> argparse.ArgumentParser: + p = argparse.ArgumentParser(prog=f"getinfo {__version__}") + p.add_argument( + "-o", + "--output", + dest="output", + help=""" + Where the output will be written to. Can be a path to a file or `stdout`. + If this flag is not specified (default), an autogenerated filename will be used. + """, + default="", + ) + p.add_argument( + "-v", + "--version", + action="version", + version=__version__, + ) + return p if __name__ == '__main__': From 9d530c319eb3b69c5d2b0386d6769576e95a3d93 Mon Sep 17 00:00:00 2001 From: Ludovico Bianchi Date: Wed, 15 May 2024 08:44:13 -0500 Subject: [PATCH 2/6] Remove deprecated function --- getinfo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/getinfo.py b/getinfo.py index 4b5fca2..9a3b705 100644 --- a/getinfo.py +++ b/getinfo.py @@ -622,7 +622,6 @@ def getinfo_watertap(): def getinfo_platform(): yield from ( locale.getlocale, - locale.getpreferredencoding, platform.platform, platform.system, platform.machine, From 074414a44ef33c2661ce71fa04a721b7a9c10473 Mon Sep 17 00:00:00 2001 From: Ludovico Bianchi Date: Wed, 15 May 2024 08:44:35 -0500 Subject: [PATCH 3/6] Use leading v for version --- getinfo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getinfo.py b/getinfo.py index 9a3b705..d9f9509 100644 --- a/getinfo.py +++ b/getinfo.py @@ -28,7 +28,7 @@ ) -__version__ = "0.24.1.25" +__version__ = "v0.24.1.25" class _Node: From a590f9518dc528685a495e2101bd8765b52c57c0 Mon Sep 17 00:00:00 2001 From: Ludovico Bianchi Date: Wed, 15 May 2024 08:45:24 -0500 Subject: [PATCH 4/6] Implement output saving functionality --- README.md | 2 +- getinfo.py | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4d0761c..47fbfc3 100644 --- a/README.md +++ b/README.md @@ -89,5 +89,5 @@ For the sake of example, let's consider the scenario in which you encounter an i ![](docs/images/copy-path.png) 3. In the same terminal/console window where the failure occurred, type `python ` followed by the path to the downloaded `getinfo.py` file, e.g. `python "C:\Users\user\Downloads\getinfo.py"`. If the terminal/console window isn't open anymore, open a new one trying to reproduce the conditions when the failure occurred (e.g. same working directory, Conda environment, etc) ![](docs/images/run.png) -4. Copy the output and paste it where needed, e.g. a GitHub issue or a Slack support message. In this particular example, inspecting the **getinfo** output would help an experienced developer spot an incompatibility between the installed versions of `Pyomo` and `idaes-pse` (the `Pyomo` version is too old) +4. After collecting various types of information, **getinfo** will save the collected data in JSON format. By default, the output is saved to a JSON file in the current working directory (the name is displayed in the logging), which can be opened with any text editor and/or attached to e.g. a GitHub issue or a Slack support message. In this particular example, inspecting the **getinfo** output would help an experienced developer spot an incompatibility between the installed versions of `Pyomo` and `idaes-pse` (the `Pyomo` version is too old) ![](docs/images/output.png) diff --git a/getinfo.py b/getinfo.py index d9f9509..ad95d5f 100644 --- a/getinfo.py +++ b/getinfo.py @@ -7,6 +7,7 @@ import importlib.metadata import inspect import locale +import logging import os import platform import shutil @@ -30,6 +31,8 @@ __version__ = "v0.24.1.25" +_log = logging.getLogger("getinfo") + class _Node: def __init__(self, parent=None, id=None): @@ -261,6 +264,7 @@ def _for_collector(obj: Collector, data: dict=None, parent=None): data = {} key = get_key(obj) subdata = data[key] = {} + _log.info("Collecting from: %s", key) for collected in obj.collect(): populate(collected, data=subdata, parent=parent) @@ -649,20 +653,41 @@ def _to_jsonable(obj: object): return str(obj) -def _display(obj): - return print( - json.dumps(obj, indent=4, default=_to_jsonable) - ) +def _get_default_file_name(prefix: str = ".getinfo-output") -> str: + ts = datetime.datetime.utcnow() + return f"{prefix}-{ts.isoformat()}" +# created in the global scope to facilitate debugging DATA = {} -def main(): +def main(args=None): + + parser = _build_cli_parser() + cli_opts = parser.parse_args(args) + logging.basicConfig(level=logging.INFO) + _log.info(parser.prog) + _log.debug(cli_opts) + _log.info("Start collecting information") root = Module(target=__name__, prefix="getinfo_") populate(root, DATA) - _display(DATA) + _log.info("Collection complete") + + output_str = json.dumps(DATA, indent=4, default=_to_jsonable) + n_lines = output_str.count("\n") + output_dest = cli_opts.output + if output_dest in {"stdout"}: + _log.info("output (%d lines) will be written to stdout", n_lines) + print(output_str) + else: + path = Path(output_dest) if output_dest else Path(_get_default_file_name()).with_suffix(".json") + path = path.resolve() + written = path.write_text(output_str) + _log.info("output (%d lines) has been written to %s (%d B)", n_lines, os.fspath(path), written) + + def _build_cli_parser() -> argparse.ArgumentParser: p = argparse.ArgumentParser(prog=f"getinfo {__version__}") p.add_argument( From ebb753305fdceaa1dbb51f5e83ffd37c06ac6f70 Mon Sep 17 00:00:00 2001 From: Ludovico Bianchi Date: Wed, 15 May 2024 08:45:43 -0500 Subject: [PATCH 5/6] Update CI to test existing CLI options --- .github/workflows/test.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b350108..e360c10 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,4 +39,9 @@ jobs: with: python-version: ${{ matrix.python-version }} - if: ${{ !matrix.unavailable }} - run: python getinfo.py + run: | + python getinfo.py --help + python getinfo.py --version + python getinfo.py + python getinfo.py -o stdout + python getinfo.py -o getinfo-output.json From 5003c8cbbe04754728607bd2b939838336fa5c62 Mon Sep 17 00:00:00 2001 From: Ludovico Bianchi Date: Wed, 15 May 2024 08:47:10 -0500 Subject: [PATCH 6/6] Modify CI workflow trigger to support pull_request --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e360c10..af1f5eb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,6 +2,8 @@ name: Test getinfo.py on: push: + branches: [main] + pull_request: jobs: