|
| 1 | +import logging, os, sys |
| 2 | +from argparse import ArgumentParser |
| 3 | + |
| 4 | +from . import Config, ImageConfigs, GitRunner, PodmanRunner, write_config, load_config |
| 5 | + |
| 6 | +class ConfigCLI: |
| 7 | + ACTION = "config" |
| 8 | + HELP = "Print or save config file" |
| 9 | + |
| 10 | + @staticmethod |
| 11 | + def execute(base_dir, args): |
| 12 | + write_config(sys.stdout) |
| 13 | + sys.stdout.write('\n') |
| 14 | + if args.save is not None: |
| 15 | + path = args.save if os.path.isabs(args.save) else os.path.join(base_dir, args.save) |
| 16 | + if not path.endswith(".json"): |
| 17 | + print("Invalid config file: %s, must be '.json'" % args.save) |
| 18 | + sys.exit(1) |
| 19 | + with open(path, 'w') as w: |
| 20 | + write_config(w) |
| 21 | + print("Saved to file: %s" % path) |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def bind(parser): |
| 25 | + parser.add_argument("-s", "--save") |
| 26 | + |
| 27 | +class ImageCLI: |
| 28 | + ACTION = "fetch" |
| 29 | + HELP = "Fetch remote build containers" |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def execute(base_dir, args): |
| 33 | + podman = PodmanRunner( |
| 34 | + base_dir, |
| 35 | + dry_run=args.dry_run |
| 36 | + ) |
| 37 | + podman.login() |
| 38 | + podman.fetch_images( |
| 39 | + images = args.image, |
| 40 | + force=args.force_download |
| 41 | + ) |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def bind(parser): |
| 45 | + parser.add_argument("-f", "--force-download", action="store_true") |
| 46 | + parser.add_argument("-i", "--image", action="append", default=[], help="The image to fetch, all by default. Possible values: %s" % ", ".join(PodmanRunner.get_images())) |
| 47 | + |
| 48 | + |
| 49 | +class GitCLI: |
| 50 | + ACTION = "checkout" |
| 51 | + HELP = "git checkout, version check, tar" |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def execute(base_dir, args): |
| 55 | + git = GitRunner(base_dir, dry_run=args.dry_run) |
| 56 | + if not args.skip_checkout: |
| 57 | + git.checkout(args.treeish) |
| 58 | + if not args.skip_check: |
| 59 | + git.check_version(args.godot_version) |
| 60 | + if not args.skip_tar: |
| 61 | + git.tgz(args.godot_version) |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def bind(parser): |
| 65 | + parser.add_argument("treeish", help="git treeish, possibly a git ref, or commit hash.", default="origin/master") |
| 66 | + parser.add_argument("godot_version", help="godot version (e.g. 3.1-alpha5)") |
| 67 | + parser.add_argument("-c", "--skip-checkout", action="store_true") |
| 68 | + parser.add_argument("-t", "--skip-tar", action="store_true") |
| 69 | + parser.add_argument("--skip-check", action="store_true") |
| 70 | + |
| 71 | + |
| 72 | +class RunCLI: |
| 73 | + ACTION = "run" |
| 74 | + HELP = "Run the desired containers" |
| 75 | + |
| 76 | + CONTAINERS = [cls.__name__.replace("Config", "") for cls in ImageConfigs] |
| 77 | + |
| 78 | + @staticmethod |
| 79 | + def execute(base_dir, args): |
| 80 | + podman = PodmanRunner(base_dir, dry_run=args.dry_run) |
| 81 | + build_mono = args.build == "all" or args.build == "mono" |
| 82 | + build_classical = args.build == "all" or args.build == "classical" |
| 83 | + if len(args.container) == 0: |
| 84 | + args.container = RunCLI.CONTAINERS |
| 85 | + to_build = [ImageConfigs[RunCLI.CONTAINERS.index(c)] for c in args.container] |
| 86 | + for b in to_build: |
| 87 | + podman.podrun(b, classical=build_classical, mono=build_mono, local=not args.remote, interactive=args.interactive) |
| 88 | + |
| 89 | + def bind(parser): |
| 90 | + parser.add_argument("-b", "--build", choices=["all", "classical", "mono"], default="all") |
| 91 | + parser.add_argument("-k", "--container", action="append", default=[], help="The containers to build, one of %s" % RunCLI.CONTAINERS) |
| 92 | + parser.add_argument("-r", "--remote", help="Run with remote containers", action="store_true") |
| 93 | + parser.add_argument("-i", "--interactive", action="store_true", help="Enter an interactive shell inside the container instead of running the default command") |
| 94 | + |
| 95 | + |
| 96 | +class ReleaseCLI: |
| 97 | + ACTION = "release" |
| 98 | + HELP = "Make a full release cycle, git checkout, reset, version check, tar, build all" |
| 99 | + |
| 100 | + @staticmethod |
| 101 | + def execute(base_dir, args): |
| 102 | + git = GitRunner(base_dir, dry_run=args.dry_run) |
| 103 | + podman = PodmanRunner(base_dir, dry_run=args.dry_run) |
| 104 | + build_mono = args.build == "all" or args.build == "mono" |
| 105 | + build_classical = args.build == "all" or args.build == "classical" |
| 106 | + if not args.localhost and not args.skip_download: |
| 107 | + podman.login() |
| 108 | + podman.fetch_images( |
| 109 | + force=args.force_download |
| 110 | + ) |
| 111 | + if not args.skip_git: |
| 112 | + git.checkout(args.git) |
| 113 | + git.check_version(args.godot_version) |
| 114 | + git.tgz(args.godot_version) |
| 115 | + |
| 116 | + for b in ImageConfigs: |
| 117 | + podman.podrun(b, classical=build_classical, mono=build_mono, local=args.localhost) |
| 118 | + |
| 119 | + @staticmethod |
| 120 | + def bind(parser): |
| 121 | + parser.add_argument("godot_version", help="godot version (e.g. 3.1-alpha5)") |
| 122 | + parser.add_argument("-b", "--build", choices=["all", "classical", "mono"], default="all") |
| 123 | + parser.add_argument("-s", "--skip-download", action="store_true") |
| 124 | + parser.add_argument("-c", "--skip-git", action="store_true") |
| 125 | + parser.add_argument("-g", "--git", help="git treeish, possibly a git ref, or commit hash.", default="origin/master") |
| 126 | + parser.add_argument("-f", "--force-download", action="store_true") |
| 127 | + parser.add_argument("-l", "--localhost", action="store_true") |
| 128 | + |
| 129 | + |
| 130 | +class CLI: |
| 131 | + OPTS = [(v, getattr(Config, v)) for v in dir(Config) if not v.startswith("_")] |
| 132 | + |
| 133 | + def add_command(self, cli): |
| 134 | + parser = self.subparsers.add_parser(cli.ACTION, help=cli.HELP) |
| 135 | + parser.add_argument("-n", "--dry-run", action="store_true") |
| 136 | + parser.set_defaults(action_func=cli.execute) |
| 137 | + cli.bind(parser) |
| 138 | + |
| 139 | + def __init__(self, base_dir): |
| 140 | + self.base_dir = base_dir |
| 141 | + self.parser = ArgumentParser() |
| 142 | + for k,v in CLI.OPTS: |
| 143 | + self.parser.add_argument("--%s" % k) |
| 144 | + self.parser.add_argument("-c", "--config", help="Configuration override") |
| 145 | + self.subparsers = self.parser.add_subparsers(dest="action", help="The requested action", required=True) |
| 146 | + self.add_command(ConfigCLI) |
| 147 | + self.add_command(GitCLI) |
| 148 | + self.add_command(ImageCLI) |
| 149 | + self.add_command(RunCLI) |
| 150 | + self.add_command(ReleaseCLI) |
| 151 | + |
| 152 | + def execute(self): |
| 153 | + args = self.parser.parse_args() |
| 154 | + if args.config is not None: |
| 155 | + path = args.config if os.path.isabs(args.config) else os.path.join(self.base_dir, args.config) |
| 156 | + if not os.path.isfile(path): |
| 157 | + print("Invalid config file: %s" % path) |
| 158 | + sys.exit(1) |
| 159 | + load_config(path) |
| 160 | + for k,v in CLI.OPTS: |
| 161 | + override = getattr(args, k) |
| 162 | + if override is not None: |
| 163 | + setattr(Config, k, override) |
| 164 | + args.action_func(self.base_dir, args) |
| 165 | + |
| 166 | + |
| 167 | +def main(loglevel=logging.DEBUG): |
| 168 | + logging.basicConfig(level=loglevel) |
| 169 | + CLI(os.getcwd()).execute() |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + main() |
0 commit comments