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
56 default_pldistro = 'onelab'
59 'group', 'groupname', 'groupdesc',
60 'package', 'pip', 'gem',
61 'nodeyumexclude', 'plcyumexclude', 'yumexclude',
62 'precious', 'junk', 'mirror',
66 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
71 def __init__(self, arch, fcdistro, pldistro, keyword, inputs, options):
73 self.fcdistro = fcdistro
74 self.pldistro = pldistro
75 self.keyword = keyword
77 # for verbose, new_line, and the like
78 self.options = options
80 for known in known_fcdistros:
83 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
84 # debian-like names can't use numbering
91 self.version = int(version)
93 print('unrecognized fcdistro', fcdistro, file=stderr)
96 # qualifier is either '>=','<=', or '='
97 def match(self, qualifier, version):
99 return self.version == version
100 elif qualifier == '>=':
101 return self.version >= version
102 elif qualifier == '<=':
103 return self.version <= version
106 'Internal error - unexpected qualifier {}'.format(qualifier))
108 m_comment = re.compile(r'\A\s*#')
109 m_blank = re.compile(r'\A\s*\Z')
111 m_ident = re.compile(r'\A'+re_ident+r'\Z')
112 re_qualified = r'\s*'
113 re_qualified += r'(?P<plus_minus>[+-]?)'
114 re_qualified += r'\s*'
115 re_qualified += r'(?P<keyword>{re_ident})'.format(re_ident=re_ident)
116 re_qualified += r'\s*'
117 re_qualified += r'(?P<qualifier>>=|<=|=)'
118 re_qualified += r'\s*'
119 re_qualified += r'(?P<fcdistro>{re_ident}[0-9]+)'.format(re_ident=re_ident)
120 re_qualified += r'\s*'
121 m_qualified = re.compile(r'\A{re_qualified}\Z'.format(re_qualified=re_qualified))
123 re_old = '[a-z]+[+-][a-z]+[0-9]+'
124 m_old = re.compile(r'\A{}\Z'.format(re_old))
126 # returns a tuple (included, excluded)
127 def parse(self, filename):
132 with open(filename) as feed:
133 for lineno, line in enumerate(feed, 1):
135 if self.m_comment.match(line) or self.m_blank.match(line):
138 lefts, rights = line.split(':', 1)
139 for left in lefts.split():
140 ########## single ident
141 if self.m_ident.match(left):
142 if left not in known_keywords:
143 raise Exception("Unknown keyword {left}".format(**locals()))
144 elif left == self.keyword:
145 included += rights.split()
147 m = self.m_qualified.match(left)
149 (plus_minus, kw, qual, fcdistro) = m.groups()
150 if kw not in known_keywords:
151 raise Exception("Unknown keyword in {left}".format(**locals()))
152 if fcdistro not in known_fcdistros:
153 raise Exception('Unknown fcdistro {fcdistro}'.format(**locals()))
154 # skip if another keyword
155 if kw != self.keyword: continue
156 # does this fcdistro match ?
157 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
158 version = int (version)
159 # skip if another distro family
160 if distro != self.distro: continue
161 # skip if the qualifier does not fit
162 if not self.match (qual, version):
163 if self.options.verbose:
164 print('{filename}:{lineno}:qualifer {left} does not apply'
165 .format(**locals()), file=stderr)
167 # we're in, let's add (default) or remove (if plus_minus is minus)
168 if plus_minus == '-':
169 if self.options.verbose:
170 print('{filename}:{lineno}: from {left}, excluding {rights}'
171 .format(**locals()), file=stderr)
172 excluded += rights.split()
174 if self.options.verbose:
175 print('{filename}:{lineno}: from {left}, including {rights}'\
176 .format(**locals()), file=stderr)
177 included += rights.split()
178 elif self.m_old.match(left):
179 raise Exception('Old-fashioned syntax not supported anymore {left}'.\
182 raise Exception('error in left expression {left}'.format(**locals()))
184 except Exception as e:
186 print("{filename}:{lineno}:syntax error: {e}".format(**locals()), file=stderr)
187 except Exception as exc:
189 print('Could not parse file', filename, exc, file=stderr)
190 return (ok, included, excluded)
196 for input in self.inputs:
197 (o, i, e) = self.parse (input)
201 # avoid set operations that would not preserve order
202 results = [x for x in included if x not in excluded]
204 results = [x.replace('@arch@', self.arch)
205 .replace('@fcdistro@', self.fcdistro)
206 .replace('@pldistro@', self.pldistro) for x in results]
207 if self.options.sort_results:
209 # default is space-separated
210 if not self.options.new_line:
211 print(" ".join(results))
212 # but for tests results are printed each on a line
214 for result in results:
219 usage = "Usage: %prog [options] keyword input[...]"
220 parser = OptionParser(usage=usage)
222 '-a', '--arch', dest='arch', action='store', default=default_arch,
223 help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch))
225 '-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro,
226 help='fcdistro, e.g. f12 or centos5')
228 '-d', '--pldistro', dest='pldistro', action='store', default=default_pldistro,
229 help='pldistro, e.g. onelab or planetlab')
231 '-v', '--verbose', dest='verbose', action='store_true', default=False,
232 help='verbose when using qualifiers')
234 '-n', '--new-line', dest='new_line', action='store_true', default=False,
235 help='print outputs separated with newlines rather than with a space')
237 '-u', '--no-sort', dest='sort_results', default=True, action='store_false',
238 help='keep results in the same order as in the inputs')
239 (options, args) = parser.parse_args()
242 parser.print_help(file=stderr)
246 if options.arch not in known_archs:
247 print('Unsupported arch', options.arch, file=stderr)
248 parser.print_help(file=stderr)
250 if options.arch == 'i686':
251 options.arch = 'i386'
252 if options.fcdistro not in known_fcdistros:
253 print('Unsupported fcdistro', options.fcdistro, file=stderr)
254 parser.print_help(file=stderr)
257 pkgs = PkgsParser(options.arch, options.fcdistro, options.pldistro,
258 keyword, inputs, options)
265 if __name__ == '__main__':