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
30 from sys import stderr
31 from optparse import OptionParser
35 known_arch = ['i386', 'i686', 'x86_64']
36 default_fcdistro='f14'
37 known_fcdistros = [ 'centos5','centos6',
38 'f8', 'f10','f12', 'f14', 'f16', 'f18',
43 'oneiric', 'precise', 'quantal' ]
44 default_pldistro='onelab'
46 known_keywords=['groupname', 'groupdesc',
47 'nodeyumexclude', 'plcyumexclude',
48 'yumexclude', 'package', 'group', 'precious', 'junk', 'mirror', ]
51 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
56 def __init__ (self,arch,fcdistro,pldistro,keyword,inputs,options):
58 self.fcdistro=fcdistro
59 self.pldistro=pldistro
62 # for verbose, new_line, and the like
65 for known in known_fcdistros:
68 (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
69 # debian-like names can't use numbering
76 self.version=int(version)
78 print >> stderr, 'unrecognized fcdistro', fcdistro
81 # qualifier is either '>=','<=', or '='
82 def match (self, qualifier, version):
84 return self.version == version
85 elif qualifier == '>=':
86 return self.version >= version
87 elif qualifier == '<=':
88 return self.version <= version
90 raise Exception, 'Internal error - unexpected qualifier %r' % qualifier
92 m_comment=re.compile('\A\s*#')
93 m_blank=re.compile('\A\s*\Z')
95 m_ident=re.compile('\A'+re_ident+'\Z')
97 re_qualified += '(?P<plus_minus>[+-]?)'
99 re_qualified += '(?P<keyword>%s)'%re_ident
100 re_qualified += '\s*'
101 re_qualified += '(?P<qualifier>>=|<=|=)'
102 re_qualified += '\s*'
103 re_qualified += '(?P<fcdistro>%s[0-9]+)'%re_ident
104 re_qualified += '\s*'
105 m_qualified = re.compile('\A%s\Z'%re_qualified)
107 re_old = '[a-z]+[+-][a-z]+[0-9]+'
108 m_old = re.compile ('\A%s\Z'%re_old)
110 # returns a tuple (included,excluded)
111 def parse (self,filename):
117 for line in file(filename).readlines():
120 if self.m_comment.match(line) or self.m_blank.match(line):
123 [lefts,rights] = line.split(':',1)
124 for left in lefts.split():
125 ########## single ident
126 if self.m_ident.match(left):
127 if left not in known_keywords:
128 raise Exception,"Unknown keyword %r"%left
129 elif left == self.keyword:
130 included += rights.split()
132 m=self.m_qualified.match(left)
134 (plus_minus,kw,qual,fcdistro) = m.groups()
135 if kw not in known_keywords:
136 raise Exception,"Unknown keyword in %r"%left
137 if fcdistro not in known_fcdistros:
138 raise Exception, 'Unknown fcdistro %r'%fcdistro
139 # skip if another keyword
140 if kw != self.keyword: continue
141 # does this fcdistro match ?
142 (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
143 version = int (version)
144 # skip if another distro family
145 if distro != self.distro: continue
146 # skip if the qualifier does not fit
147 if not self.match (qual, version):
148 if self.options.verbose: print >> stderr,'%s:%d:qualifer %s does not apply'%(filename,lineno,left)
150 # we're in, let's add (default) or remove (if plus_minus is minus)
151 if plus_minus == '-':
152 if self.options.verbose: print >> stderr,'%s:%d: from %s, excluding %r'%(filename,lineno,left,rights)
153 excluded += rights.split()
155 if self.options.verbose: print >> stderr,'%s:%d: from %s, including %r'%(filename,lineno,left,rights)
156 included += rights.split()
157 elif self.m_old.match(left):
158 raise Exception,'Old-fashioned syntax not supported anymore %r'%left
160 raise Exception,'error in left expression %r'%left
164 print >> stderr, "%s:%d:syntax error: %r"%(filename,lineno,e)
167 print >> stderr, 'Could not parse file',filename,e
168 return (ok,included,excluded)
174 for input in self.inputs:
175 (o,i,e) = self.parse (input)
179 # avoid set operations that would not preserve order
180 results = [ x for x in included if x not in excluded ]
182 results = [ x.replace('@arch@',self.arch).\
183 replace('@fcdistro@',self.fcdistro).\
184 replace('@pldistro@',self.pldistro) for x in results]
185 if self.options.sort_results:
187 # default is space-separated
188 if not self.options.new_line:
189 print " ".join(results)
190 # but for tests results are printed each on a line
192 for result in results : print result
196 usage="Usage: %prog [options] keyword input[...]"
197 parser=OptionParser (usage=usage)
198 parser.add_option ('-a','--arch',dest='arch',action='store',default=default_arch,
199 help='target arch, e.g. i386 or x86_64, default=%s'%default_arch)
200 parser.add_option ('-f','--fcdistro',dest='fcdistro',action='store', default=default_fcdistro,
201 help='fcdistro, e.g. f12 or centos5')
202 parser.add_option ('-d','--pldistro',dest='pldistro',action='store', default=default_pldistro,
203 help='pldistro, e.g. onelab or planetlab')
204 parser.add_option ('-v', '--verbose',dest='verbose',action='store_true',default=False,
205 help='verbose when using qualifiers')
206 parser.add_option ('-n', '--new-line',dest='new_line',action='store_true',default=False,
207 help='print outputs separated with newlines rather than with a space')
208 parser.add_option ('-u', '--no-sort',dest='sort_results',default=True,action='store_false',
209 help='keep results in the same order as in the inputs')
210 (options,args) = parser.parse_args()
213 parser.print_help(file=stderr)
217 if not options.arch in known_arch:
218 print >> stderr, 'Unsupported arch',options.arch
219 parser.print_help(file=stderr)
221 if options.arch == 'i686': options.arch='i386'
222 if not options.fcdistro in known_fcdistros:
223 print >> stderr, 'Unsupported fcdistro',options.fcdistro
224 parser.print_help(file=stderr)
227 pkgs = PkgsParser (options.arch,options.fcdistro,options.pldistro,keyword,inputs,options)
234 if __name__ == '__main__':