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