From 797f2e89a0c4df4692efe569f50e6d61afbbdf78 Mon Sep 17 00:00:00 2001 From: R1kaB3rN <100738684+R1kaB3rN@users.noreply.github.com> Date: Mon, 19 Feb 2024 19:46:15 -0800 Subject: [PATCH] Add version flag to launcher --- ULWGL_VERSIONS.toml | 3 +++ ulwgl_run.py | 43 +++++++++++++++++++++++++++++++++++-- ulwgl_test.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 ULWGL_VERSIONS.toml diff --git a/ULWGL_VERSIONS.toml b/ULWGL_VERSIONS.toml new file mode 100644 index 000000000..01c40b0d8 --- /dev/null +++ b/ULWGL_VERSIONS.toml @@ -0,0 +1,3 @@ +[ulwgl.versions] +runtime = "sniper_platform_0.20231211.70175" +launcher = "0.1-RC3" diff --git a/ulwgl_run.py b/ulwgl_run.py index 605c6d2c4..b49ca5173 100755 --- a/ulwgl_run.py +++ b/ulwgl_run.py @@ -7,7 +7,7 @@ import sys from pathlib import Path import tomllib -from typing import Dict, Any, List, Set, Union, Tuple +from typing import Dict, Any, List, Set, Union, Tuple, NoReturn import ulwgl_plugins from re import match import subprocess @@ -15,7 +15,7 @@ def parse_args() -> Union[Namespace, Tuple[str, List[str]]]: # noqa: D103 - opt_args: Set[str] = {"--help", "-h", "--config"} + opt_args: Set[str] = {"--help", "-h", "--config", "-v", "--version"} exe: str = Path(__file__).name usage: str = f""" example usage: @@ -32,6 +32,7 @@ def parse_args() -> Union[Namespace, Tuple[str, List[str]]]: # noqa: D103 formatter_class=argparse.RawTextHelpFormatter, ) parser.add_argument("--config", help="path to TOML file") + parser.add_argument("-v", "--version", action="store_true", help="print version") if not sys.argv[1:]: err: str = "Please see project README.md for more info and examples.\nhttps://github.com/Open-Wine-Components/ULWGL-launcher" @@ -44,6 +45,37 @@ def parse_args() -> Union[Namespace, Tuple[str, List[str]]]: # noqa: D103 return sys.argv[1], sys.argv[2:] +def print_versions(paths: List[Path]) -> NoReturn: + """Print the versions of this launcher and all of its associated tools declared in the config file. + + NOTE: The following table is required: [ulwgl.versions] + """ + version: str = "" + + for path in paths: + if path.is_file(): + toml: Dict[str, Any] = None + + with path.open(mode="rb") as file: + toml = tomllib.load(file) + + if "ulwgl" in toml and "versions" in toml["ulwgl"]: + exe: str = Path(__file__).name + launcher: str = toml["ulwgl"]["versions"]["launcher"] + tools: str = "\n".join( + [ + f"{key}: {val}" + for key, val in toml["ulwgl"]["versions"].items() + if key != "launcher" + ] + ) + version = f"{exe} {launcher}\n\n{tools}" + + break + + raise SystemExit(version) + + def setup_pfx(path: str) -> None: """Create a symlink to the WINE prefix and tracked_files file.""" pfx: Path = Path(path).joinpath("pfx").expanduser() @@ -312,6 +344,13 @@ def main() -> int: # noqa: D103 args: Union[Namespace, Tuple[str, List[str]]] = parse_args() opts: List[str] = None + if isinstance(args, Namespace) and getattr(args, "version", None): + paths: List[Path] = [ + Path(__file__).parent.joinpath("ULWGL_VERSIONS.toml"), + Path("/usr/share/ULWGL/ULWGL_VERSIONS.toml"), + ] + print_versions(paths) + if isinstance(args, Namespace): set_env_toml(env, args) else: diff --git a/ulwgl_test.py b/ulwgl_test.py index a14ac573e..e62047d28 100644 --- a/ulwgl_test.py +++ b/ulwgl_test.py @@ -100,6 +100,28 @@ def tearDown(self): if self.test_proton_dir.exists(): rmtree(self.test_proton_dir.as_posix()) + def test_print_versions(self): + """Test print_versions. + + In the real usage, we search /usr/share/ULWGL or the current script's dir for the config. + """ + test_toml = "ULWGL_VERSIONS.toml" + toml_str = """ + [ulwgl.versions] + launcher = "foo" + runtime = "foo" + """ + toml_path = self.test_file + "/" + test_toml + Path(toml_path).touch() + + with Path(toml_path).open(mode="w") as file: + file.write(toml_str) + + with self.assertRaises(SystemExit): + ulwgl_run.print_versions( + [Path(self.test_file).joinpath("ULWGL_VERSIONS.toml")] + ) + def test_latest_interrupt(self): """Test _get_latest in the event the user interrupts the download/extraction process. @@ -1442,6 +1464,36 @@ def test_parse_args_config(self): result, Namespace, "Expected a Namespace from parse_arg" ) + def test_parse_args_version(self): + """Test parse_args --version. + + An SystemExit should be raised after printing the version + """ + test_toml = "foo.toml" + toml_str = """ + [ulwgl.versions] + launcher = 'foo' + """ + result = None + toml_path = self.test_file + "/" + test_toml + Path(toml_path).touch() + + with Path(toml_path).open(mode="w") as file: + file.write(toml_str) + + with patch.object( + ulwgl_run, + "parse_args", + return_value=argparse.Namespace(version=self.test_file), + ): + with patch("ulwgl_run.print_versions"): + # Mock the print_version call + # The VM will probably not have the dirs/ULWGL_VERSIONS.toml file created + result = ulwgl_run.parse_args() + self.assertIsInstance( + result, Namespace, "Expected a Namespace from parse_arg" + ) + def test_env_proton_nodir(self): """Test check_env when $PROTONPATH is not set on failing to setting it.