def TestFile(name, stringlist, force, update): errors = 0 # Get the old timestamp if os.path.exists(name): old_time = os.stat(filename)[ST_MTIME] else: old_time = 'NONE' # Create the file and write to it out = IDLOutFile(filename, force) for item in stringlist: out.Write(item) # We wait for flush to force the timestamp to change time.sleep(2) wrote = out.Close() cur_time = os.stat(filename)[ST_MTIME] if update: if not wrote: ErrOut.Log('Failed to write output %s.' % filename) return 1 if cur_time == old_time: ErrOut.Log('Failed to update timestamp for %s.' % filename) return 1 else: if wrote: ErrOut.Log('Should not have writen output %s.' % filename) return 1 if cur_time != old_time: ErrOut.Log('Should not have modified timestamp for %s.' % filename) return 1 return 0
def GenerateRange(self, ast, releases, options): # Get list of out files outlist = GetOption('out') if outlist: outlist = outlist.split(',') skipList = [] cnt = 0 for filenode in ast.GetListOf('File'): # Ignore files with errors if filenode in self.skip_list: continue # Skip this file if not required if outlist and filenode.GetName() not in outlist: continue # Create the output file and increment out count if there was a delta if self.GenerateFile(filenode, releases, options): cnt = cnt + 1 for filenode in skipList: errcnt = filenode.GetProperty('ERRORS') ErrOut.Log('%s : Skipped because of %d errors.' % (filenode.GetName(), errcnt)) if skipList: return -len(skipList) if GetOption('diff'): return -cnt return cnt
def TestNamespaceFiles(filter): idldir = os.path.split(sys.argv[0])[0] idldir = os.path.join(idldir, 'test_namespace', '*.idl') filenames = glob.glob(idldir) testnames = [] for filename in filenames: if filter and filename not in filter: continue testnames.append(filename) # If we have no files to test, then skip this test if not testnames: InfoOut.Log('No files to test for namespace.') return 0 InfoOut.SetConsole(False) ast = ParseFiles(testnames) InfoOut.SetConsole(True) errs = ast.GetProperty('ERRORS') if errs: ErrOut.Log("Failed namespace test.") else: InfoOut.Log("Passed namespace test.") return errs
def ParseFiles(filenames): parser = IDLParser() filenodes = [] if not filenames: filenames = [] srcroot = GetOption('srcroot') dirs = default_dirs if GetOption('include_private'): dirs += ['private'] for dirname in dirs: srcdir = os.path.join(srcroot, dirname, '*.idl') srcdir = os.path.normpath(srcdir) filenames += sorted(glob.glob(srcdir)) if not filenames: ErrOut.Log('No sources provided.') for filename in filenames: filenode = parser.ParseFile(filename) filenodes.append(filenode) ast = IDLAst(filenodes) if GetOption('dump_tree'): ast.Dump(0) Lint(ast) return ast
def ParseOptions(args): short_opts= "" long_opts = [] # Build short and long option lists for name in sorted(OptionMap.keys()): option = OptionMap[name] if len(name) > 1: if option.default is None: long_opts.append('%s' % name) else: long_opts.append('%s=' % name) else: if option.default is None: short_opts += name else: short_opts += '%s:' % name try: opts, filenames = getopt.getopt(args, short_opts, long_opts) for opt, val in opts: if len(opt) == 2: opt = opt[1:] if opt[0:2] == '--': opt = opt[2:] OptionMap[opt].Set(val) except getopt.error, e: ErrOut.Log('Illegal option: %s\n' % str(e)) DumpHelp() sys.exit(-1)
def Define(self, node, releases, tabs=0, prefix='', comment=False): # If this request does not match unique release, or if the release is not # available (possibly deprecated) then skip. unique = node.GetUniqueReleases(releases) if not unique or not node.InReleases(releases): return '' self.LogEnter('Define %s tab=%d prefix="%s"' % (node,tabs,prefix)) declmap = dict({ 'Enum': CGen.DefineEnum, 'Function': CGen.DefineMember, 'Interface': CGen.DefineStruct, 'Member': CGen.DefineMember, 'Struct': CGen.DefineStruct, 'Typedef': CGen.DefineTypedef }) out = '' func = declmap.get(node.cls, None) if not func: ErrOut.Log('Failed to define %s named %s' % (node.cls, node.GetName())) define_txt = func(self, node, releases, prefix=prefix, comment=comment) comment_txt = GetNodeComments(node, tabs=0) if comment_txt and comment: out += comment_txt out += define_txt indented_out = self.Indent(out, tabs) self.LogExit('Exit Define') return indented_out
def Main(): errors = StringTest() errors += ChildTest() if errors: ErrOut.Log('IDLNode failed with %d errors.' % errors) return -1 return 0
def Main(): errors = 0 errors += PropertyTest() if errors: ErrOut.Log('IDLNode failed with %d errors.' % errors) return -1 return 0
def StringTest(): errors = 0 name_str = 'MyName' text_str = 'MyNode(%s)' % name_str name_node = IDLAttribute('NAME', name_str) node = IDLNode('MyNode', 'no file', 1, 0, [name_node]) if node.GetName() != name_str: ErrOut.Log('GetName returned >%s< not >%s<' % (node.GetName(), name_str)) errors += 1 if node.GetProperty('NAME') != name_str: ErrOut.Log('Failed to get name property.') errors += 1 if str(node) != text_str: ErrOut.Log('str() returned >%s< not >%s<' % (str(node), text_str)) errors += 1 if not errors: InfoOut.Log('Passed StringTest') return errors
def Close(self): filename = os.path.realpath(self.filename) self.open = False outtext = ''.join(self.outlist) oldtext = '' if not self.always_write: if os.path.isfile(filename): oldtext = open(filename, 'rb').read() if self.IsEquivalent_(oldtext): if GetOption('verbose'): InfoOut.Log('Output %s unchanged.' % self.filename) return False if GetOption('diff'): for line in difflib.unified_diff(oldtext.split('\n'), outtext.split('\n'), 'OLD ' + self.filename, 'NEW ' + self.filename, n=1, lineterm=''): ErrOut.Log(line) try: # If the directory does not exit, try to create it, if we fail, we # still get the exception when the file is openned. basepath, leafname = os.path.split(filename) if basepath and not os.path.isdir(basepath) and self.create_dir: InfoOut.Log('Creating directory: %s\n' % basepath) os.makedirs(basepath) if not GetOption('test'): outfile = open(filename, 'wb') outfile.write(outtext) outfile.close() InfoOut.Log('Output %s written.' % self.filename) return True except IOError as e: ErrOut.Log("I/O error(%d): %s" % (e.errno, e.strerror)) except: ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0]) raise return False
def TestErrorFiles(filter): idldir = os.path.split(sys.argv[0])[0] idldir = os.path.join(idldir, 'test_parser', '*.idl') filenames = glob.glob(idldir) parser = IDLParser() total_errs = 0 for filename in filenames: if filter and filename not in filter: continue errs = TestFile(parser, filename) if errs: ErrOut.Log("%s test failed with %d error(s)." % (filename, errs)) total_errs += errs if total_errs: ErrOut.Log("Failed parsing test.") else: InfoOut.Log("Passed parsing test.") return total_errs
def Main(): errors = 0 errors += PropertyTest() errors += ReplaceTest() errors += MultiParentTest() if errors: ErrOut.Log('IDLNode failed with %d errors.' % errors) return -1 return 0
def ParseData(self, data, filename='<Internal>'): self.SetData(filename, data) try: self.parse_errors = 0 self.parse_warnings = 0 return self.yaccobj.parse(lexer=self) except lex.LexError as le: ErrOut.Log(str(le)) return []
def Main(args): filenames = ParseOptions(args) # If testing... if GetOption('test'): errs = TestErrorFiles(filenames) errs = TestNamespaceFiles(filenames) if errs: ErrOut.Log("Parser failed with %d errors." % errs) return -1 return 0 # Otherwise, build the AST ast = ParseFiles(filenames) errs = ast.GetProperty('ERRORS') if errs: ErrOut.Log('Found %d error(s).' % errs) InfoOut.Log("%d files processed." % len(filenames)) return errs
def Define(self, node, releases, tabs=0, prefix='', comment=False): # If this request does not match unique release, or if the release is not # available (possibly deprecated) then skip. unique = node.GetUniqueReleases(releases) if not unique or not node.InReleases(releases): return '' self.LogEnter('Define %s tab=%d prefix="%s"' % (node, tabs, prefix)) declmap = dict({ 'Enum': CGen.DefineEnum, 'Function': CGen.DefineMember, 'Interface': CGen.DefineStruct, 'Member': CGen.DefineMember, 'Struct': CGen.DefineStruct, 'Typedef': CGen.DefineTypedef }) out = '' func = declmap.get(node.cls, None) if not func: ErrOut.Log('Failed to define %s named %s' % (node.cls, node.GetName())) define_txt = func(self, node, releases, prefix=prefix, comment=comment) comment_txt = GetNodeComments(node, tabs=0) if comment_txt and comment: out += comment_txt out += define_txt tab = ' ' * tabs lines = [] for line in out.split('\n'): # Add indentation line = tab + line if len(line) > 80: left = line.rfind('(') + 1 args = line[left:].split(',') line_max = 0 for arg in args: if len(arg) > line_max: line_max = len(arg) if left + line_max >= 80: space = '%s ' % tab args = (',\n%s' % space).join( [arg.strip() for arg in args]) lines.append('%s\n%s%s' % (line[:left], space, args)) else: space = ' ' * (left - 1) args = (',\n%s' % space).join(args) lines.append('%s%s' % (line[:left], args)) else: lines.append(line.rstrip()) self.LogExit('Exit Define') return '\n'.join(lines)
def ChildTest(): errors = 0 child = IDLNode('child', 'no file', 1, 0) parent = IDLNode('parent', 'no file', 1, 0, [child]) if child.parent != parent: ErrOut.Log('Failed to connect parent.') errors += 1 if [child] != parent.GetChildren(): ErrOut.Log('Failed GetChildren.') errors += 1 if child != parent.GetOneOf('child'): ErrOut.Log('Failed GetOneOf(child)') errors += 1 if parent.GetOneOf('bogus'): ErrOut.Log('Failed GetOneOf(bogus)') errors += 1 if not parent.IsA('parent'): ErrOut.Log('Expecting parent type') errors += 1 parent = IDLNode('parent', 'no file', 1, 0, [child, child]) if [child, child] != parent.GetChildren(): ErrOut.Log('Failed GetChildren2.') errors += 1 if not errors: InfoOut.Log('Passed ChildTest') return errors
def TestFiles(filenames): if not filenames: idldir = os.path.split(sys.argv[0])[0] idldir = os.path.join(idldir, 'test_cgen', '*.idl') filenames = glob.glob(idldir) filenames = sorted(filenames) ast = ParseFiles(filenames) total_errs = 0 for filenode in ast.GetListOf('File'): errs = TestFile(filenode) if errs: ErrOut.Log('%s test failed with %d error(s).' % (filenode.GetName(), errs)) total_errs += errs if total_errs: ErrOut.Log('Failed generator test.') else: InfoOut.Log('Passed generator test.') return total_errs
def TestFiles(filenames, test_releases): ast = ParseFiles(filenames) iface_releases = pnaclgen.DetermineInterfaces(ast, test_releases) new_output = CleanString( pnaclgen.GenerateWrapperForMethods(iface_releases, comments=False)) old_output = GetOldTestOutput(ast) if new_output != old_output: PrintErrorDiff(old_output, new_output) ErrOut.Log('Failed pnacl generator test.') return 1 else: InfoOut.Log('Passed pnacl generator test.') return 0
def Lint(ast): options = ['wcomment', 'wenum', 'winline', 'wparam', 'wpass', 'wname'] wnone = GetOption('wnone') for opt in options: if wnone or GetOption(opt): ast.SetProperty(opt, True) skipList = [] for filenode in ast.GetListOf('File'): name = filenode.GetProperty('NAME') if filenode.GetProperty('ERRORS') > 0: ErrOut.Log('%s : Skipped due to errors.' % name) skipList.append(filenode) continue warnings = IDLLinter().Visit(filenode, 0) if warnings: WarnOut.Log('%s warning(s) for %s\n' % (warnings, name)) return skipList
def GenerateRange(self, ast, releases, options): """Generate shim code for a range of releases. """ # Remember to set the output filename before running this. out_filename = self.output_file if out_filename is None: ErrOut.Log('Did not set filename for writing out wrapper\n') return 1 InfoOut.Log("Generating %s for %s" % (out_filename, self.wrapper_prefix)) out = IDLOutFile(out_filename) # Get a list of all the interfaces along with metadata. iface_releases = self.DetermineInterfaces(ast, releases) # Generate the includes. self.GenerateIncludes(iface_releases, out) out.Write(self.GetGuardStart()) # Write out static helper functions (mystrcmp). self.GenerateHelperFunctions(out) # Declare list of WrapperInfo before actual wrapper methods, since # they reference each other. self.DeclareWrapperInfos(iface_releases, out) # Generate wrapper functions for each wrapped method in the interfaces. result = self.GenerateWrapperForMethods(iface_releases) out.Write(result) # Collect all the wrapper functions into interface structs. self.GenerateWrapperInterfaces(iface_releases, out) # Generate a table of the wrapped interface structs that can be looked up. self.GenerateWrapperInfoAndCollection(iface_releases, out) # Write out the IDL-invariant functions. self.GenerateFixedFunctions(out) out.Write(self.GetGuardEnd()) out.Close() return 0
def TestFile(filenode): cgen = CGen() errors = 0 for node in filenode.GetChildren()[2:]: instr = node.GetOneOf('Comment') if not instr: continue instr.Dump() instr = CleanString(instr.GetName()) outstr = cgen.Define(node, releases=['M14']) if GetOption('verbose'): print outstr + '\n' outstr = CleanString(outstr) if instr != outstr: ErrOut.Log('Failed match of\n>>%s<<\n>>%s<<\nto:' % (instr, outstr)) node.Dump(1, comments=True) errors += 1 return errors
def TestVersionFiles(filter): idldir = os.path.split(sys.argv[0])[0] idldir = os.path.join(idldir, 'test_version', '*.idl') filenames = glob.glob(idldir) testnames = [] for filename in filenames: if filter and filename not in filter: continue testnames.append(filename) # If we have no files to test, then skip this test if not testnames: InfoOut.Log('No files to test for version.') return 0 ast = ParseFiles(testnames) errs = FindVersionError(ast.releases, ast) if errs: ErrOut.Log("Failed version test.") else: InfoOut.Log("Passed version test.") return errs
def GenerateRange(self, ast, releases, options): # Get list of out files outlist = GetOption('out') if outlist: outlist = outlist.split(',') skipList = [] cnt = 0 for filenode in ast.GetListOf('File'): # Skip this file if not required if outlist and filenode.GetName() not in outlist: continue # If this file has errors, skip it if filenode.GetProperty('ERRORS') > 0: skipList.append(filenode) continue # Create output file out = self.GetOutFile(filenode, options) self.GenerateHead(out, filenode, releases, options) self.GenerateBody(out, filenode, releases, options) self.GenerateTail(out, filenode, releases, options) if out.Close(): cnt = cnt + 1 for filenode in skipList: errcnt = filenode.GetProperty('ERRORS') ErrOut.Log('%s : Skipped because of %d errors.' % (filenode.GetName(), errcnt)) if skipList: return -len(skipList) if GetOption('diff'): return -cnt return cnt
def ExpectProp(node, name, val): found = node.GetProperty(name) if found != val: ErrOut.Log('Got property %s expecting %s' % (found, val)) return 1 return 0
def ExpectText(node, text, val): found = node.Replace(text) if found != val: ErrOut.Log('Got replacement %s expecting %s' % (found, val)) return 1 return 0
def Error(self, msg): name = self.GetProperty('NAME', 'Unknown') parents = [parent.GetProperty('NAME', '???') for parent in self.parents] ErrOut.Log('%s [%s] : %s' % (name, ' '.join(parents), msg))
def TestErrors(filename, filenode): nodelist = filenode.GetChildren() lexer = IDLLexer() data = open(filename).read() lexer.SetData(filename, data) pass_comments = [] fail_comments = [] while True: tok = lexer.lexobj.token() if tok == None: break if tok.type == 'COMMENT': args = tok.value[3:-3].split() if args[0] == 'OK': pass_comments.append((tok.lineno, ' '.join(args[1:]))) else: if args[0] == 'FAIL': fail_comments.append((tok.lineno, ' '.join(args[1:]))) obj_list = [] for node in nodelist: obj_list.extend(FlattenTree(node)) errors = 0 # # Check for expected successes # obj_cnt = len(obj_list) pass_cnt = len(pass_comments) if obj_cnt != pass_cnt: InfoOut.Log("Mismatched pass (%d) vs. nodes built (%d)." % (pass_cnt, obj_cnt)) InfoOut.Log("PASS: %s" % [x[1] for x in pass_comments]) InfoOut.Log("OBJS: %s" % obj_list) errors += 1 if pass_cnt > obj_cnt: pass_cnt = obj_cnt for i in range(pass_cnt): line, comment = pass_comments[i] if obj_list[i] != comment: ErrOut.LogLine(filename, line, None, "OBJ %s : EXPECTED %s\n" % (obj_list[i], comment)) errors += 1 # # Check for expected errors # err_list = ErrOut.DrainLog() err_cnt = len(err_list) fail_cnt = len(fail_comments) if err_cnt != fail_cnt: InfoOut.Log("Mismatched fail (%d) vs. errors seen (%d)." % (fail_cnt, err_cnt)) InfoOut.Log("FAIL: %s" % [x[1] for x in fail_comments]) InfoOut.Log("ERRS: %s" % err_list) errors += 1 if fail_cnt > err_cnt: fail_cnt = err_cnt for i in range(fail_cnt): line, comment = fail_comments[i] err = err_list[i].strip() if err_list[i] != comment: ErrOut.Log("%s(%d) Error\n\tERROR : %s\n\tEXPECT: %s" % (filename, line, err_list[i], comment)) errors += 1 # Clear the error list for the next run err_list = [] return errors
def Error(self, msg): ErrOut.Log('Error %s : %s' % (self.name, msg)) self.errors += 1
def PrintErrorDiff(old, new): oldlines = old.split(';') newlines = new.split(';') d = difflib.Differ() diff = d.compare(oldlines, newlines) ErrOut.Log('Diff is:\n%s' % '\n'.join(diff))
# still get the exception when the file is openned. basepath, leafname = os.path.split(filename) if basepath and not os.path.isdir(basepath) and self.create_dir: InfoOut.Log('Creating directory: %s\n' % basepath) os.makedirs(basepath) if not GetOption('test'): outfile = open(filename, 'wb') outfile.write(outtext) InfoOut.Log('Output %s written.' % self.filename) return True except IOError as (errno, strerror): ErrOut.Log("I/O error(%d): %s" % (errno, strerror)) except: ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0]) raise return False def TestFile(name, stringlist, force, update): errors = 0 # Get the old timestamp if os.path.exists(name): old_time = os.stat(filename)[ST_MTIME] else: old_time = 'NONE' # Create the file and write to it