declare distro f22 that is used in sliceimage-omf.pkgs
[build.git] / pkgs.py
1 #!/usr/bin/python
2 #
3 # This is a replacement for the formerly bash-written function pl_parsePkgs () 
4
5 # Usage: $0  [-a arch] default_arch keyword fcdistro pldistro pkgs-file[..s]
6 # default_arch is $pl_DISTRO_ARCH, but can be overridden
7 #
8
9 #################### original language was (in this example, keyword=package)
10 ## to add to all distros
11 # package: p1 p2
12 ## to add in one distro
13 # package+f12: p1 p2
14 ## to remove in one distro
15 # package-f10: p1 p2
16 #################### replacement language
17 ## add in distro f10
18 # +package=f10: p1 p2
19 # or simply
20 # package=f10: p1 p2
21 ## add in fedora distros starting with f10
22 # +package>=f10: p1 p2
23 # or simply
24 # package>=f10: p1 p2
25 ## ditto but remove instead
26 # -package=centos5: p1 p2
27 # -package<=centos5: p1 p2
28
29 import sys
30 from sys import stderr
31 from optparse import OptionParser
32 import re
33
34 default_arch='x86_64'
35 known_arch = ['i386', 'i686', 'x86_64']
36 default_fcdistro='f14'
37 known_fcdistros = [ 'centos5','centos6',
38                     'f8', 'f10','f12', 'f14', 'f16', 'f18','f20','f22',
39                     'sl6', 
40                     # debians
41                     'squeeze','wheezy','jessie',
42                     # ubuntus
43                     'oneiric', 'precise', 'quantal', 'raring', 'saucy' ]
44 default_pldistro='onelab'
45
46 known_keywords=['groupname', 'groupdesc', 
47                 'nodeyumexclude', 'plcyumexclude', 
48                 'yumexclude', 'package', 'group', 'precious', 'junk', 'mirror', ]
49
50
51 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
52 re_ident='[a-z]+'
53
54 class PkgsParser:
55
56     def __init__ (self,arch,fcdistro,pldistro,keyword,inputs,options):
57         self.arch=arch
58         self.fcdistro=fcdistro
59         self.pldistro=pldistro
60         self.keyword=keyword
61         self.inputs=inputs
62         # for verbose, new_line, and the like
63         self.options=options
64         ok=False
65         for known in known_fcdistros:
66             if fcdistro == known:
67                 try:
68                     (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
69                 # debian-like names can't use numbering
70                 except:
71                     distro=fcdistro
72                     version=0
73                 ok=True
74         if ok:
75             self.distro=distro
76             self.version=int(version)
77         else:
78             print >> stderr, 'unrecognized fcdistro', fcdistro
79             sys.exit(1)
80
81     # qualifier is either '>=','<=', or '='
82     def match (self, qualifier, version):
83         if qualifier == '=':
84             return self.version == version
85         elif qualifier == '>=':
86             return self.version >= version
87         elif qualifier == '<=':
88             return self.version <= version
89         else:
90             raise Exception, 'Internal error - unexpected qualifier %r' % qualifier
91
92     m_comment=re.compile('\A\s*#')
93     m_blank=re.compile('\A\s*\Z')
94
95     m_ident=re.compile('\A'+re_ident+'\Z')
96     re_qualified = '\s*'
97     re_qualified += '(?P<plus_minus>[+-]?)'
98     re_qualified += '\s*'
99     re_qualified += '(?P<keyword>%s)'%re_ident
100     re_qualified += '\s*'
101     re_qualified += '(?P<qualifier>>=|<=|=)'
102     re_qualified += '\s*'
103     re_qualified += '(?P<fcdistro>%s[0-9]+)'%re_ident
104     re_qualified += '\s*'
105     m_qualified = re.compile('\A%s\Z'%re_qualified)
106
107     re_old = '[a-z]+[+-][a-z]+[0-9]+'
108     m_old = re.compile ('\A%s\Z'%re_old)
109     
110     # returns a tuple (included,excluded)
111     def parse (self,filename):
112         ok=True
113         included=[]
114         excluded=[]
115         lineno=0
116         try:
117             for line in file(filename).readlines():
118                 lineno += 1
119                 line=line.strip()
120                 if self.m_comment.match(line) or self.m_blank.match(line):
121                     continue
122                 try:
123                     [lefts,rights] = line.split(':',1)
124                     for left in lefts.split():
125                         ########## single ident
126                         if self.m_ident.match(left):
127                             if left not in known_keywords:
128                                 raise Exception,"Unknown keyword %r"%left
129                             elif left == self.keyword:
130                                 included += rights.split()
131                         else:
132                             m=self.m_qualified.match(left)
133                             if m:
134                                 (plus_minus,kw,qual,fcdistro) = m.groups()
135                                 if kw not in known_keywords:
136                                     raise Exception,"Unknown keyword in %r"%left
137                                 if fcdistro not in known_fcdistros:
138                                     raise Exception, 'Unknown fcdistro %r'%fcdistro
139                                 # skip if another keyword
140                                 if kw != self.keyword: continue
141                                 # does this fcdistro match ?
142                                 (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
143                                 version = int (version)
144                                 # skip if another distro family
145                                 if distro != self.distro: continue
146                                 # skip if the qualifier does not fit
147                                 if not self.match (qual, version): 
148                                     if self.options.verbose: print >> stderr,'%s:%d:qualifer %s does not apply'%(filename,lineno,left)
149                                     continue
150                                 # we're in, let's add (default) or remove (if plus_minus is minus)
151                                 if plus_minus == '-':
152                                     if self.options.verbose: print >> stderr,'%s:%d: from %s, excluding %r'%(filename,lineno,left,rights)
153                                     excluded += rights.split()
154                                 else:
155                                     if self.options.verbose: print >> stderr,'%s:%d: from %s, including %r'%(filename,lineno,left,rights)
156                                     included += rights.split()
157                             elif self.m_old.match(left):
158                                 raise Exception,'Old-fashioned syntax not supported anymore %r'%left
159                             else:
160                                 raise Exception,'error in left expression %r'%left
161                                 
162                 except Exception,e:
163                     ok=False
164                     print >> stderr, "%s:%d:syntax error: %r"%(filename,lineno,e)
165         except Exception,e:
166             ok=False
167             print >> stderr, 'Could not parse file',filename,e
168         return (ok,included,excluded)
169
170     def run (self):
171         ok=True
172         included=[]
173         excluded=[]
174         for input in self.inputs:
175             (o,i,e) = self.parse (input)
176             included += i
177             excluded += e
178             ok = ok and o
179         # avoid set operations that would not preserve order
180         results = [ x for x in included if x not in excluded ]
181         
182         results = [ x.replace('@arch@',self.arch).\
183                         replace('@fcdistro@',self.fcdistro).\
184                         replace('@pldistro@',self.pldistro) for x in results]
185         if self.options.sort_results:
186             results.sort()
187         # default is space-separated
188         if not self.options.new_line:
189             print " ".join(results)
190         # but for tests results are printed each on a line
191         else:
192             for result in results : print result
193         return ok
194
195 def main ():
196     usage="Usage: %prog [options] keyword input[...]"
197     parser=OptionParser (usage=usage)
198     parser.add_option ('-a','--arch',dest='arch',action='store',default=default_arch,
199                        help='target arch, e.g. i386 or x86_64, default=%s'%default_arch)
200     parser.add_option ('-f','--fcdistro',dest='fcdistro',action='store', default=default_fcdistro,
201                        help='fcdistro, e.g. f12 or centos5')
202     parser.add_option ('-d','--pldistro',dest='pldistro',action='store', default=default_pldistro,
203                        help='pldistro, e.g. onelab or planetlab')
204     parser.add_option ('-v', '--verbose',dest='verbose',action='store_true',default=False,
205                        help='verbose when using qualifiers')
206     parser.add_option ('-n', '--new-line',dest='new_line',action='store_true',default=False,
207                        help='print outputs separated with newlines rather than with a space')
208     parser.add_option ('-u', '--no-sort',dest='sort_results',default=True,action='store_false',
209                        help='keep results in the same order as in the inputs')
210     (options,args) = parser.parse_args()
211     
212     if len(args) <=1 :
213         parser.print_help(file=stderr)
214         sys.exit(1)
215     keyword=args[0]
216     inputs=args[1:]
217     if not options.arch in known_arch:
218         print >> stderr, 'Unsupported arch',options.arch
219         parser.print_help(file=stderr)
220         sys.exit(1)
221     if options.arch == 'i686': options.arch='i386'
222     if not options.fcdistro in known_fcdistros:
223         print >> stderr, 'Unsupported fcdistro',options.fcdistro
224         parser.print_help(file=stderr)
225         sys.exit(1)
226
227     pkgs = PkgsParser (options.arch,options.fcdistro,options.pldistro,keyword,inputs,options)
228
229     if pkgs.run():
230         sys.exit(0)
231     else:
232         sys.exit(1)
233
234 if __name__ == '__main__':
235     if main():
236         sys.exit(0)
237     else:
238         sys.exit(1)