-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from MAK-Relic-Tool/cli-support
Adds SGA support to the command line interface Unpacking: `relic sga unpack src_file out_dir` Packing: `relic sga pack {vX} -h` where vX is the version of SGA to pack. `-h` will print the commands help, which will ispecify how to run that SGA's packer.
- Loading branch information
Showing
4 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import annotations | ||
|
||
from argparse import ArgumentParser, Namespace | ||
from typing import Optional | ||
|
||
import fs.copy | ||
from fs.base import FS | ||
from relic.core.cli import CliPluginGroup, _SubParsersAction, CliPlugin | ||
|
||
|
||
class RelicSgaCli(CliPluginGroup): | ||
GROUP = "relic.cli.sga" | ||
|
||
def _create_parser( | ||
self, command_group: Optional[_SubParsersAction] = None | ||
) -> ArgumentParser: | ||
if command_group is None: | ||
return ArgumentParser("sga") | ||
else: | ||
return command_group.add_parser("sga") | ||
|
||
|
||
class RelicSgaUnpackCli(CliPlugin): | ||
def _create_parser( | ||
self, command_group: Optional[_SubParsersAction] = None | ||
) -> ArgumentParser: | ||
parser: ArgumentParser | ||
if command_group is None: | ||
parser = ArgumentParser("unpack") | ||
else: | ||
parser = command_group.add_parser("unpack") | ||
|
||
parser.add_argument("src_sga", type=str, help="Source SGA File") | ||
parser.add_argument("out_dir", type=str, help="Output Directory") | ||
|
||
return parser | ||
|
||
def command(self, ns: Namespace) -> Optional[int]: | ||
infile: str = ns.src_sga | ||
outdir: str = ns.out_dir | ||
|
||
print(f"Unpacking `{infile}`") | ||
|
||
def _callback(_1: FS, srcfile: str, _2: FS, _3: str) -> None: | ||
print(f"\t\tUnpacking File `{srcfile}`") | ||
|
||
fs.copy.copy_fs(f"sga://{infile}", f"osfs://{outdir}", on_copy=_callback) | ||
|
||
return None # To shut-up mypy | ||
|
||
|
||
class RelicSgaPackCli(CliPluginGroup): | ||
GROUP = "relic.cli.sga.pack" | ||
|
||
def _create_parser( | ||
self, command_group: Optional[_SubParsersAction] = None | ||
) -> ArgumentParser: | ||
parser: ArgumentParser | ||
if command_group is None: | ||
parser = ArgumentParser("pack") | ||
else: | ||
parser = command_group.add_parser("pack") | ||
|
||
# pack further delegates to version plugins | ||
|
||
return parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import io | ||
import subprocess | ||
|
||
# Local testing requires running `pip install -e "."` | ||
from contextlib import redirect_stdout | ||
from typing import Sequence | ||
|
||
import pytest | ||
|
||
|
||
class CommandTests: | ||
def test_run(self, args: Sequence[str], output: str, exit_code: int): | ||
_args = ["relic", *args] | ||
cmd = subprocess.run(_args, capture_output=True, text=True) | ||
result = cmd.stdout | ||
status = cmd.returncode | ||
print(f"'{result}'") # Visual Aid for Debugging | ||
assert output in result | ||
assert status == exit_code | ||
|
||
def test_run_with(self, args: Sequence[str], output: str, exit_code: int): | ||
from relic.core.cli import cli_root | ||
|
||
with io.StringIO() as f: | ||
with redirect_stdout(f): | ||
status = cli_root.run_with(*args) | ||
f.seek(0) | ||
result = f.read() | ||
print(f"'{result}'") # Visual Aid for Debugging | ||
assert output in result | ||
assert status == exit_code | ||
|
||
|
||
_SGA_HELP = ["sga", "-h"], """usage: relic sga [-h] {pack,unpack} ...""", 0 | ||
_SGA_PACK_HELP = ["sga", "pack", "-h"], """usage: relic sga pack [-h] {} ...""", 0 | ||
_SGA_UNPACK_HELP = ["sga", "unpack", "-h"], """usage: relic sga unpack [-h]""", 0 | ||
|
||
_TESTS = [_SGA_HELP, _SGA_PACK_HELP, _SGA_UNPACK_HELP] | ||
_TEST_IDS = [" ".join(_[0]) for _ in _TESTS] | ||
|
||
|
||
@pytest.mark.parametrize(["args", "output", "exit_code"], _TESTS, ids=_TEST_IDS) | ||
class TestRelicSgaCli(CommandTests): | ||
... |