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 = 'f22'
41 'f14', 'f18', 'f20', 'f21', 'f22',
46 'precise', # 12.04 LTS
52 default_pldistro='onelab'
55 'group', 'groupname', 'groupdesc',
56 'package', 'pip', 'gem',
57 'nodeyumexclude', 'plcyumexclude', 'yumexclude',
58 'precious', 'junk', 'mirror',
62 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
67 def __init__(self, arch, fcdistro, pldistro, keyword, inputs, options):
69 self.fcdistro = fcdistro
70 self.pldistro = pldistro
71 self.keyword = keyword
73 # for verbose, new_line, and the like
74 self.options = options
76 for known in known_fcdistros:
79 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
80 # debian-like names can't use numbering
87 self.version = int(version)
89 print('unrecognized fcdistro', fcdistro, file=stderr)
92 # qualifier is either '>=','<=', or '='
93 def match (self, qualifier, version):
95 return self.version == version
96 elif qualifier == '>=':
97 return self.version >= version
98 elif qualifier == '<=':
99 return self.version <= version
101 raise Exception('Internal error - unexpected qualifier {qualifier}'.format(**locals()))
103 m_comment = re.compile('\A\s*#')
104 m_blank = re.compile('\A\s*\Z')
106 m_ident = re.compile('\A'+re_ident+'\Z')
108 re_qualified += '(?P<plus_minus>[+-]?)'
109 re_qualified += '\s*'
110 re_qualified += '(?P<keyword>{re_ident})'.format(re_ident=re_ident)
111 re_qualified += '\s*'
112 re_qualified += '(?P<qualifier>>=|<=|=)'
113 re_qualified += '\s*'
114 re_qualified += '(?P<fcdistro>{re_ident}[0-9]+)'.format(re_ident=re_ident)
115 re_qualified += '\s*'
116 m_qualified = re.compile('\A{re_qualified}\Z'.format(**locals()))
118 re_old = '[a-z]+[+-][a-z]+[0-9]+'
119 m_old = re.compile('\A{re_old}\Z'.format(**locals()))
121 # returns a tuple (included, excluded)
122 def parse(self, filename):
128 for line in file(filename).readlines():
131 if self.m_comment.match(line) or self.m_blank.match(line):
134 lefts, rights = line.split(':', 1)
135 for left in lefts.split():
136 ########## single ident
137 if self.m_ident.match(left):
138 if left not in known_keywords:
139 raise Exception("Unknown keyword {left}".format(**locals()))
140 elif left == self.keyword:
141 included += rights.split()
143 m = self.m_qualified.match(left)
145 (plus_minus, kw, qual, fcdistro) = m.groups()
146 if kw not in known_keywords:
147 raise Exception("Unknown keyword in {left}".format(**locals()))
148 if fcdistro not in known_fcdistros:
149 raise Exception('Unknown fcdistro {fcdistro}'.format(**locals()))
150 # skip if another keyword
151 if kw != self.keyword: continue
152 # does this fcdistro match ?
153 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
154 version = int (version)
155 # skip if another distro family
156 if distro != self.distro: continue
157 # skip if the qualifier does not fit
158 if not self.match (qual, version):
159 if self.options.verbose:
160 print('{filename}:{lineno}:qualifer {left} does not apply'
161 .format(**locals()), file=stderr)
163 # we're in, let's add (default) or remove (if plus_minus is minus)
164 if plus_minus == '-':
165 if self.options.verbose:
166 print('{filename}:{lineno}: from {left}, excluding {rights}'
167 .format(**locals()), file=stderr)
168 excluded += rights.split()
170 if self.options.verbose:
171 print('{filename}:{lineno}: from {left}, including {rights}'\
172 .format(**locals()), file=stderr)
173 included += rights.split()
174 elif self.m_old.match(left):
175 raise Exception('Old-fashioned syntax not supported anymore {left}'.\
178 raise Exception('error in left expression {left}'.format(**locals()))
180 except Exception as e:
182 print("{filename}:{lineno}:syntax error: {e}".format(**locals()), file=stderr)
183 except Exception as e:
185 print('Could not parse file', filename, e, file=stderr)
186 return (ok, included, excluded)
192 for input in self.inputs:
193 (o, i, e) = self.parse (input)
197 # avoid set operations that would not preserve order
198 results = [ x for x in included if x not in excluded ]
200 results = [ x.replace('@arch@', self.arch).\
201 replace('@fcdistro@', self.fcdistro).\
202 replace('@pldistro@', self.pldistro) for x in results]
203 if self.options.sort_results:
205 # default is space-separated
206 if not self.options.new_line:
207 print(" ".join(results))
208 # but for tests results are printed each on a line
210 for result in results:
215 usage = "Usage: %prog [options] keyword input[...]"
216 parser = OptionParser (usage=usage)
217 parser.add_option ('-a', '--arch', dest='arch', action='store', default=default_arch,
218 help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch))
219 parser.add_option ('-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro,
220 help='fcdistro, e.g. f12 or centos5')
221 parser.add_option ('-d', '--pldistro', dest='pldistro', action='store', default=default_pldistro,
222 help='pldistro, e.g. onelab or planetlab')
223 parser.add_option ('-v', '--verbose', dest='verbose', action='store_true', default=False,
224 help='verbose when using qualifiers')
225 parser.add_option ('-n', '--new-line', dest='new_line', action='store_true', default=False,
226 help='print outputs separated with newlines rather than with a space')
227 parser.add_option ('-u', '--no-sort', dest='sort_results', default=True, action='store_false',
228 help='keep results in the same order as in the inputs')
229 (options, args) = parser.parse_args()
232 parser.print_help(file=stderr)
236 if not options.arch in known_arch:
237 print('Unsupported arch', options.arch, file=stderr)
238 parser.print_help(file=stderr)
240 if options.arch == 'i686':
242 if not options.fcdistro in known_fcdistros:
243 print('Unsupported fcdistro', options.fcdistro, file=stderr)
244 parser.print_help(file=stderr)
247 pkgs = PkgsParser(options.arch, options.fcdistro, options.pldistro, keyword, inputs, options)
254 if __name__ == '__main__':