📄 bif.py
字号:
## <includeprofiles> def start_includeprofiles(self, attr): self.push_tag(TAG_includeprofiles) ## </includeprofiles> def end_includeprofiles(self): self.pop_tag(TAG_includeprofiles) ## <excludeprofiles> def start_excludeprofiles(self, attr): self.push_tag(TAG_excludeprofiles) ## </excludeprofiles> def end_excludeprofiles(self): self.pop_tag(TAG_excludeprofiles) ## <dependlist> def start_dependlist(self, attr): self.push_tag(TAG_dependlist) ## </dependlist> def end_dependlist(self): self.pop_tag(TAG_dependlist) ## <source_dependlist> def start_source_dependlist(self, attr): self.push_tag(TAG_source_dependlist) ## </source_dependlist> def end_source_dependlist(self): self.pop_tag(TAG_source_dependlist) ## <checkin_dependlist> def start_checkin_dependlist(self, attr): self.push_tag(TAG_checkin_dependlist) ## </checkin_dependlist> def end_checkin_dependlist(self): self.pop_tag(TAG_checkin_dependlist) ## <defines> def start_defines(self, attr): self.push_tag(TAG_defines) ## </defines> def end_defines(self): self.pop_tag(TAG_defines) ## <ifdef> def start_ifdef(self, attr): self.push_tag(TAG_ifdef) ## </ifdef> def end_ifdef(self): self.pop_tag(TAG_ifdef) ## <ifndef> def start_ifndef(self, attr): self.push_tag(TAG_ifndef) ## </ifndef> def end_ifndef(self): self.pop_tag(TAG_ifndef) ## <umake_includefiles> def start_umake_includefiles(self, attr): self.push_tag(TAG_umake_includefiles) ## </umake_includefiles> def end_umake_includefiles(self): self.pop_tag(TAG_umake_includefiles)def install_old_bif_parser(): global BIFParser import xmllib class OLDBIFParser(xmllib.XMLParser, BIFParserFunctions): def __init__(self, filename, bif_data = None, branchlist = None): import xmllib xmllib.XMLParser.__init__(self) BIFParserFunctions.__init__(self, filename, bif_data, branchlist) def handle_data(self, data): BIFParserFunctions.handle_data(self, data) BIFParser=OLDBIFParserdef install_new_bif_parser(): global BIFParser import xml.parsers.expat class NewBIFParser(BIFParserFunctions): def __init__(self, filename, bif_data = None, branchlist = None): import xml.parsers.expat self.__parser=xml.parsers.expat.ParserCreate() self.__start={} self.__end={} self.__parser.StartElementHandler = self.__start_element self.__parser.EndElementHandler = self.__end_element self.__parser.CharacterDataHandler = self.handle_data try: self.__parser.buffer_text=1 except AttributeError: pass for key in BIFParserFunctions.__dict__.keys(): if key[:6] == 'start_': self.__start[key[6:]]=getattr(self, key) if key[:4] == 'end_': self.__end[key[4:]]=getattr(self, key) BIFParserFunctions.__init__(self, filename, bif_data, branchlist) def parse_bif(self, filename): import xml.parsers.expat try: self.__parser.Parse(open(filename, "r").read(), 1); except xml.parsers.expat.ExpatError: e=err.Error(); e.Set("bif parser(line %d): %s" %( self.__parser.ErrorLineNumber, xml.parsers.expat.ErrorString(self.__parser.ErrorLineNumber))); raise err.error, e def location(self): l=self.filename if self.last_module: l=l+" near module %s" % self.last_module else: l=l+" near beginning" if self.current_tag: l=l+" in <%s>" % self.current_tag return l def __start_element(self, name, attrs): if self.__start.has_key(name): nattrs={} for (key, value) in attrs.items(): nattrs[key.encode("iso-8859-1")]=value.encode("iso-8859-1") attrs=nattrs self.__start[name](attrs) def __end_element(self, name): if self.__end.has_key(name): self.__end[name]() def handle_data(self, data): data=data.encode("iso-8859-1") data=re.sub("\n\\s+","\n", data) BIFParserFunctions.handle_data(self, data) BIFParser=NewBIFParser ## useful...def module_list_to_module_id_list(module_list): module_id_list = [] for module in module_list: module_id_list.append(module.id) return module_id_list## check for circular dependanciesclass CircularDependCheck: def __init__(self, bif_data): self.bif_data = bif_data self.good_module_list = [] self.bad_module_list = [] ## flag that allows us to abort the recursion self.abort = 0 ## working stack self.stack = [] ## list of discovered cycles self.cycle_list = [] ## list of module IDs without modules self.unknown_module_id_list = [] def run(self): for module in self.bif_data.module_hash.values(): outmsg.verbose("checking module id=\"%s\"" % (module.id)) self.rc_check(module) def rc_check(self, _module): ## somthing happened if self.abort: return ## if this module is in the good_module_list, then we've ## been certified good if self.good_module_list.count(_module): return ## check for cycle (circular dependancy) if self.stack.count(_module): ## add entire stack to the bad module list for module in self.stack: if not self.bad_module_list.count(module): self.bad_module_list.append(module) ## record the discovered cycle cycle = self.stack[self.stack.index(_module):] if not self.check_duplicate_in_cycle_list(cycle): self.cycle_list.append(cycle) self.abort = 1 return ## push current module onto the stack self.stack.append(_module) ## check for a dependancy in the stack for module_id in _module.dependancy_id_list: try: chk_module = self.bif_data.module_hash[module_id] except KeyError: ## log modules that do not exist self.unknown_module_id_list.append((_module.id, module_id)) continue self.rc_check(chk_module) ## if we're not bad, then we're good! if not self.bad_module_list.count(_module): self.good_module_list.append(_module) ## pop off the stack self.stack.remove(_module) def compare_lists(self, list1, list2): ## if the lengths don't match, then the lists ## don't match if len(list1) != len(list2): return 0 for index in range(len(list1)): if list1[index] != list2[index]: return 0 return 1 def check_duplicate_in_cycle_list(self, cycle): for temp in self.cycle_list: if self.compare_lists(temp, cycle): return 1 return 0 def print_module_list(self, list): print '--- MODULE DUMP ---' for module in list: print module.id print '------------------'def CheckBIFData(bif_data): cdep_check = CircularDependCheck(bif_data) cdep_check.run() ## print any circular dependancies if len(cdep_check.cycle_list): print '*** found BIF circular dependancies ***' for cycle in cdep_check.cycle_list: temp = "bif dependancy cycle=\"%s\"" % ( string.join(module_list_to_module_id_list(cycle), '->')) outmsg.error(temp) ## print unresolved module ids if len(cdep_check.unknown_module_id_list): for unknown in cdep_check.unknown_module_id_list: outmsg.error('in dependlist of %s found unknown dependancy %s' % ( unknown[0], unknown[1]))def CheckBIFFile(filename): if not os.path.isfile(filename): print 'file not found' sys.exit(1) ## load BIF file print 'loading...' bif_data = load_bif_data(filename)def rdiff(a, b, done): if a == b: return 1 if done.has_key( repr( (a,b) ) ): return 1 done[ repr( (a,b) ) ] = 1 ta=type(a) tb=type(b) if ta == tb: if ta == types.InstanceType: if not rdiff(a.__class__,b.__class__, done): print "Class differs" return 0 if not rdiff(a.__dict__,b.__dict__, done): print "Dict differs" return 0 return 1 if ta == types.DictType: for k in a.keys(): if not b.has_key(k): print "Only in a: %s" % repr(k) return 0 for k in b.keys(): if not a.has_key(k): print "Only in b: %s" % repr(k) return 0 for k in a.keys(): if not rdiff(a[k], b[k], done): print "Value for key %s differs." % repr(k) return 0 return 1 if ta == types.TupleType or ta==types.ListType: if not rdiff(len(a), len(b), done): print "length differs" return 0 n=0 while n < len(a): if not rdiff(a[n], b[n], done): print "index %d differs" % n return 0 n = n + 1 return 1 print "%s != %s " % (repr(a), repr(b)) print "%s != %s " % (repr(b), repr(a)) return 0if __name__ == '__main__': def test_bif(bif_path, branchlist): ## get the bif_data sturcture print "loading %s" % (bif_path) try: bif_data = load_bif_data(bif_path, branchlist) except err.error, e: print "Didn't load...." print e.Text() return except: e = err.Error() e.Set("BIF file %s didn't load..." % bif_path) e.SetTraceback(sys.exc_info()) print print e.Text() return open("./biftestfile-tmp.bif","w").write(bif_data.write()) bif_data2 = load_bif_data("./biftestfile-tmp.bif") if not rdiff(bif_data, bif_data2, {}): print "Bif file %s not reproducable" % bif_path sys.exit(1) return bif_data import buildmenu buildmenu.call_buildrc() import getopt def main(): bif_path = '' file_flag = 0 all_flag=0 branch_list=0 (opt_list, arg_list) = getopt.getopt(sys.argv[1:], 'fa') ## check for the remote build argument for opt in opt_list: if opt[0] == '-f': file_flag = 1 if opt[0] == '-a': all_flag = 1 if all_flag: import branchlist branch_list = branchlist.BranchList() for branch_name in branch_list.list: fname = branch_list.file(branch_name) test_bif(fname, branch_list) print "Successful test" sys.exit(0) ## check that there was a argument specified if len(arg_list) < 1: pname = os.path.basename(sys.argv[0]) print '%s: invalid usage' % (pname) print 'python %s [-f] build-branch' % (pname) print 'python %s -a' % (pname) print '-f: take argument as path instead of branch specification' sys.exit(1) ## if we've been given a branch specification, then find ## the build information file path for it if file_flag: bif_path = arg_list[0] else: import branchlist branch_list = branchlist.BranchList() bif_path = branch_list.file(arg_list[0]) if not bif_path: print '%s invalid branch' % (arg_list[0]) sys.exit(1) print test_bif(bif_path, branch_list).write() main() #import profile #profile.run('main()')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -