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 from __future__ import print_function
32 from sys import stderr
33 from optparse import OptionParser
37 known_arch = ['i386', 'i686', 'x86_64']
38 default_fcdistro = 'f27'
41 # oldies but we have references to that in the pkgs files
42 'f8', 'f10', 'f12', 'f16',
43 # these ones are still relevant
44 'f14', 'f18', 'f20', 'f21', 'f22', 'f23', 'f24', 'f25', 'f27',
49 'precise', # 12.04 LTS
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
105 raise Exception('Internal error - unexpected qualifier {qualifier}'.format(**locals()))
107 m_comment = re.compile('\A\s*#')
108 m_blank = re.compile('\A\s*\Z')
110 m_ident = re.compile('\A'+re_ident+'\Z')
112 re_qualified += '(?P<plus_minus>[+-]?)'
113 re_qualified += '\s*'
114 re_qualified += '(?P<keyword>{re_ident})'.format(re_ident=re_ident)
115 re_qualified += '\s*'
116 re_qualified += '(?P<qualifier>>=|<=|=)'
117 re_qualified += '\s*'
118 re_qualified += '(?P<fcdistro>{re_ident}[0-9]+)'.format(re_ident=re_ident)
119 re_qualified += '\s*'
120 m_qualified = re.compile('\A{re_qualified}\Z'.format(**locals()))
122 re_old = '[a-z]+[+-][a-z]+[0-9]+'
123 m_old = re.compile('\A{re_old}\Z'.format(**locals()))
125 # returns a tuple (included, excluded)
126 def parse(self, filename):
132 for line in file(filename).readlines():
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 e:
189 print('Could not parse file', filename, e, 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)
221 parser.add_option ('-a', '--arch', dest='arch', action='store', default=default_arch,
222 help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch))
223 parser.add_option ('-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro,
224 help='fcdistro, e.g. f12 or centos5')
225 parser.add_option ('-d', '--pldistro', dest='pldistro', action='store', default=default_pldistro,
226 help='pldistro, e.g. onelab or planetlab')
227 parser.add_option ('-v', '--verbose', dest='verbose', action='store_true', default=False,
228 help='verbose when using qualifiers')
229 parser.add_option ('-n', '--new-line', dest='new_line', action='store_true', default=False,
230 help='print outputs separated with newlines rather than with a space')
231 parser.add_option ('-u', '--no-sort', dest='sort_results', default=True, action='store_false',
232 help='keep results in the same order as in the inputs')
233 (options, args) = parser.parse_args()
236 parser.print_help(file=stderr)
240 if not options.arch in known_arch:
241 print('Unsupported arch', options.arch, file=stderr)
242 parser.print_help(file=stderr)
244 if options.arch == 'i686':
246 if not options.fcdistro in known_fcdistros:
247 print('Unsupported fcdistro', options.fcdistro, file=stderr)
248 parser.print_help(file=stderr)
251 pkgs = PkgsParser(options.arch, options.fcdistro, options.pldistro, keyword, inputs, options)
258 if __name__ == '__main__':