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