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_arch = ['i386', 'i686', 'x86_64']
39 default_fcdistro = 'f29'
42 # oldies but we have references to that in the pkgs files
43 'f8', 'f10', 'f12', 'f14', 'f16', 'f18', 'f20', 'f21', 'f22', 'f23', 'f24',
44 # these ones are still relevant
55 default_pldistro = 'onelab'
58 'group', 'groupname', 'groupdesc',
59 'package', 'pip', 'gem',
60 'nodeyumexclude', 'plcyumexclude', 'yumexclude',
61 'precious', 'junk', 'mirror',
65 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
70 def __init__(self, arch, fcdistro, pldistro, keyword, inputs, options):
72 self.fcdistro = fcdistro
73 self.pldistro = pldistro
74 self.keyword = keyword
76 # for verbose, new_line, and the like
77 self.options = options
79 for known in known_fcdistros:
82 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
83 # debian-like names can't use numbering
90 self.version = int(version)
92 print('unrecognized fcdistro', fcdistro, file=stderr)
95 # qualifier is either '>=','<=', or '='
96 def match(self, qualifier, version):
98 return self.version == version
99 elif qualifier == '>=':
100 return self.version >= version
101 elif qualifier == '<=':
102 return self.version <= version
105 'Internal error - unexpected qualifier {}'.format(qualifier))
107 m_comment = re.compile(r'\A\s*#')
108 m_blank = re.compile(r'\A\s*\Z')
110 m_ident = re.compile(r'\A'+re_ident+r'\Z')
111 re_qualified = r'\s*'
112 re_qualified += r'(?P<plus_minus>[+-]?)'
113 re_qualified += r'\s*'
114 re_qualified += r'(?P<keyword>{re_ident})'.format(re_ident=re_ident)
115 re_qualified += r'\s*'
116 re_qualified += r'(?P<qualifier>>=|<=|=)'
117 re_qualified += r'\s*'
118 re_qualified += r'(?P<fcdistro>{re_ident}[0-9]+)'.format(re_ident=re_ident)
119 re_qualified += r'\s*'
120 m_qualified = re.compile(r'\A{}\Z'.format(re_qualified))
122 re_old = '[a-z]+[+-][a-z]+[0-9]+'
123 m_old = re.compile(r'\A{}\Z'.format(re_old))
125 # returns a tuple (included, excluded)
126 def parse(self, filename):
131 with open(filename) as feed:
132 for lineno, line in enumerate(feed, 1):
134 if self.m_comment.match(line) or self.m_blank.match(line):
137 lefts, rights = line.split(':', 1)
138 for left in lefts.split():
139 ########## single ident
140 if self.m_ident.match(left):
141 if left not in known_keywords:
142 raise Exception("Unknown keyword {left}".format(**locals()))
143 elif left == self.keyword:
144 included += rights.split()
146 m = self.m_qualified.match(left)
148 (plus_minus, kw, qual, fcdistro) = m.groups()
149 if kw not in known_keywords:
150 raise Exception("Unknown keyword in {left}".format(**locals()))
151 if fcdistro not in known_fcdistros:
152 raise Exception('Unknown fcdistro {fcdistro}'.format(**locals()))
153 # skip if another keyword
154 if kw != self.keyword: continue
155 # does this fcdistro match ?
156 (distro, version) = m_fcdistro_cutter.match(fcdistro).groups()
157 version = int (version)
158 # skip if another distro family
159 if distro != self.distro: continue
160 # skip if the qualifier does not fit
161 if not self.match (qual, version):
162 if self.options.verbose:
163 print('{filename}:{lineno}:qualifer {left} does not apply'
164 .format(**locals()), file=stderr)
166 # we're in, let's add (default) or remove (if plus_minus is minus)
167 if plus_minus == '-':
168 if self.options.verbose:
169 print('{filename}:{lineno}: from {left}, excluding {rights}'
170 .format(**locals()), file=stderr)
171 excluded += rights.split()
173 if self.options.verbose:
174 print('{filename}:{lineno}: from {left}, including {rights}'\
175 .format(**locals()), file=stderr)
176 included += rights.split()
177 elif self.m_old.match(left):
178 raise Exception('Old-fashioned syntax not supported anymore {left}'.\
181 raise Exception('error in left expression {left}'.format(**locals()))
183 except Exception as e:
185 print("{filename}:{lineno}:syntax error: {e}".format(**locals()), file=stderr)
186 except Exception as exc:
188 print('Could not parse file', filename, exc, file=stderr)
189 return (ok, included, excluded)
195 for input in self.inputs:
196 (o, i, e) = self.parse (input)
200 # avoid set operations that would not preserve order
201 results = [x for x in included if x not in excluded]
203 results = [x.replace('@arch@', self.arch)
204 .replace('@fcdistro@', self.fcdistro)
205 .replace('@pldistro@', self.pldistro) for x in results]
206 if self.options.sort_results:
208 # default is space-separated
209 if not self.options.new_line:
210 print(" ".join(results))
211 # but for tests results are printed each on a line
213 for result in results:
218 usage = "Usage: %prog [options] keyword input[...]"
219 parser = OptionParser(usage=usage)
221 '-a', '--arch', dest='arch', action='store', default=default_arch,
222 help='target arch, e.g. i386 or x86_64, default={}'.format(default_arch))
224 '-f', '--fcdistro', dest='fcdistro', action='store', default=default_fcdistro,
225 help='fcdistro, e.g. f12 or centos5')
227 '-d', '--pldistro', dest='pldistro', action='store', default=default_pldistro,
228 help='pldistro, e.g. onelab or planetlab')
230 '-v', '--verbose', dest='verbose', action='store_true', default=False,
231 help='verbose when using qualifiers')
233 '-n', '--new-line', dest='new_line', action='store_true', default=False,
234 help='print outputs separated with newlines rather than with a space')
236 '-u', '--no-sort', dest='sort_results', default=True, action='store_false',
237 help='keep results in the same order as in the inputs')
238 (options, args) = parser.parse_args()
241 parser.print_help(file=stderr)
245 if not options.arch in known_arch:
246 print('Unsupported arch', options.arch, file=stderr)
247 parser.print_help(file=stderr)
249 if options.arch == 'i686':
250 options.arch = 'i386'
251 if not options.fcdistro in known_fcdistros:
252 print('Unsupported fcdistro', options.fcdistro, file=stderr)
253 parser.print_help(file=stderr)
256 pkgs = PkgsParser(options.arch, options.fcdistro, options.pldistro,
257 keyword, inputs, options)
264 if __name__ == '__main__':