import repository from arizona
[raven.git] / apps / tppublish / storktptohtml.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             ["",     "--requiretags",               "tags",           "store",       "string", None,                                       None,        "require tags when installing packages"],
21             ["",     "--lockdir",            "lockdir",     "store", "string",       "/var/lock",                        "dir",   "use the specified mutex lock directory (default /var/lock)"],
22             ["-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)"],
23             ["-o",   "--output",             "outputfile",  "store", "string",       None,                              "FILE", "write html to output file name"],
24             ["-d",   "--download",           "download",    "store", "string",       None,                              "DIR", "download packages to dir"]]
25    includes=[]
26 """
27
28 import os
29 import errno
30 import sys
31 import arizonaconfig
32 import arizonageneral
33 import storktrustedpackagesparse
34 import arizonareport
35 import urllib2
36 import storkerror
37 import storkpackagelist
38 import storkversion
39 import storkusername
40 import time\r
41 import storkmutex\r
42 import storktpqualify\r
43 \r
44 \r
45 def header(dest):\r
46    print >> dest, "<table>"\r
47 \r
48 def footer(dest):\r
49    print >> dest, "</table>"\r
50 \r
51 def output_line(dest, pack, url):\r
52    print >> dest, "<tr><td>" + pack["name"] + "</td><td>" + pack["version"] + "-" + pack["release"] + "</td>",\r
53    print >> dest, "<td><a href=\"" + url + "\">" + "download" + "</a></td></tr>"\r
54 \r
55 def download_package(destDir, pack, url):\r
56    destFileName = os.path.join(destDir, pack["filename"])\r
57    tempFileName = os.path.join(destDir, "download.tmp")\r
58 \r
59    if not os.path.exists(destFileName):\r
60        src = urllib2.urlopen(url)\r
61 \r
62        arizonareport.send_out(1, "downloading: " + url)\r
63 \r
64        tempFile = open(tempFileName, "w")\r
65        tempFile.write(src.read())\r
66        tempFile.close()\r
67 \r
68        os.rename(tempFileName, destFileName)\r
69 \r
70 def cleanup_packages(destDir, downloadedPacks):\r
71    """ Delete things left in destDir that were not part of the downloaded\r
72        packages. These will be packages leftover from previous sessions.\r
73    """\r
74    for filename in os.listdir(destDir):\r
75        if filename.endswith(".rpm"):\r
76            found = None\r
77            for pack in downloadedPacks:\r
78                if pack["filename"] == filename:\r
79                    found = pack\r
80            if not found:\r
81                arizonareport.send_out(1, "removing old package: " + filename)\r
82                os.remove(os.path.join(destDir, filename))\r
83 \r
84 def tag_check(tpentry):\r
85    """ stolen from storktrustedpackagesparse """\r
86 \r
87    tag_list = []\r
88    cmdline_tags = arizonaconfig.get_option("tags")\r
89    if cmdline_tags:\r
90       tag_list = cmdline_tags.lower().strip().split(",")\r
91       # remove any empty-string tags\r
92       tag_list = [tag for tag in tag_list if tag != '']\r
93 \r
94    tags = tag_list\r
95 \r
96    # check to see if package contains tags the user wants\r
97    for tag in tags:
98       if not tag in tpentry['tags']:
99          arizonareport.send_out(1, "tp reject: " + tpentry['pattern'] + \
100                                 " does not contain tag: '" + tag + "'")
101          return False
102
103    # check to see if user specified tags the package wants
104    for tag in tpentry['mantags']:
105       if (not tags) or (not tag in tags):
106           arizonareport.send_out(1, "tp reject: " + tpentry['pattern'] + \
107                                  " requires mandatory tag: '" + tag + "'")
108           return False\r
109 \r
110    return True\r
111 \r
112 \r
113
114 ########################### MAIN ###############################
115 def main(args):
116    """ TODO comment """
117
118    global glo_exitstatus;
119
120    if len(args)<2:
121       arizonareport.send_error(0, "syntax: storktptohtml.py username keystring")
122       sys.exit(1)
123
124    username = args[0]
125    keystring = args[1]
126
127    storkusername.build_key_database_from_single_keystring(username, keystring)
128
129    storkpackagelist.init()
130
131    #storktprank.compute_prefer_rules()
132    storktrustedpackagesparse.init()
133
134    sorted_list = storktpqualify.list_tp_hierarchy()
135
136    destFileName = arizonaconfig.get_option("outputfile")
137    if destFileName:
138        dest = open(destFileName, "w")
139    else:
140        dest = sys.stdout
141
142    downloadDir = arizonaconfig.get_option("download")
143    if downloadDir:
144        if not os.path.exists(downloadDir):
145            os.makedirs(downloadDir)
146
147    header(dest)
148
149    downloadedPacks = []
150    alreadyDone = {}
151
152    for item in sorted_list:
153        pack_list = storkpackagelist.find_packages({"_metadatahash": item["hash"].encode("utf8")})
154
155        if not tag_check(item):
156            continue
157
158        if not pack_list:
159            arizonareport.send_out(1, "no metadata: " + item['pattern'] + " hash=" + item['hash'])
160            continue
161
162        pack = pack_list[0]
163
164        if alreadyDone.get(pack['name'], False):
165            continue
166
167        arizonareport.send_out(1, "package: " + pack['name'] + " = " + item['pattern'])
168
169        url = pack["_URL"][0]
170        if not ("://" in url):
171           url = "http://" + url
172
173        output_line(dest, pack, url)
174
175        if downloadDir:
176            download_package(downloadDir, pack, url)
177            downloadedPacks.append(pack)
178
179        alreadyDone[pack['name']] = True
180
181    if downloadDir:
182        cleanup_packages(downloadDir, downloadedPacks)
183
184    footer(dest)
185
186
187
188
189
190 # Start main
191 if __name__ == "__main__":
192    try:
193       # use error reporting tool
194       storkerror.init_error_reporting("storktptohtml.py")
195
196       # get command line and config file options
197       args = arizonaconfig.init_options("storktptohtml.py", configfile_optvar="configfile", version=storkversion.VERREL)
198
199       if os.geteuid() > 0:
200           arizonareport.send_error(0, "You must be root to run this program...")
201           sys.exit(1)
202
203       # We don't want to run while storkconfigsync or storkreposync is updating
204       # files on disk, so grab the mutex.
205       storkmutex.wait_for_mutex()
206
207       main(args)
208    except KeyboardInterrupt:
209       arizonareport.send_out(0, "Exiting via keyboard interrupt...")
210       sys.exit(0)