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 -def action_delete(args):
37 """ Method called when the 'delete' action has been selected by the 38 user. 39 40 :param args: argparse arguments provided by the user 41 """ 42 subcommands.delete(args.copr)
43 44
45 -def action_list(args):
46 """ Method called when the 'list' action has been selected by the 47 user. 48 49 :param args: argparse arguments provided by the user 50 51 """ 52 subcommands.listcoprs(args.username)
53 54
55 -def action_status(args):
56 subcommands.status(args.build_id)
57
58 -def action_cancel(args):
59 """ Method called when the 'cancel' action has been selected by the 60 user. 61 62 :param args: argparse arguments provided by the user 63 64 """ 65 subcommands.cancel(args.build_id)
66
67 -def setup_parser():
68 """ 69 Set the main arguments. 70 """ 71 parser = argparse.ArgumentParser(prog="copr-cli") 72 # General connection options 73 parser.add_argument("--version", action="version", 74 version="copr-cli {0}".format(__version__)) 75 76 subparsers = parser.add_subparsers(title="actions") 77 78 # create the parser for the "list" command 79 parser_list = subparsers.add_parser("list", 80 help="List all the copr of the " 81 "provided " 82 ) 83 parser_list.add_argument("username", nargs="?", 84 help="The username that you would like to " 85 "list the copr of (defaults to current user)" 86 ) 87 parser_list.set_defaults(func=action_list) 88 89 # create the parser for the "create" command 90 parser_create = subparsers.add_parser("create", 91 help="Create a new copr") 92 parser_create.add_argument("name", 93 help="The name of the copr to create") 94 parser_create.add_argument("--chroot", dest="chroots", action="append", 95 help="Chroot to use for this copr") 96 parser_create.add_argument("--repo", dest="repos", action="append", 97 help="Repository to add to this copr") 98 parser_create.add_argument("--initial-pkgs", dest="initial_pkgs", 99 action="append", 100 help="List of packages URL to build in this " 101 "new copr") 102 parser_create.add_argument("--description", 103 help="Description of the copr") 104 parser_create.add_argument("--instructions", 105 help="Instructions for the copr") 106 parser_create.set_defaults(func=action_create) 107 108 # create the parser for the "delete" command 109 parser_delete = subparsers.add_parser("delete", 110 help="Deletes the entire project") 111 parser_delete.add_argument("copr", 112 help="Name of your project to be deleted.") 113 parser_delete.set_defaults(func=action_delete) 114 115 # create the parser for the "build" command 116 parser_build = subparsers.add_parser("build", 117 help="Build packages to a " 118 "specified copr") 119 parser_build.add_argument("copr", 120 help="The copr repo to build the package in. Can just name of project or even in format username/project." 121 ) 122 parser_build.add_argument("pkgs", nargs="+", 123 help="URL of packages to build") 124 parser_build.add_argument("-r", "--chroot", dest="chroots", action="append", 125 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.") 126 parser_build.add_argument("--memory", dest="memory", 127 help="") 128 parser_build.add_argument("--timeout", dest="timeout", 129 help="") 130 parser_build.add_argument("--nowait", action="store_true", default=False, 131 help="Don't wait for build") 132 parser_build.set_defaults(func=action_build) 133 134 # create the parser for the "status" command 135 parser_build = subparsers.add_parser("status", 136 help="Get build status of build" 137 " specified by its ID") 138 parser_build.add_argument("build_id", 139 help="Build ID") 140 parser_build.set_defaults(func=action_status) 141 142 # create the parser for the "cancel" command 143 parser_build = subparsers.add_parser("cancel", 144 help="Cancel build specified by its ID") 145 parser_build.add_argument("build_id", 146 help="Build ID") 147 parser_build.set_defaults(func=action_cancel) 148 149 return parser
150 151
152 -def main(argv=sys.argv[1:]):
153 try: 154 # Set up parser for global args 155 parser = setup_parser() 156 # Parse the commandline 157 arg = parser.parse_args() 158 arg.func(arg) 159 except KeyboardInterrupt: 160 sys.stderr.write("\nInterrupted by user.") 161 sys.exit(1) 162 except copr_exceptions.CoprCliRequestException, e: 163 sys.stderr.write("\nSomething went wrong:") 164 sys.stderr.write("\nError: {0}\n".format(e)) 165 sys.exit(1) 166 except argparse.ArgumentTypeError, e: 167 sys.stderr.write("\nError: {0}".format(e)) 168 sys.exit(2) 169 except copr_exceptions.CoprCliException, e: 170 sys.stderr.write("\nError: {0}\n".format(e)) 171 sys.exit(3) 172 except ConfigParser.ParsingError, e: 173 sys.stderr.write("\nError: {0}\n".format(e)) 174 sys.stderr.write("Lines in INI file should not be indented.\n") 175 sys.exit(3) 176 except copr_exceptions.CoprCliBuildException, e: 177 sys.stderr.write("\nBuild error: {0}\n".format(e)) 178 sys.exit(4) 179 except copr_exceptions.CoprCliUnknownResponseException, e: 180 sys.stderr.write("\nError: {0}\n".format(e)) 181 sys.exit(5)
182 # except Exception as e: 183 # print "Error: {0}".format(e) 184 # sys.exit(100) 185 186 187 if __name__ == "__main__": 188 main() 189