resetting manifest requested domain to floor
[platform/upstream/createrepo.git] / worker.py
1 #!/usr/bin/python -tt
2
3 import sys
4 import yum
5 import createrepo
6 import os
7 import rpmUtils
8 from optparse import OptionParser
9
10
11 # pass in dir to make tempdirs in
12 # make tempdir for this worker
13 # create 3 files in that tempdir
14 # return how many pkgs
15 # return on stderr where things went to hell
16
17 #TODO - take most of read_in_package from createrepo and duplicate it here
18 # so we can do downloads, etc.
19 # then replace callers of read_in_package with forked callers of this
20 # and reassemble at the end
21
22 def main(args):
23     parser = OptionParser()
24     parser.add_option('--tmpmdpath', default=None, 
25                 help="path where the outputs should be dumped for this worker")
26     parser.add_option("--pkgoptions", default=[], action='append',
27                 help="pkgoptions in the format of key=value")
28     parser.add_option("--quiet", default=False, action='store_true',
29                 help="only output errors and a total")
30     parser.add_option("--verbose", default=False, action='store_true',
31                 help="output errors and a total")
32     parser.add_option("--globalopts", default=[], action='append',
33                 help="general options in the format of key=value")
34
35     
36     opts, pkgs = parser.parse_args(args)
37     external_data = {'_packagenumber': 1}
38     globalopts = {}
39     if not opts.tmpmdpath:
40         print >> sys.stderr, "tmpmdpath required for destination files"
41         sys.exit(1)
42     
43     
44     for strs in opts.pkgoptions:
45         k,v = strs.split('=')
46         if v in ['True', 'true', 'yes', '1', 1]:
47             v = True
48         elif v in ['False', 'false', 'no', '0', 0]:
49             v = False
50         elif v in ['None', 'none', '']:
51             v = None
52         external_data[k] = v
53
54     for strs in opts.globalopts:
55         k,v = strs.split('=')
56         if v in ['True', 'true', 'yes', '1', 1]:
57             v = True
58         elif v in ['False', 'false', 'no', '0', 0]:
59             v = False
60         elif v in ['None', 'none', '']:
61             v = None
62         globalopts[k] = v
63
64     
65     reldir = external_data['_reldir']
66     ts = rpmUtils.transaction.initReadOnlyTransaction()
67     pri = open(opts.tmpmdpath + '/primary.xml' , 'w')
68     fl = open(opts.tmpmdpath  + '/filelists.xml' , 'w')
69     other = open(opts.tmpmdpath  + '/other.xml' , 'w')
70     
71     
72     for pkgfile in pkgs:
73         pkgpath = reldir + '/' + pkgfile
74         if not os.path.exists(pkgpath):
75             print >> sys.stderr, "File not found: %s" % pkgpath
76             continue
77
78         try:
79             if not opts.quiet and opts.verbose:
80                 print "reading %s" % (pkgfile)
81
82             pkg = createrepo.yumbased.CreateRepoPackage(ts, package=pkgpath, 
83                                                         external_data=external_data)
84             pri.write(pkg.xml_dump_primary_metadata())
85             fl.write(pkg.xml_dump_filelists_metadata())
86             other.write(pkg.xml_dump_other_metadata(clog_limit=
87                                             globalopts.get('clog_limit', None)))
88         except yum.Errors.YumBaseError, e:
89             print >> sys.stderr, "Error: %s" % e
90             continue
91         else:
92             external_data['_packagenumber']+=1
93         
94     pri.close()
95     fl.close()
96     other.close()
97     
98 if __name__ == "__main__":
99     main(sys.argv[1:])