-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodfs.py
More file actions
executable file
·66 lines (54 loc) · 2.32 KB
/
Copy pathmodfs.py
File metadata and controls
executable file
·66 lines (54 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: 2023 Jonas Tobias Hopusch <git@jotoho.de>
# SPDX-License-Identifier: AGPL-3.0-only
from pathlib import Path
from sys import stderr
from code.commandline import process_commandline_args, get_instance_path
from code.mod import set_mod_base_path, resolve_base_dir
from code.settings import set_instance_settings, InstanceSettings, get_instance_settings
from code.subcommands import get_subcommands_table, SubcommandArgDict
def app_install_dir() -> Path:
return Path(__file__).resolve(strict=True).parent
def execution_path_dir() -> Path | None:
from shutil import which
from sys import argv
if len(argv) > 0:
script_path_str = which(argv[0])
if script_path_str is not None:
script_path = Path(script_path_str)
if script_path.exists():
return script_path.resolve(strict=True).parent
return None
def get_git_version(working_dir: Path) -> str:
from subprocess import run, PIPE, DEVNULL
from os import getcwd, chdir
previous_working_dir = getcwd()
result = run(["git", "describe", "--tags", "--always", "--dirty"],
stdout=PIPE, stderr=DEVNULL, cwd=working_dir)
chdir(previous_working_dir)
return result.stdout.decode().strip()
def main() -> None:
instance_path: Path = get_instance_path()
set_mod_base_path(instance_path)
assert resolve_base_dir(base_dir=None) is not None
set_instance_settings(InstanceSettings(instance_path))
assert get_instance_settings() is not None
# Warning: mod base path must be known before this can be safely called
args: SubcommandArgDict = vars(process_commandline_args())
if args["show_args"]:
from pprint import pprint
pprint(args)
exec_dir = execution_path_dir()
args["version_string"] = get_git_version(exec_dir
if exec_dir is not None
else app_install_dir())
subcommands = get_subcommands_table()
if args["subcommand"] in subcommands:
subcommands[args["subcommand"]](args)
else:
print(f"ERROR: No action has been implemented for subcommand {args['subcommand']}.",
"This is either a bug or a missing feature!", file=stderr)
exit(1)
if __name__ == "__main__":
main()