Skip to content

Commit

Permalink
Add version flag to launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
R1kaB3rN committed Feb 20, 2024
1 parent 33bd828 commit 797f2e8
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ULWGL_VERSIONS.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[ulwgl.versions]
runtime = "sniper_platform_0.20231211.70175"
launcher = "0.1-RC3"
43 changes: 41 additions & 2 deletions ulwgl_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
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
from ulwgl_dl_util import get_ulwgl_proton


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:
Expand All @@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
52 changes: 52 additions & 0 deletions ulwgl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 797f2e8

Please sign in to comment.