Package copr_cli :: Module main
[hide private]
[frames] | no frames]

Source Code for Module copr_cli.main

  1  #-*- coding: UTF-8 -*- 
  2   
  3  import argparse 
  4  import sys 
  5  import ConfigParser 
  6   
  7  import subcommands 
  8  import copr_exceptions 
  9   
 10  __version__ = "0.2.0" 
 11  __description__ = "CLI tool to run copr" 
 12   
 13   
14 -def action_build(args):
15 """ Method called when the 'build' action has been selected by the 16 user. 17 18 :param args: argparse arguments provided by the user 19 20 """ 21 subcommands.build(args.copr, args.pkgs, 22 args.memory, args.timeout, not args.nowait, chroots=args.chroots)
23 24
25 -def action_create(args):
26 """ Method called when the 'create' action has been selected by the 27 user. 28 29 :param args: argparse arguments provided by the user 30 31 """ 32 subcommands.create(args.name, args.chroots, args.description, 33 args.instructions, args.repos, 34 args.initial_pkgs)
35 36
37 -def action_list(args):
38 """ Method called when the 'list' action has been selected by the 39 user. 40 41 :param args: argparse arguments provided by the user 42 43 """ 44 subcommands.listcoprs(args.username)
45 46
47 -def action_status(args):
48 subcommands.status(args.build_id)
49
50 -def action_cancel(args):
51 """ Method called when the 'cancel' action has been selected by the 52 user. 53 54 :param args: argparse arguments provided by the user 55 56 """ 57 subcommands.cancel(args.build_id)
58
59 -def setup_parser():
60 """ 61 Set the main arguments. 62 """ 63 parser = argparse.ArgumentParser(prog="copr-cli") 64 # General connection options 65 parser.add_argument("--version", action="version", 66 version="copr-cli {0}".format(__version__)) 67 68 subparsers = parser.add_subparsers(title="actions") 69 70 # create the parser for the "list" command 71 parser_list = subparsers.add_parser("list", 72 help="List all the copr of the " 73 "provided " 74 ) 75 parser_list.add_argument("username", nargs="?", 76 help="The username that you would like to " 77 "list the copr of (defaults to current user)" 78 ) 79 parser_list.set_defaults(func=action_list) 80 81 # create the parser for the "create" command 82 parser_create = subparsers.add_parser("create", 83 help="Create a new copr") 84 parser_create.add_argument("name", 85 help="The name of the copr to create") 86 parser_create.add_argument("--chroot", dest="chroots", action="append", 87 help="Chroot to use for this copr") 88 parser_create.add_argument("--repo", dest="repos", action="append", 89 help="Repository to add to this copr") 90 parser_create.add_argument("--initial-pkgs", dest="initial_pkgs", 91 action="append", 92 help="List of packages URL to build in this " 93 "new copr") 94 parser_create.add_argument("--description", 95 help="Description of the copr") 96 parser_create.add_argument("--instructions", 97 help="Instructions for the copr") 98 parser_create.set_defaults(func=action_create) 99 100 # create the parser for the "build" command 101 parser_build = subparsers.add_parser("build", 102 help="Build packages to a " 103 "specified copr") 104 parser_build.add_argument("copr", 105 help="The copr repo to build the package in. Can just name of project or even in format username/project." 106 ) 107 parser_build.add_argument("pkgs", nargs="+", 108 help="URL of packages to build") 109 parser_build.add_argument("-r", "--chroot", dest="chroots", action="append", 110 help="If you don't need this build for all the project's chroots. You can use it several times for each chroot you need.") 111 parser_build.add_argument("--memory", dest="memory", 112 help="") 113 parser_build.add_argument("--timeout", dest="timeout", 114 help="") 115 parser_build.add_argument("--nowait", action="store_true", default=False, 116 help="Don't wait for build") 117 parser_build.set_defaults(func=action_build) 118 119 # create the parser for the "status" command 120 parser_build = subparsers.add_parser("status", 121 help="Get build status of build" 122 " specified by its ID") 123 parser_build.add_argument("build_id", 124 help="Build ID") 125 parser_build.set_defaults(func=action_status) 126 127 # create the parser for the "cancel" command 128 parser_build = subparsers.add_parser("cancel", 129 help="Cancel build specified by its ID") 130 parser_build.add_argument("build_id", 131 help="Build ID") 132 parser_build.set_defaults(func=action_cancel) 133 134 return parser
135 136
137 -def main(argv=sys.argv[1:]):
138 try: 139 # Set up parser for global args 140 parser = setup_parser() 141 # Parse the commandline 142 arg = parser.parse_args() 143 arg.func(arg) 144 except KeyboardInterrupt: 145 sys.stderr.write("\nInterrupted by user.") 146 sys.exit(1) 147 except argparse.ArgumentTypeError, e: 148 sys.stderr.write("\nError: {0}".format(e)) 149 sys.exit(2) 150 except copr_exceptions.CoprCliException, e: 151 sys.stderr.write("\nError: {0}\n".format(e)) 152 sys.exit(3) 153 except ConfigParser.ParsingError, e: 154 sys.stderr.write("\nError: {0}\n".format(e)) 155 sys.stderr.write("Lines in INI file should not be indented.\n") 156 sys.exit(4)
157 # except Exception as e: 158 # print "Error: {0}".format(e) 159 # sys.exit(100) 160 161 162 if __name__ == "__main__": 163 main() 164