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', 'f15', 'sl6']
38 default_pldistro='onelab'
40 known_keywords=['groupname', 'groupdesc', 'kexclude', 'package', 'group', 'precious', 'junk', 'mirror', ]
43 m_fcdistro_cutter = re.compile('([a-z]+)([0-9]+)')
48 def __init__ (self,arch,fcdistro,pldistro,keyword,inputs,options):
50 self.fcdistro=fcdistro
51 self.pldistro=pldistro
54 # for verbose, new_line, and the like
57 for known in known_fcdistros:
59 (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
63 self.version=int(version)
65 print >> stderr, 'unrecognized fcdistro', fcdistro
68 # qualifier is either '>=','<=', or '='
69 def match (self, qualifier, version):
71 return self.version == version
72 elif qualifier == '>=':
73 return self.version >= version
74 elif qualifier == '<=':
75 return self.version <= version
77 raise Exception, 'Internal error - unexpected qualifier %r' % qualifier
79 m_comment=re.compile('\A\s*#')
80 m_blank=re.compile('\A\s*\Z')
82 m_ident=re.compile('\A'+re_ident+'\Z')
84 re_qualified += '(?P<plus_minus>[+-]?)'
86 re_qualified += '(?P<keyword>%s)'%re_ident
88 re_qualified += '(?P<qualifier>>=|<=|=)'
90 re_qualified += '(?P<fcdistro>%s[0-9]+)'%re_ident
92 m_qualified = re.compile('\A%s\Z'%re_qualified)
94 re_old = '[a-z]+[+-][a-z]+[0-9]+'
95 m_old = re.compile ('\A%s\Z'%re_old)
97 # returns a tuple (included,excluded)
98 def parse (self,filename):
104 for line in file(filename).readlines():
107 if self.m_comment.match(line) or self.m_blank.match(line):
110 [lefts,rights] = line.split(':',1)
111 for left in lefts.split():
112 ########## single ident
113 if self.m_ident.match(left):
114 if left not in known_keywords:
115 raise Exception,"Unknown keyword %r"%left
116 elif left == self.keyword:
117 included += rights.split()
119 m=self.m_qualified.match(left)
121 (plus_minus,kw,qual,fcdistro) = m.groups()
122 if kw not in known_keywords:
123 raise Exception,"Unknown keyword in %r"%left
124 if fcdistro not in known_fcdistros:
125 raise Exception, 'Unknown fcdistro %r'%fcdistro
126 # skip if another keyword
127 if kw != self.keyword: continue
128 # does this fcdistro match ?
129 (distro,version)=m_fcdistro_cutter.match(fcdistro).groups()
130 version = int (version)
131 # skip if another distro family
132 if distro != self.distro: continue
133 # skip if the qualifier does not fit
134 if not self.match (qual, version):
135 if self.options.verbose: print >> stderr,'%s:%d:qualifer %s does not apply'%(filename,lineno,left)
137 # we're in, let's add (default) or remove (if plus_minus is minus)
138 if plus_minus == '-':
139 if self.options.verbose: print >> stderr,'%s:%d: from %s, excluding %r'%(filename,lineno,left,rights)
140 excluded += rights.split()
142 if self.options.verbose: print >> stderr,'%s:%d: from %s, including %r'%(filename,lineno,left,rights)
143 included += rights.split()
144 elif self.m_old.match(left):
145 raise Exception,'Old-fashioned syntax not supported anymore %r'%left
147 raise Exception,'error in left expression %r'%left
151 print >> stderr, "%s:%d:syntax error: %r"%(filename,lineno,e)
154 print >> stderr, 'Could not parse file',filename,e
155 return (ok,included,excluded)
161 for input in self.inputs:
162 (o,i,e) = self.parse (input)
166 results = list (set(included).difference(set(excluded)))
168 results = [ x.replace('@arch@',self.arch).\
169 replace('@fcdistro@',self.fcdistro).\
170 replace('@pldistro@',self.pldistro) for x in results]
172 # default is space-separated
173 if not self.options.new_line:
174 print " ".join(results)
175 # but for tests results are printed each on a line
177 for result in results : print result
181 usage="Usage: %prog [options] keyword input[...]"
182 parser=OptionParser (usage=usage)
183 parser.add_option ('-a','--arch',dest='arch',action='store',default=default_arch,
184 help='target arch, e.g. i386 or x86_64')
185 parser.add_option ('-f','--fcdistro',dest='fcdistro',action='store', default=default_fcdistro,
186 help='fcdistro, e.g. f12 or centos5')
187 parser.add_option ('-d','--pldistro',dest='pldistro',action='store', default=default_pldistro,
188 help='pldistro, e.g. onelab or planetlab')
189 parser.add_option ('-v', '--verbose',dest='verbose',action='store_true',default=False,
190 help='verbose when using qualifiers')
191 parser.add_option ('-n', '--new-line',dest='new_line',action='store_true',default=False,
192 help='print outputs separated with newlines rather than with a space')
193 (options,args) = parser.parse_args()
196 parser.print_help(file=stderr)
200 if not options.arch in known_arch:
201 print >> stderr, 'Unsupported arch',options.arch
202 parser.print_help(file=stderr)
204 if not options.fcdistro in known_fcdistros:
205 print >> stderr, 'Unsupported fcdistro',options.fcdistro
206 parser.print_help(file=stderr)
209 pkgs = PkgsParser (options.arch,options.fcdistro,options.pldistro,keyword,inputs,options)
216 if __name__ == '__main__':