3 # This is a replacement for the formerly bash-written function pl_parsePkgs ()
5 # Usage: $0 [-a arch] default_arch keyword fcdistro pldistro pkgs-file[..s]
6 # default_arch is $pl_DISTRO_ARCH, but can be overridden
9 #################### original language was (in this example, keyword=package)
10 ## to add to all distros
12 ## to add in one distro
14 ## to remove in one distro
16 #################### replacement language
21 ## add in fedora distros starting with f10
22 # +package>=f10: p1 p2
25 ## ditto but remove instead
26 # -package=centos5: p1 p2
27 # -package<=centos5: p1 p2
29 # pylint: disable=c0111
33 from sys import stderr
34 from optparse import OptionParser
37 default_arch = 'x86_64'
38 known_archs = ['i386', 'i686', 'x86_64']
39 default_fcdistro = 'f33'
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',
59 default_pldistro = 'onelab'
62 'group', 'groupname', 'groupdesc',
63 'package', 'pip', 'gem',
64 'nodeyumexclude', 'plcyumexclude', 'yumexclude',
65 'precious', 'junk', 'mirror',
69 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
74 def __init__(self, arch, fcdistro, pldistro, keyword, inputs, options):
76 self.fcdistro = fcdistro
77 self.pldistro = pldistro
78 self.keyword = keyword
80 # for verbose, new_line, and the like
81 self.options = options
83 for known in known_fcdistros:
86 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
87 # debian-like names can't use numbering
94 self.version = int(version)
96 print('unrecognized fcdistro', fcdistro, file=stderr)
99 # qualifier is either '>=','<=', or '='
100 def match(self, qualifier, version):
102 return self.version == version
103 elif qualifier == '>=':
104 return self.version >= version
105 elif qualifier == '<=':
106 return self.version <= version
109 'Internal error - unexpected qualifier {}'.format(qualifier))
111 m_comment = re.compile(r'\A\s*#')
112 m_blank = re.compile(r'\A\s*\Z')
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))
126 re_old = '[a-z]+[+-][a-z]+[0-9]+'
127 m_old = re.compile(r'\A{}\Z'.format(re_old))
129 # returns a tuple (included, excluded)
130 def parse(self, filename):
135 with open(filename) as feed:
136 for lineno, line in enumerate(feed, 1):
138 if self.m_comment.match(line) or self.m_blank.match(line):
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()
150 m = self.m_qualified.match(left)
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)
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()
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}'.\
185 raise Exception('error in left expression {left}'.format(**locals()))
187 except Exception as e:
189 print("{filename}:{lineno}:syntax error: {e}".format(**locals()), file=stderr)
190 except Exception as exc:
192 print('Could not parse file', filename, exc, file=stderr)
193 return (ok, included, excluded)
199 for input in self.inputs:
200 (o, i, e) = self.parse (input)
204 # avoid set operations that would not preserve order
205 results = [x for x in included if x not in excluded]
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:
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
217 for result in results:
222 usage = "Usage: %prog [options] keyword input[...]"
223 parser = OptionParser(usage=usage)
225 '-a', '--arch', dest='arch', action='store', default=default_arch,
226 help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch))
228 '-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro,
229 help='fcdistro, e.g. f12 or centos5')
231 '-d', '--pldistro', dest='pldistro', action='store', default=default_pldistro,
232 help='pldistro, e.g. onelab or planetlab')
234 '-v', '--verbose', dest='verbose', action='store_true', default=False,
235 help='verbose when using qualifiers')
237 '-n', '--new-line', dest='new_line', action='store_true', default=False,
238 help='print outputs separated with newlines rather than with a space')
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()
245 parser.print_help(file=stderr)
249 if options.arch not in known_archs:
250 print('Unsupported arch', options.arch, file=stderr)
251 parser.print_help(file=stderr)
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)
260 pkgs = PkgsParser(options.arch, options.fcdistro, options.pldistro,
261 keyword, inputs, options)
268 if __name__ == '__main__':