support architecture and repo substitution
[platform/upstream/kickstarter.git] / kickstarter.py
1 #!/usr/bin/python
2 # Anas Nashif <anas.nashif@intel.com>
3 import yaml,  sys
4
5 import re, os
6 from kickstart import kickstart
7
8 import copy
9 import time
10 import optparse
11 from time import gmtime, strftime
12
13 class KSWriter():
14     def __init__(self,  im, rep, out):
15         self.image_filename = im
16         self.repo_filename = rep
17         self.outdir = out
18         self.image_stream = file(self.image_filename, 'r')
19         self.repo_stream = file(self.repo_filename, 'r')
20         self.extra = {}
21         pass
22     def merge(*input):
23         return list(reduce(set.union, input, set()))
24         
25     def dump(self):
26         print yaml.dump(yaml.load(self.stream))
27         
28         
29     def parse(self, img):
30         print "Creating %s (%s.ks)" %(img['Name'], img['FileName'] )
31         conf = copy.copy(image_meta['Default'])
32         plat = copy.copy(image_meta[img['Platform']])
33         conf.update(plat)
34         conf.update(img)
35         lval = ['Repos', 'Groups', 'PostScripts', 'NoChrootScripts', 'RemovePackages', 'ExtraPackages']
36         lvald = {}
37         for l in lval:
38             full = []
39             if image_meta['Default'].has_key(l) and image_meta['Default'][l]:
40                 full = full + image_meta['Default'][l]                
41             if plat.has_key(l) and plat[l]:
42                 full = full + plat[l]
43             if img.has_key(l) and img[l]:
44                 fll = full + img[l]                    
45             lvald[l] = set(full)
46             #print full
47         conf.update(lvald)
48         #print conf
49         postscript = ""  
50         for scr in conf['PostScripts']:
51             f = open('./custom/scripts/%s.post' %scr, 'r')
52             postscript += f.read()
53             postscript += "\n\n"
54             f.close()
55
56         nochrootscript = ""              
57         for scr in conf['NoChrootScripts']:
58             f = open('./custom/scripts/%s.nochroot' %scr, 'r')
59             nochrootscript += f.read()
60             nochrootscript += "\n\n"
61             f.close()
62
63         ptab = ""
64         if img.has_key("Part"):
65             f = open("./custom/part/%s" %img['Part'] )
66             ptab = f.read()
67             f.close()  
68             
69         conf['Part'] = ptab
70         conf['Post'] = postscript
71         conf['NoChroot'] = nochrootscript
72         return conf
73
74     def process_files(self,  meta,  repos):
75         new_repos = []
76         #print repos
77         #print meta
78         if meta.has_key("Architecture") and  meta['Architecture']:
79             for repo in repos:
80                 r = {}
81                 r['Name'] = repo['Name']
82                 r['Url'] = repo['Url'].replace("@ARCH@", meta['Architecture'])
83                 new_repos.append(r)
84         else:
85             new_repos = repos
86                 
87         nameSpace = {'metadata': meta,  'repos': new_repos}
88         t = kickstart(searchList=[nameSpace])
89         a = str(t)
90         if meta.has_key('FileName') and meta['FileName']:
91             f = open("%s/%s.ks" %( self.outdir, meta['FileName'] ), 'w')
92             f.write(a)
93             f.close()
94
95 if __name__ == '__main__':
96     parser = optparse.OptionParser()
97
98     parser.add_option("-c", "--configs", type="string", dest="configsfile",
99                     help="configuration meta file")
100     parser.add_option("-o", "--outdir", type="string", dest="outdir",
101                     help="outdir")
102     parser.add_option("-r", "--repos", type="string", dest="repofile",
103                     help="repo meta file")
104
105     (options, args) = parser.parse_args()
106
107     if options.configsfile is None or options.repofile is None:
108         print "you need to provide meta files with --configs and --repos"
109         sys.exit(1)
110
111     outdir = ""
112     if options.outdir is None:
113         outdir = "."
114     else:
115         outdir = options.outdir
116
117     ks = KSWriter(options.configsfile, options.repofile, outdir)
118     repo_meta = yaml.load(ks.repo_stream)
119     image_meta = yaml.load(ks.image_stream)
120     r = repo_meta['Repositories']
121     for img in image_meta['Configurations']:
122         conf = ks.parse(img)
123         ks.process_files(conf, r)