import repository from arizona
[raven.git] / apps / stork / storkquery.py
1 #! /usr/bin/env python
2 """
3 <Program Name>
4    stork.py
5
6 <Started>
7    April 27, 2004
8
9 <Author>
10    Programmed by Justin Cappos.  Refactored by Jeffry Johnston.
11
12 <Purpose>
13    Install/remove package(s) and package dependencies.
14 """
15
16
17 #           [option, long option,    variable,     action,        data,     default,                            metavar, description]
18 """arizonaconfig
19    options=[
20             ["-C",   "--configfile", "configfile", "store",       "string", "/usr/local/stork/etc/stork.conf", "FILE",   "use a different config file (/usr/local/stork/etc/stork.conf is the default)"]]
21    includes=[]
22 """
23
24 import os
25 import errno
26 import sys
27 import arizonaconfig
28 import arizonageneral
29 import storktrustedpackagesparse
30 import arizonareport
31 import storkerror
32 import storkpackagelist
33 import storkdependency
34 import storkversion
35 import time
36 import storkstatuscodes\r
37 import storkmutex\r
38
39 def do_find(package_list):
40    for pack in package_list:
41       (name, ver, tags) = storkdependency.split_pack_name(pack)
42
43       trusted_pkg_list = storkdependency.find_trusted_satisfying_packages(name, ver, tags)
44
45       i = 0
46       for pkg in trusted_pkg_list:
47          if i>0:
48              arizonareport.send_out(0, "-----------------")
49          arizonareport.send_out(0, "Package:   " + pkg.get("name", "unknown"))
50          arizonareport.send_out(1, "Version:   " + pkg.get("version", "unknown"))
51          arizonareport.send_out(1, "Release:   " + pkg.get("release", "unknown"))
52          arizonareport.send_out(1, "Hash:      " + pkg.get("hash", "unknown"))
53          arizonareport.send_out(0, "Size:      " + str(pkg.get("size", "unknown")))
54          arizonareport.send_out(0, "Filename:  " + pkg.get("filename", "unknown"))
55          arizonareport.send_out(1, "Requires:  " + ", ".join(pkg.get("requires", [])))
56          arizonareport.send_out(1, "Provides:  " + ", ".join(pkg.get("provides", [])))
57          arizonareport.send_out(0, "Url:       " + ", ".join(pkg.get("_URL", ["unknown"])))
58          arizonareport.send_out(2, "Files:     " + ", ".join(pkg.get("files", [])))
59          i = i+1
60
61 def init(args):
62    """ TODO comment """
63    # check params
64    arizonageneral.check_type_stringlist(args, "args", "stork.init")
65
66    # check for root user
67    if os.geteuid() > 0:
68       arizonareport.send_error(0, "You must be root to run this program...")
69       exit("Failure", storkstatuscodes.STATUS_ERROR)
70 \r
71    # set up package list
72    # note that this may have been already initialized by arizonacurl
73    storkpackagelist.init()
74
75    # set up trustedpackages file
76    if not arizonaconfig.get_option("disable_tpf"):
77       storktrustedpackagesparse.init()
78
79    # look up each "package name" in the package list.  the user may have
80    # given a filename by mistake, so we want to convert filenames to
81    # real package names, if possible.
82    new_args = []
83    for name in args:
84       # the user may have used a relational operator (=,>,etc) in the package
85       # name. We'll check both the full name, and the name without the relop.
86       (name_only, ver, tags) = storkdependency.split_pack_name(name)
87
88       if (not storkpackagelist.package_exists(name)) and  \
89             (not storkpackagelist.package_exists(name_only)):
90          arizonareport.send_error(2, "Warning: A package named `" + name + \
91                                      "' does not exist.\n         Attempting filename match.")
92          try:
93             packname = storkpackagelist.find_package_name(name)
94          except TypeError, e:
95             arizonareport.send_error(0, str(e))
96             sys.exit(1)
97
98          if packname != None:
99             # found a match
100             arizonareport.send_error(2, "Warning: Using package name `" + \
101                                         packname + "' instead of `" + \
102                                         name + "'.\n")
103             new_args.append(packname)
104          else:
105             # couldn't find anything, but try the original name anyway
106             new_args.append(name)
107       else:
108          new_args.append(name)
109
110    return new_args
111
112
113
114
115
116 ########################### MAIN ###############################
117 def main(args):
118    """ TODO comment """
119
120    global glo_exitstatus;
121
122    # check params
123    arizonageneral.check_type_stringlist(args, "args", "storkquery.main")
124
125    # process command line and initialize variables
126    args = init(args)
127
128    # now perform the operation
129    do_find(args)
130
131
132
133
134
135
136 # Start main
137 if __name__ == "__main__":
138    try:
139       # use error reporting tool
140       storkerror.init_error_reporting("storkquery.py")
141
142       # get command line and config file options
143       args = arizonaconfig.init_options("storkquery.py", configfile_optvar="configfile", version=storkversion.VERREL)
144
145       if os.geteuid() > 0:
146           arizonareport.send_error(0, "You must be root to run this program...")
147           sys.exit(1)
148
149       # smbaker: wait for mutex before doing arizonacurl update, to prevent
150       # multiple storks from downloading keys/config files
151       storkmutex.wait_for_mutex()
152
153       main(args)
154    except KeyboardInterrupt:
155       arizonareport.send_out(0, "Exiting via keyboard interrupt...")
156       sys.exit(0)