quick $ dirty f35 for create-cm.sh
[build.git] / pkgs.py
1 #!/usr/bin/env python3
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 # pylint: disable=c0111
30
31
32 import sys
33 from sys import stderr
34 from optparse import OptionParser
35 import re
36
37 default_arch = 'x86_64'
38 known_archs = ['i386', 'i686', 'x86_64']
39 default_fcdistro = 'f35'
40 known_fcdistros = [
41     'centos5', 'centos6',
42     # oldies but we have references to that in the pkgs files
43     'f8', 'f10', 'f12', 'f14', 'f16', 'f18', 
44     'f20', 'f21', 'f22', 'f23', 'f24', 'f25', 'f27', 
45     # these ones are still relevant;
46     # f32 is mentioned to be able to use create-vms with that distro
47     # as we're running into issues to build a minimal f33 from a f29 host
48     'f29', 'f31', 'f32', 'f33', 'f35',
49     # scientific linux
50     'sl6',
51     # debians
52     'wheezy', 'jessie',
53     # ubuntus
54     'trusty',  # 14.04 LTS
55     'xenial',  # 16.04 LTS
56     'bionic',  # 18.04 LTS
57     'focal',   # 20.04 LTS
58 ]
59 default_pldistro = 'onelab'
60
61 known_keywords = [
62     'group', 'groupname', 'groupdesc',
63      'package', 'pip', 'gem',
64     'nodeyumexclude', 'plcyumexclude', 'yumexclude',
65     'precious', 'junk', 'mirror',
66 ]
67
68
69 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
70 re_ident = '[a-z]+'
71
72 class PkgsParser:
73
74     def __init__(self, arch, fcdistro, pldistro, keyword, inputs, options):
75         self.arch = arch
76         self.fcdistro = fcdistro
77         self.pldistro = pldistro
78         self.keyword = keyword
79         self.inputs = inputs
80         # for verbose, new_line, and the like
81         self.options = options
82         ok = False
83         for known in known_fcdistros:
84             if fcdistro == known:
85                 try:
86                     (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
87                 # debian-like names can't use numbering
88                 except:
89                     distro = fcdistro
90                     version = 0
91                 ok = True
92         if ok:
93             self.distro = distro
94             self.version = int(version)
95         else:
96             print('unrecognized fcdistro', fcdistro, file=stderr)
97             sys.exit(1)
98
99     # qualifier is either '>=','<=', or '='
100     def match(self, qualifier, version):
101         if qualifier == '=':
102             return self.version == version
103         elif qualifier == '>=':
104             return self.version >= version
105         elif qualifier == '<=':
106             return self.version <= version
107         else:
108             raise Exception(
109                 'Internal error - unexpected qualifier {}'.format(qualifier))
110
111     m_comment = re.compile(r'\A\s*#')
112     m_blank = re.compile(r'\A\s*\Z')
113
114     m_ident = re.compile(r'\A'+re_ident+r'\Z')
115     re_qualified = r'\s*'
116     re_qualified += r'(?P<plus_minus>[+-]?)'
117     re_qualified += r'\s*'
118     re_qualified += r'(?P<keyword>{re_ident})'.format(re_ident=re_ident)
119     re_qualified += r'\s*'
120     re_qualified += r'(?P<qualifier>>=|<=|=)'
121     re_qualified += r'\s*'
122     re_qualified += r'(?P<fcdistro>{re_ident}[0-9]+)'.format(re_ident=re_ident)
123     re_qualified += r'\s*'
124     m_qualified = re.compile(r'\A{re_qualified}\Z'.format(re_qualified=re_qualified))
125
126     re_old = '[a-z]+[+-][a-z]+[0-9]+'
127     m_old = re.compile(r'\A{}\Z'.format(re_old))
128
129     # returns a tuple (included, excluded)
130     def parse(self, filename):
131         ok = True
132         included = []
133         excluded = []
134         try:
135             with open(filename) as feed:
136                 for lineno, line in enumerate(feed, 1):
137                     line = line.strip()
138                     if self.m_comment.match(line) or self.m_blank.match(line):
139                         continue
140                     try:
141                         lefts, rights = line.split(':', 1)
142                         for left in lefts.split():
143                             ########## single ident
144                             if self.m_ident.match(left):
145                                 if left not in known_keywords:
146                                     raise Exception("Unknown keyword {left}".format(**locals()))
147                                 elif left == self.keyword:
148                                     included += rights.split()
149                             else:
150                                 m = self.m_qualified.match(left)
151                                 if m:
152                                     (plus_minus, kw, qual, fcdistro) = m.groups()
153                                     if kw not in known_keywords:
154                                         raise Exception("Unknown keyword in {left}".format(**locals()))
155                                     if fcdistro not in known_fcdistros:
156                                         raise Exception('Unknown fcdistro {fcdistro}'.format(**locals()))
157                                     # skip if another keyword
158                                     if kw != self.keyword: continue
159                                     # does this fcdistro match ?
160                                     (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
161                                     version = int (version)
162                                     # skip if another distro family
163                                     if distro != self.distro: continue
164                                     # skip if the qualifier does not fit
165                                     if not self.match (qual, version):
166                                         if self.options.verbose:
167                                             print('{filename}:{lineno}:qualifer {left} does not apply'
168                                                   .format(**locals()), file=stderr)
169                                         continue
170                                     # we're in, let's add (default) or remove (if plus_minus is minus)
171                                     if plus_minus == '-':
172                                         if self.options.verbose:
173                                             print('{filename}:{lineno}: from {left}, excluding {rights}'
174                                                   .format(**locals()), file=stderr)
175                                         excluded += rights.split()
176                                     else:
177                                         if self.options.verbose:
178                                             print('{filename}:{lineno}: from {left}, including {rights}'\
179                                                   .format(**locals()), file=stderr)
180                                         included += rights.split()
181                                 elif self.m_old.match(left):
182                                     raise Exception('Old-fashioned syntax not supported anymore {left}'.\
183                                                     format(**locals()))
184                                 else:
185                                     raise Exception('error in left expression {left}'.format(**locals()))
186
187                     except Exception as e:
188                         ok = False
189                         print("{filename}:{lineno}:syntax error: {e}".format(**locals()), file=stderr)
190         except Exception as exc:
191             ok = False
192             print('Could not parse file', filename, exc, file=stderr)
193         return (ok, included, excluded)
194
195     def run (self):
196         ok = True
197         included = []
198         excluded = []
199         for input in self.inputs:
200             (o, i, e) = self.parse (input)
201             included += i
202             excluded += e
203             ok = ok and o
204         # avoid set operations that would not preserve order
205         results = [x for x in included if x not in excluded]
206
207         results = [x.replace('@arch@', self.arch)
208                    .replace('@fcdistro@', self.fcdistro)
209                    .replace('@pldistro@', self.pldistro) for x in results]
210         if self.options.sort_results:
211             results.sort()
212         # default is space-separated
213         if not self.options.new_line:
214             print(" ".join(results))
215         # but for tests results are printed each on a line
216         else:
217             for result in results:
218                 print(result)
219         return ok
220
221 def main ():
222     usage = "Usage: %prog [options] keyword input[...]"
223     parser = OptionParser(usage=usage)
224     parser.add_option(
225         '-a', '--arch', dest='arch', action='store', default=default_arch,
226         help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch))
227     parser.add_option(
228         '-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro,
229         help='fcdistro, e.g. f12 or centos5')
230     parser.add_option(
231         '-d', '--pldistro', dest='pldistro', action='store', default=default_pldistro,
232         help='pldistro, e.g. onelab or planetlab')
233     parser.add_option(
234         '-v', '--verbose', dest='verbose', action='store_true', default=False,
235         help='verbose when using qualifiers')
236     parser.add_option(
237         '-n', '--new-line', dest='new_line', action='store_true', default=False,
238         help='print outputs separated with newlines rather than with a space')
239     parser.add_option(
240         '-u', '--no-sort', dest='sort_results', default=True, action='store_false',
241         help='keep results in the same order as in the inputs')
242     (options, args) = parser.parse_args()
243
244     if len(args) <= 1:
245         parser.print_help(file=stderr)
246         sys.exit(1)
247     keyword = args[0]
248     inputs = args[1:]
249     if options.arch not in known_archs:
250         print('Unsupported arch', options.arch, file=stderr)
251         parser.print_help(file=stderr)
252         sys.exit(1)
253     if options.arch == 'i686':
254         options.arch = 'i386'
255     if options.fcdistro not in known_fcdistros:
256         print('Unsupported fcdistro', options.fcdistro, file=stderr)
257         parser.print_help(file=stderr)
258         sys.exit(1)
259
260     pkgs = PkgsParser(options.arch, options.fcdistro, options.pldistro,
261                       keyword, inputs, options)
262
263     if pkgs.run():
264         sys.exit(0)
265     else:
266         sys.exit(1)
267
268 if __name__ == '__main__':
269     if main():
270         sys.exit(0)
271     else:
272         sys.exit(1)