import repository from arizona
[raven.git] / apps / importyum / yumimport.py
1 #! /usr/bin/env python
2 """
3 <Program Name>
4    yumimport.py
5
6 <Started>
7    6-10-09
8
9 <Author>
10    Programmed by .
11
12 <Purpose>
13    Routines for .
14 """
15
16 #           [option, long option,    variable,     action,        data,     default,                            metavar, description]
17 """arizonaconfig
18    options=[
19             ["-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)"],
20             ["",     "--importusername",  "importusername",  "store",       "string", "yum",                                None,    "username to use when importing"],
21             ["",     "--tpfiledir",   "tpfiledir",   "store",       "string", "/repository/yumimport/user-upload/tpfiles",  None,    "directory to put tpfile"],
22             ["",     "--metadatadir", "metadatadir", "store",       "string", "/repository/yumimport/metadata",     None,    "directory to put metadata files"],
23             ["",     "--privkeyfile", "privkeyfile", "store",       "string", "/repository/yumimport/keys/yum.privatekey", None, "name of privatekey"]]
24
25    includes=[]
26 """
27
28 import arizonaconfig
29 import arizonacrypt
30 import arizonareport
31 import storkerror
32 import storkversion
33 import ravenlib.package.storkpackage
34
35 import os
36 import re
37 import shutil
38 import sys
39 import urlparse
40 import yumxmlparse
41 import yumflparse
42
43 def write_tpfile(packages, filename):
44 #   XXX this is what we'd do if we wanted to preserve the contents of the existing file
45 #   if os.path.exists(filename):
46 #       lines = file(filename).readlines()
47 #   else:
48 #       lines = []
49
50    lines = []
51
52    for package in packages:
53        line = '<FILE PATTERN="' + package["filename"] + '" HASH="' + package["_metadatahash"] + '" ACTION="ALLOW"/>\n'
54        if not (line in lines):
55           lines.append(line)
56
57    f = open(filename + ".tmp", "w")
58    f.writelines(lines)
59    f.close()
60
61    os.rename(filename + ".tmp", filename)
62
63 def importfile(baseurl, filename, filelists_filename, username, destmetadir, desttpdir):
64    print "importing filelist:", filelists_filename
65    filelists = yumflparse.YumFileListsParse(filelists_filename)
66
67    print "importing primary:", filename
68    packages = yumxmlparse.YumXmlParse(filename)
69
70    print "merging filelist data into primary data"
71    for package in packages:
72        pkgid = package.get("hash", None)
73        if pkgid:
74            filelist = filelists.get(pkgid, None)
75            if filelist:
76                package["files"] = filelist["files"]
77            else:
78                print "failed to find filelist for", package["name"], package["version"]
79        else:
80            print "failed to find pkgid for", package["name"], package["version"]
81            print package
82            sys.exit(-1)
83
84    print "writing metadata"
85    for package in packages:
86        arizonareport.send_out(1, "writing metadata for " + package["name"])
87        package["URL"] = [urlparse.urljoin(baseurl, package["url"])]
88        (hash, tempfn) = ravenlib.package.storkpackage.package_metadata_dict_get_hash(package, True)
89        outfn = os.path.join(destmetadir, hash)
90        shutil.move(tempfn, outfn)
91        package["_metadatahash"] = hash
92
93    print "writing tpfile"
94    write_tpfile(packages, os.path.join(desttpdir, username + ".tpfile.import"))
95
96 def finalize(username, privkeyfilename, desttpdir):
97    infilename = os.path.join(desttpdir, username + ".tpfile.import")
98    infile = file(infilename,"r")
99    lines = infile.readlines()
100
101    outfilename = os.path.join(desttpdir, username + ".tpfile")
102    outfile = file(outfilename,"w")
103
104    outfile.write('<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>\n')
105    outfile.write("<TRUSTEDPACKAGES>\n")
106    outfile.writelines(lines)
107    outfile.write("</TRUSTEDPACKAGES>\n")
108    outfile.close()
109
110    print "calling arizonacrypt.XML_sign_file on", outfilename
111    print "pkey", privkeyfilename
112
113    arizonacrypt.XML_sign_file_using_privatekey_fn(outfilename, privkeyfilename, privatekey_pass=None, timestamp=None, duration=None)
114
115    # this would be the old way...
116    #publickey_compat = arizonacrypt.PrivateKey(file=privkeyfilename, password=None).get_public_key().string
117    #publickey_compat = re.sub('/','_',publickey_compat)
118    #publickey_compat = publickey_compat.strip('=')
119
120    # this would be the new way...
121    publickey_hash = arizonacrypt.PrivateKey(file=privkeyfilename, password=None).get_public_key().hash
122
123    signedname = os.path.join(desttpdir, username + "." + publickey_hash + ".tpfile")
124
125    os.rename(outfilename, signedname)
126
127    print "signedname", signedname
128
129
130 def main(args):
131    """ TODO comment """
132
133    if len(args)<2:
134        arizonareport.send_error(0, "Syntax: yumimport.py <baseurl> <primary_xml_filename>")
135        sys.exit(-1)
136
137    username = arizonaconfig.get_option("importusername")
138    privkeyfilename = arizonaconfig.get_option("privkeyfile")
139    destmetadir = arizonaconfig.get_option("metadatadir")
140    desttpdir = arizonaconfig.get_option("tpfiledir")
141
142    print "      username:", username
143    print "private key fn:", privkeyfilename
144    print "  dest metadir:", destmetadir
145    print "    dest tpdir:", desttpdir
146
147
148    baseurl = args[0]
149
150    for filename in args[1:]:
151        filelists_filename = filename.replace(".xml", "_fl.xml")
152        importfile(baseurl, filename, filelists_filename, username, destmetadir, desttpdir)
153
154    finalize(username, privkeyfilename, desttpdir)
155
156 if __name__ == "__main__":
157    try:
158       # use error reporting tool
159       # storkerror.init_error_reporting("yumimport.py")
160
161       # get command line and config file options
162       args = arizonaconfig.init_options("yumimport.py", configfile_optvar="configfile", version=storkversion.VERREL)
163
164       main(args)
165    except KeyboardInterrupt:
166       arizonareport.send_out(0, "Exiting via keyboard interrupt...")
167       sys.exit(0)