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','x86_64']
36 default_fcdistro='f12'
37 known_fcdistros = [ 'centos5','centos6','f8', 'f9','f10','f11','f12', 'f13', 'f14', 'f16', 'sl6']
38 default_pldistro='onelab'
40 known_keywords=['groupname', 'groupdesc',
41 'nodeyumexclude', 'plcyumexclude',
42 'yumexclude', 'package', 'group', 'precious', 'junk', 'mirror', ]
45 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
50 def __init__ (self,arch,fcdistro,pldistro,keyword,inputs,options):
52 self.fcdistro=fcdistro
53 self.pldistro=pldistro
56 # for verbose, new_line, and the like
59 for known in known_fcdistros:
61 (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
65 self.version=int(version)
67 print >> stderr, 'unrecognized fcdistro', fcdistro
70 # qualifier is either '>=','<=', or '='
71 def match (self, qualifier, version):
73 return self.version == version
74 elif qualifier == '>=':
75 return self.version >= version
76 elif qualifier == '<=':
77 return self.version <= version
79 raise Exception, 'Internal error - unexpected qualifier %r' % qualifier
81 m_comment=re.compile('\A\s*#')
82 m_blank=re.compile('\A\s*\Z')
84 m_ident=re.compile('\A'+re_ident+'\Z')
86 re_qualified += '(?P<plus_minus>[+-]?)'
88 re_qualified += '(?P<keyword>%s)'%re_ident
90 re_qualified += '(?P<qualifier>>=|<=|=)'
92 re_qualified += '(?P<fcdistro>%s[0-9]+)'%re_ident
94 m_qualified = re.compile('\A%s\Z'%re_qualified)
96 re_old = '[a-z]+[+-][a-z]+[0-9]+'
97 m_old = re.compile ('\A%s\Z'%re_old)
99 # returns a tuple (included,excluded)
100 def parse (self,filename):
106 for line in file(filename).readlines():
109 if self.m_comment.match(line) or self.m_blank.match(line):
112 [lefts,rights] = line.split(':',1)
113 for left in lefts.split():
114 ########## single ident
115 if self.m_ident.match(left):
116 if left not in known_keywords:
117 raise Exception,"Unknown keyword %r"%left
118 elif left == self.keyword:
119 included += rights.split()
121 m=self.m_qualified.match(left)
123 (plus_minus,kw,qual,fcdistro) = m.groups()
124 if kw not in known_keywords:
125 raise Exception,"Unknown keyword in %r"%left
126 if fcdistro not in known_fcdistros:
127 raise Exception, 'Unknown fcdistro %r'%fcdistro
128 # skip if another keyword
129 if kw != self.keyword: continue
130 # does this fcdistro match ?
131 (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
132 version = int (version)
133 # skip if another distro family
134 if distro != self.distro: continue
135 # skip if the qualifier does not fit
136 if not self.match (qual, version):
137 if self.options.verbose: print >> stderr,'%s:%d:qualifer %s does not apply'%(filename,lineno,left)
139 # we're in, let's add (default) or remove (if plus_minus is minus)
140 if plus_minus == '-':
141 if self.options.verbose: print >> stderr,'%s:%d: from %s, excluding %r'%(filename,lineno,left,rights)
142 excluded += rights.split()
144 if self.options.verbose: print >> stderr,'%s:%d: from %s, including %r'%(filename,lineno,left,rights)
145 included += rights.split()
146 elif self.m_old.match(left):
147 raise Exception,'Old-fashioned syntax not supported anymore %r'%left
149 raise Exception,'error in left expression %r'%left
153 print >> stderr, "%s:%d:syntax error: %r"%(filename,lineno,e)
156 print >> stderr, 'Could not parse file',filename,e
157 return (ok,included,excluded)
163 for input in self.inputs:
164 (o,i,e) = self.parse (input)
168 results = list (set(included).difference(set(excluded)))
170 results = [ x.replace('@arch@',self.arch).\
171 replace('@fcdistro@',self.fcdistro).\
172 replace('@pldistro@',self.pldistro) for x in results]
174 # default is space-separated
175 if not self.options.new_line:
176 print " ".join(results)
177 # but for tests results are printed each on a line
179 for result in results : print result
183 usage="Usage: %prog [options] keyword input[...]"
184 parser=OptionParser (usage=usage)
185 parser.add_option ('-a','--arch',dest='arch',action='store',default=default_arch,
186 help='target arch, e.g. i386 or x86_64')
187 parser.add_option ('-f','--fcdistro',dest='fcdistro',action='store', default=default_fcdistro,
188 help='fcdistro, e.g. f12 or centos5')
189 parser.add_option ('-d','--pldistro',dest='pldistro',action='store', default=default_pldistro,
190 help='pldistro, e.g. onelab or planetlab')
191 parser.add_option ('-v', '--verbose',dest='verbose',action='store_true',default=False,
192 help='verbose when using qualifiers')
193 parser.add_option ('-n', '--new-line',dest='new_line',action='store_true',default=False,
194 help='print outputs separated with newlines rather than with a space')
195 (options,args) = parser.parse_args()
198 parser.print_help(file=stderr)
202 if not options.arch in known_arch:
203 print >> stderr, 'Unsupported arch',options.arch
204 parser.print_help(file=stderr)
206 if not options.fcdistro in known_fcdistros:
207 print >> stderr, 'Unsupported fcdistro',options.fcdistro
208 parser.print_help(file=stderr)
211 pkgs = PkgsParser (options.arch,options.fcdistro,options.pldistro,keyword,inputs,options)
218 if __name__ == '__main__':