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',
58 default_pldistro = 'onelab'
61 'group', 'groupname', 'groupdesc',
62 'package', 'pip', 'gem',
63 'nodeyumexclude', 'plcyumexclude', 'yumexclude',
64 'precious', 'junk', 'mirror',
68 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
73 def __init__(self, arch, fcdistro, pldistro, keyword, inputs, options):
75 self.fcdistro = fcdistro
76 self.pldistro = pldistro
77 self.keyword = keyword
79 # for verbose, new_line, and the like
80 self.options = options
82 for known in known_fcdistros:
85 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
86 # debian-like names can't use numbering
93 self.version = int(version)
95 print('unrecognized fcdistro', fcdistro, file=stderr)
98 # qualifier is either '>=','<=', or '='
99 def match(self, qualifier, version):
101 return self.version == version
102 elif qualifier == '>=':
103 return self.version >= version
104 elif qualifier == '<=':
105 return self.version <= version
108 'Internal error - unexpected qualifier {}'.format(qualifier))
110 m_comment = re.compile(r'\A\s*#')
111 m_blank = re.compile(r'\A\s*\Z')
113 m_ident = re.compile(r'\A'+re_ident+r'\Z')
114 re_qualified = r'\s*'
115 re_qualified += r'(?P<plus_minus>[+-]?)'
116 re_qualified += r'\s*'
117 re_qualified += r'(?P<keyword>{re_ident})'.format(re_ident=re_ident)
118 re_qualified += r'\s*'
119 re_qualified += r'(?P<qualifier>>=|<=|=)'
120 re_qualified += r'\s*'
121 re_qualified += r'(?P<fcdistro>{re_ident}[0-9]+)'.format(re_ident=re_ident)
122 re_qualified += r'\s*'
123 m_qualified = re.compile(r'\A{re_qualified}\Z'.format(re_qualified=re_qualified))
125 re_old = '[a-z]+[+-][a-z]+[0-9]+'
126 m_old = re.compile(r'\A{}\Z'.format(re_old))
128 # returns a tuple (included, excluded)
129 def parse(self, filename):
134 with open(filename) as feed:
135 for lineno, line in enumerate(feed, 1):
137 if self.m_comment.match(line) or self.m_blank.match(line):
140 lefts, rights = line.split(':', 1)
141 for left in lefts.split():
142 ########## single ident
143 if self.m_ident.match(left):
144 if left not in known_keywords:
145 raise Exception("Unknown keyword {left}".format(**locals()))
146 elif left == self.keyword:
147 included += rights.split()
149 m = self.m_qualified.match(left)
151 (plus_minus, kw, qual, fcdistro) = m.groups()
152 if kw not in known_keywords:
153 raise Exception("Unknown keyword in {left}".format(**locals()))
154 if fcdistro not in known_fcdistros:
155 raise Exception('Unknown fcdistro {fcdistro}'.format(**locals()))
156 # skip if another keyword
157 if kw != self.keyword: continue
158 # does this fcdistro match ?
159 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
160 version = int (version)
161 # skip if another distro family
162 if distro != self.distro: continue
163 # skip if the qualifier does not fit
164 if not self.match (qual, version):
165 if self.options.verbose:
166 print('{filename}:{lineno}:qualifer {left} does not apply'
167 .format(**locals()), file=stderr)
169 # we're in, let's add (default) or remove (if plus_minus is minus)
170 if plus_minus == '-':
171 if self.options.verbose:
172 print('{filename}:{lineno}: from {left}, excluding {rights}'
173 .format(**locals()), file=stderr)
174 excluded += rights.split()
176 if self.options.verbose:
177 print('{filename}:{lineno}: from {left}, including {rights}'\
178 .format(**locals()), file=stderr)
179 included += rights.split()
180 elif self.m_old.match(left):
181 raise Exception('Old-fashioned syntax not supported anymore {left}'.\
184 raise Exception('error in left expression {left}'.format(**locals()))
186 except Exception as e:
188 print("{filename}:{lineno}:syntax error: {e}".format(**locals()), file=stderr)
189 except Exception as exc:
191 print('Could not parse file', filename, exc, file=stderr)
192 return (ok, included, excluded)
198 for input in self.inputs:
199 (o, i, e) = self.parse (input)
203 # avoid set operations that would not preserve order
204 results = [x for x in included if x not in excluded]
206 results = [x.replace('@arch@', self.arch)
207 .replace('@fcdistro@', self.fcdistro)
208 .replace('@pldistro@', self.pldistro) for x in results]
209 if self.options.sort_results:
211 # default is space-separated
212 if not self.options.new_line:
213 print(" ".join(results))
214 # but for tests results are printed each on a line
216 for result in results:
221 usage = "Usage: %prog [options] keyword input[...]"
222 parser = OptionParser(usage=usage)
224 '-a', '--arch', dest='arch', action='store', default=default_arch,
225 help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch))
227 '-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro,
228 help='fcdistro, e.g. f12 or centos5')
230 '-d', '--pldistro', dest='pldistro', action='store', default=default_pldistro,
231 help='pldistro, e.g. onelab or planetlab')
233 '-v', '--verbose', dest='verbose', action='store_true', default=False,
234 help='verbose when using qualifiers')
236 '-n', '--new-line', dest='new_line', action='store_true', default=False,
237 help='print outputs separated with newlines rather than with a space')
239 '-u', '--no-sort', dest='sort_results', default=True, action='store_false',
240 help='keep results in the same order as in the inputs')
241 (options, args) = parser.parse_args()
244 parser.print_help(file=stderr)
248 if options.arch not in known_archs:
249 print('Unsupported arch', options.arch, file=stderr)
250 parser.print_help(file=stderr)
252 if options.arch == 'i686':
253 options.arch = 'i386'
254 if options.fcdistro not in known_fcdistros:
255 print('Unsupported fcdistro', options.fcdistro, file=stderr)
256 parser.print_help(file=stderr)
259 pkgs = PkgsParser(options.arch, options.fcdistro, options.pldistro,
260 keyword, inputs, options)
267 if __name__ == '__main__':