import repository from arizona
[raven.git] / apps / mprepo / repoutil.py
1 #! /usr/bin/env python
2
3 """
4 <Program Name>
5
6 <Started>
7
8 <Author>
9
10 <Purpose>
11 """
12
13 #           [option, long option,      variable,       action,  data,     default,                           metavar, description]
14 """arizonaconfig
15    options=[["-C",   "--configfile",    "configfile",    "store",      "string", "/usr/local/mprepo/etc/mprepo.conf",               "FILE", "use a different config file (/usr/local/mprepo/etc/mprepo.conf is the default)"]]
16    includes=[]
17 """
18
19 import sys
20 import arizonaconfig
21 import arizonareport
22 import tempfile
23 import shutil
24
25 import passfile
26 import repometafileupdate
27 import repoconfig
28 import repobackend
29 import repoclassify
30 import os
31
32 def main():
33    # process command line arguments
34    args = arizonaconfig.init_options("repoutil.py", configfile_optvar="configfile", version="2.0")
35    if len(args) < 1:
36       arizonareport.send_error(1, "Usage: repoutil.py [OPTION(s)] command args")
37       arizonareport.send_error(1, "   command = ")
38       arizonareport.send_error(1, "      metafileupdate <fn> <fn> ...")
39       arizonareport.send_error(1, "      process <fn> <fn> ...")
40       arizonareport.send_error(1, "      processdir <dir>")
41       arizonareport.send_error(1, "      rebuildtarballs")
42       sys.exit(1)
43
44    repoconfig.init()
45
46    command = args[0]
47    args = args[1:]
48
49    if command == "metafileupdate":
50       if len(args) < 1:
51          arizonareport.send_error(1, "must specify list of files to update")
52          sys.exit(1)
53
54       repometafileupdate.metafileUpdate(args,
55                                         repoconfig.tarball_dir,
56                                         "metafile",
57                                         repoconfig.privatekey_fn)
58
59    if command == "process":
60       if len(args) < 1:
61          arizonareport.send_error(1, "must specify list of files to process")
62          sys.exit(1)
63
64       files = []
65       for arg in args:
66          file = {"srcname": arg}
67          files.append(file)
68
69       repobackend.process_uploads(files)
70
71    if command == "processdir":
72       if len(args) != 1:
73          arizonareport.send_error(1, "must specify directory to process")
74          sys.exit(1)
75
76       files = []
77       for name in os.listdir(args[0]):
78          pathname = os.path.join(args[0], name)
79          if os.path.isfile(pathname):
80             file = {"srcname": pathname}
81             files.append(file)
82
83       repobackend.process_uploads(files)
84
85    if command == "sendevent":
86       metafilename =  os.path.join(repoconfig.tarball_dir, "metafile")
87       if not os.path.isfile(metafilename):
88           print metafilename, "is not a file"
89           sys.exit(1)
90       repobackend.send_event(metafilename)
91
92    if command == "adduser":
93       passfilename = os.path.join(repoconfig.repo_etc_dir, "password")
94       if len(args) < 2:
95          arizonareport.send_error(1, "syntax is adduser <user> <password> [slice1 slice2 ...]")
96          sys.exit(1)
97
98       #if len(args) > 2:
99       #   slices = args[2:]
100       #else:
101       #   slices = None
102
103       passfile.add(args[0], args[1], args[2:], passfilename)
104
105    if command == "removeuser":
106       passfilename = os.path.join(repoconfig.repo_etc_dir, "password")
107       if len(args) != 1:
108          arizonareport.send_error(1, "syntax is removeuser <user>")
109          sys.exit(1)
110
111       passfile.remove(args[0], passfilename)
112
113    if command == "testuser":
114       passfilename = os.path.join(repoconfig.repo_etc_dir, "password")
115       if len(args) != 2:
116          arizonareport.send_error(1, "syntax is testuser <user><password>")
117          sys.exit(1)
118
119       passfile.login(args[0], args[1], passfilename)
120       slices = passfile.get_slices(args[0], passfilename)
121       print "login tested successfully, slices = ", slices
122
123    if command == "extractmeta":
124       if len(args) < 1:
125          arizonareport.send_error(1, "must specify list of files to process")
126          sys.exit(1)
127
128       files = []
129       for arg in args:
130          file = {"srcname": arg}
131
132          # to simulate what the repo would do, copy the file to a temporary
133          # filename
134          file['name'] = tempfile.mktemp()
135          shutil.copy(file['srcname'], file['name'])
136
137          #file['name'] = file['srcname']
138
139          # fill in some fields that repobackend.process_uploads usually fills
140          # in for us
141          file['desiredname'] = os.path.basename(file['srcname'])
142          file['type'] = repoclassify.getFiletype(file['srcname'])
143
144          repobackend.extract_metadata(file)
145
146          print "name:", file['name']
147          print "hash:", file['metahash']
148          print "metadata:", file['metadata']
149
150    if command == "rebuildtarballs":
151        repobackend.rebuild_metadata_tarballs()
152
153
154 # Start main
155 if __name__ == "__main__":
156     main()