import repository from arizona
[raven.git] / apps / mprepo / repocli.py
1 import os
2 from sfa.client import sfi
3 from repoclient import *
4
5 repo_url = None
6 repo_client = None
7
8 def create_parser():
9    parser = sfi.create_parser()
10    parser.add_option("", "--storkrepo", dest="storkrepo",
11         help="stork repository", metavar="URL", default=None)
12    return parser
13
14 def create_cmd_parser(command):
15    cmdargs = {"uploadfile": "filename"}
16
17    parser = sfi.create_cmd_parser(command, cmdargs)
18    return parser
19
20 def set_servers(options):
21    global repo_url
22    global repo_client
23
24    sfi.set_servers(options)
25
26    if (options.storkrepo is not None):
27       storkrepo = options.storkrepo
28    elif ("SFI_STORKREPO" in os.environ):
29       storkrepo = os.environ["SFI_STORKREPO"]
30    else:
31       print "No Known Stork Repo (see cmd line options)"
32       sys.exit(1)
33
34    key_file = sfi.get_key_file()
35    cert_file = sfi.get_cert_file(key_file)
36
37    repo_client = RepoClient(storkrepo, key_file, cert_file)
38
39 def dispatch(command, cmd_opts, cmd_args):
40    try:
41       globals()[command](cmd_opts, cmd_args)
42    except KeyError:
43       sfi.dispatch(command, cmd_opts, cmd_args)
44
45 def main():
46    global verbose
47
48    parser = create_parser()
49    (options, args) = parser.parse_args()
50
51    if len(args) <= 0:
52         print "No command given. Use -h for help."
53         return -1
54
55    command = args[0]
56    (cmd_opts, cmd_args) = create_cmd_parser(command).parse_args(args[1:])
57    verbose = options.verbose
58    if verbose :
59       print options.registry, options.sm, options.dir, options.verbose,\
60          options.user, options.auth
61       print command
62       if command in ("nodes", "resources"):
63          print cmd_opts.format
64       elif command in ("list","show","remove"):
65          print cmd_opts.type
66       print cmd_args
67
68    set_servers(options)
69
70    try:
71       dispatch(command, cmd_opts, cmd_args)
72    except KeyError:
73       print "Command not found:", command
74       sys.exit(1)
75
76    return
77
78 def uploadfile(opts, args):
79    upload_filename = args[0]
80    user_cred = sfi.get_user_cred()
81
82    upload_bytes = file(upload_filename).read()
83    result = repo_client.upload_file(user_cred, upload_filename, upload_bytes)
84
85    print "RESULT:", result
86
87 if __name__=="__main__":
88    main()
89
90