def print_dockerfile(args): argparser = argparse.ArgumentParser( description="action \"print-dockerfile\": " + str(actions()["print-dockerfile"]["description"])) argparser.add_argument("env", help="Name of the environment " + "of which to print the combined Dockerfile") argparser.add_argument("--map-to-user", default="root", nargs=1,\ help="The unprivileged user which the Dockerfile should use." " If none is specified, defaults to unsafe root", dest="maptouser") args = argparser.parse_args(args) # Get user: if type(args.maptouser) == list: args.maptouser = args.maptouser[0] uname_or_uid = process_uname_arg(args.maptouser, complain_about_root=False) # Get environment: envs = buildenv.get_environments() env = None for _env in envs: if _env.name == args.env: env = _env break if env is None: print("p4aspaces: error: " + "no such environment found: '" + str(args.env) + "'", file=sys.stderr, flush=True) sys.exit(1) print(env.get_docker_file(add_workspace=True, user_id_or_name=uname_or_uid)) sys.exit(0)
def list_envs(args): argparser = argparse.ArgumentParser( description="action \"list-envs\": " + str(actions()["list-envs"]["description"])) args = argparser.parse_args(args) # List environments: envs = buildenv.get_environments() print("Available environments:") for env in envs: print(" " + env.name + "\n " + env.description) print("") print("Launch shell with:\n" + " p4aspaces shell " + sorted([env.name for env in envs])[0]) sys.exit(0)
def main(args=None): if args is None: args = sys.argv[1:] argparser = argparse.ArgumentParser() argparser.add_argument( "action", nargs=1, help="The p4a-build-spaces action out of \"" + "\", \"".join(sorted([aname for aname in actions().keys()])) + "\"", ) argparser.add_argument("arguments", nargs=argparse.REMAINDER, help="The arguments to the action. Use " + "\"p4aspaces <action> --help\" to see available " + "arguments and options for an action, e.g. " + "\"p4aspaces shell --help\"") if len(args) == 0: argparser.print_help(sys.stderr) print("p4aspaces: error: the following argument " + "is required: action", file=sys.stderr, flush=True) sys.exit(1) args = argparser.parse_args(args) if len(args.action) > 0: args.action = args.action[0] if args.action not in actions().keys(): print("p4aspaces: error: not a known action: \"" + str(args.action) + "\".", file=sys.stderr, flush=True) print(" Available actions are:", file=sys.stderr, flush=True) print(" - " + "\n - ".join(actions().keys()), file=sys.stderr, flush=True) print(" Get more detailed help on an action like this:", file=sys.stderr, flush=True) print(" p4aspaces " + str(list(actions().keys())[0]) + " --help", file=sys.stderr, flush=True) sys.exit(1) actions()[args.action]["function"](args.arguments)
def launch_shell_or_cmd(args, shell=False): if shell: argparser = argparse.ArgumentParser( description="action \"shell\": " + str(actions()["shell"]["description"])) else: argparser = argparse.ArgumentParser( description="action \"cmd\": " + str(actions()["cmd"]["description"])) argparser.add_argument("env", default=None, nargs=1, help="Specify an environment to use. Use 'p4aspaces list-envs' " + "to list available environments") argparser.add_argument("--map-to-user", default="interactive_prompt", nargs=1,\ help="The unprivileged user which to run as. If you use a " + "workspace (with --workspace), specify the user here that " + "owns the workspace so you can run the build as non-root " + "while still having correct access permissions", dest="maptouser") argparser.add_argument("--force-redownload-p4a", default=False, action="store_true", help="Force docker to rebuild from the p4a download step " + "(previous step remain cached), to ensure the newest version " + "as present in the repo", dest="force_p4a_redownload") argparser.add_argument("--force-rebuild", default=False, action="store_true", help="Force docker to rebuild entire image from scratch, " + "without using any caching", dest="clean_image_rebuild") argparser.add_argument("--workspace", help="Specify a workspace directory to be mounted into the " + "build environment at ~/workspace. If not specified, there " + "will be no access to files outside of the container", default=None, dest="workspace", nargs="?") if not shell: argparser.add_argument("command", nargs=1, help="The command to run, defaults to 'bash'. If you want to " + "just build the demo app, replace with 'testbuild' (which will " + "automatically write it's result to the --output target)", default="bash") argparser.add_argument("--output", help="Path where to place any .apk encountered after the " + "build inside the build environments internal ~/output folder", default=None, nargs="?", dest="output_file") argparser.add_argument("--p4a", default=None, nargs="?", help="Specify p4a release archive to use " + "(like 'https://github.com/kivy/python-for-android/" + "archive/master.zip'), or the branch " + "name (like 'master'). Will default to master branch " + "if unspecified", dest="p4a_url") args = argparser.parse_args(args) if shell: args.command = "bash" args.output_file = None else: if len(args.command) > 0: args.command = args.command[0] if len(args.env) > 0: args.env = args.env[0] # List environments: envs = buildenv.get_environments() # Test docker availability: try: output = subprocess.check_output(["docker", "ps"], stderr=subprocess.STDOUT) except (subprocess.CalledProcessError, FileNotFoundError) as e: print("p4aspaces: error: `docker ps` test command failed. " + "\n Is docker running, and do we have access?", file=sys.stderr, flush=True) sys.exit(1) # Choose user: if type(args.maptouser) == list: args.maptouser = args.maptouser[0] uname_or_id = process_uname_arg(args.maptouser) # Choose environment: env_name = args.env if len([env for env in envs if env.name == env_name]) == 0: print("p4aspaces: error: Not a known environment. Aborting.", file=sys.stderr, flush=True) sys.exit(1) env = [env for env in envs if env.name == env_name][0] # Choose download target: dl_target = "master" if args.p4a_url is not None: dl_target = args.p4a_url # Launch it: env.p4a_target = dl_target env.launch_shell(force_p4a_refetch=args.force_p4a_redownload, output_file=args.output_file, launch_cmd=args.command, workspace=args.workspace, user_id_or_name=uname_or_id, clean_image_rebuild=args.clean_image_rebuild)