-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcli.py
172 lines (143 loc) · 6.6 KB
/
cli.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import logging, os, sys
from argparse import ArgumentParser
from . import Config, ImageConfigs, GitRunner, PodmanRunner, write_config, load_config
class ConfigCLI:
ACTION = "config"
HELP = "Print or save config file"
@staticmethod
def execute(base_dir, args):
write_config(sys.stdout)
sys.stdout.write('\n')
if args.save is not None:
path = args.save if os.path.isabs(args.save) else os.path.join(base_dir, args.save)
if not path.endswith(".json"):
print("Invalid config file: %s, must be '.json'" % args.save)
sys.exit(1)
with open(path, 'w') as w:
write_config(w)
print("Saved to file: %s" % path)
@staticmethod
def bind(parser):
parser.add_argument("-s", "--save")
class ImageCLI:
ACTION = "fetch"
HELP = "Fetch remote build containers"
@staticmethod
def execute(base_dir, args):
podman = PodmanRunner(
base_dir,
dry_run=args.dry_run
)
podman.login()
podman.fetch_images(
images = args.image,
force=args.force_download
)
@staticmethod
def bind(parser):
parser.add_argument("-f", "--force-download", action="store_true")
parser.add_argument("-i", "--image", action="append", default=[], help="The image to fetch, all by default. Possible values: %s" % ", ".join(PodmanRunner.get_images()))
class GitCLI:
ACTION = "checkout"
HELP = "git checkout, version check, tar"
@staticmethod
def execute(base_dir, args):
git = GitRunner(base_dir, dry_run=args.dry_run)
if not args.skip_checkout:
git.checkout(args.treeish)
if not args.skip_check:
git.check_version(args.godot_version)
if not args.skip_tar:
git.tgz(args.godot_version)
@staticmethod
def bind(parser):
parser.add_argument("treeish", help="git treeish, possibly a git ref, or commit hash.", default="origin/master")
parser.add_argument("godot_version", help="godot version (e.g. 3.1-alpha5)")
parser.add_argument("-c", "--skip-checkout", action="store_true")
parser.add_argument("-t", "--skip-tar", action="store_true")
parser.add_argument("--skip-check", action="store_true")
class RunCLI:
ACTION = "run"
HELP = "Run the desired containers"
CONTAINERS = [cls.__name__.replace("Config", "") for cls in ImageConfigs]
@staticmethod
def execute(base_dir, args):
podman = PodmanRunner(base_dir, dry_run=args.dry_run)
build_mono = args.build == "all" or args.build == "mono"
build_classical = args.build == "all" or args.build == "classical"
if len(args.container) == 0:
args.container = RunCLI.CONTAINERS
to_build = [ImageConfigs[RunCLI.CONTAINERS.index(c)] for c in args.container]
for b in to_build:
podman.podrun(b, classical=build_classical, mono=build_mono, local=not args.remote, interactive=args.interactive)
def bind(parser):
parser.add_argument("-b", "--build", choices=["all", "classical", "mono"], default="all")
parser.add_argument("-k", "--container", action="append", default=[], help="The containers to build, one of %s" % RunCLI.CONTAINERS)
parser.add_argument("-r", "--remote", help="Run with remote containers", action="store_true")
parser.add_argument("-i", "--interactive", action="store_true", help="Enter an interactive shell inside the container instead of running the default command")
class ReleaseCLI:
ACTION = "release"
HELP = "Make a full release cycle, git checkout, reset, version check, tar, build all"
@staticmethod
def execute(base_dir, args):
git = GitRunner(base_dir, dry_run=args.dry_run)
podman = PodmanRunner(base_dir, dry_run=args.dry_run)
build_mono = args.build == "all" or args.build == "mono"
build_classical = args.build == "all" or args.build == "classical"
if not args.localhost and not args.skip_download:
podman.login()
podman.fetch_images(
force=args.force_download
)
if not args.skip_git:
git.checkout(args.git)
git.check_version(args.godot_version)
git.tgz(args.godot_version)
for b in ImageConfigs:
podman.podrun(b, classical=build_classical, mono=build_mono, local=args.localhost)
@staticmethod
def bind(parser):
parser.add_argument("godot_version", help="godot version (e.g. 3.1-alpha5)")
parser.add_argument("-b", "--build", choices=["all", "classical", "mono"], default="all")
parser.add_argument("-s", "--skip-download", action="store_true")
parser.add_argument("-c", "--skip-git", action="store_true")
parser.add_argument("-g", "--git", help="git treeish, possibly a git ref, or commit hash.", default="origin/master")
parser.add_argument("-f", "--force-download", action="store_true")
parser.add_argument("-l", "--localhost", action="store_true")
class CLI:
OPTS = [(v, getattr(Config, v)) for v in dir(Config) if not v.startswith("_")]
def add_command(self, cli):
parser = self.subparsers.add_parser(cli.ACTION, help=cli.HELP)
parser.add_argument("-n", "--dry-run", action="store_true")
parser.set_defaults(action_func=cli.execute)
cli.bind(parser)
def __init__(self, base_dir):
self.base_dir = base_dir
self.parser = ArgumentParser()
for k,v in CLI.OPTS:
self.parser.add_argument("--%s" % k)
self.parser.add_argument("-c", "--config", help="Configuration override")
self.subparsers = self.parser.add_subparsers(dest="action", help="The requested action", required=True)
self.add_command(ConfigCLI)
self.add_command(GitCLI)
self.add_command(ImageCLI)
self.add_command(RunCLI)
self.add_command(ReleaseCLI)
def execute(self):
args = self.parser.parse_args()
if args.config is not None:
path = args.config if os.path.isabs(args.config) else os.path.join(self.base_dir, args.config)
if not os.path.isfile(path):
print("Invalid config file: %s" % path)
sys.exit(1)
load_config(path)
for k,v in CLI.OPTS:
override = getattr(args, k)
if override is not None:
setattr(Config, k, override)
args.action_func(self.base_dir, args)
def main(loglevel=logging.DEBUG):
logging.basicConfig(level=loglevel)
CLI(os.getcwd()).execute()
if __name__ == "__main__":
main()