def PublicKeyExport(self, dest, mock=None): """Export the public keys corresponding to our key set to destination.""" kmd = self.metadata pubkmd = None if kmd.type == keyinfo.DSA_PRIV and kmd.purpose == keyinfo.SIGN_AND_VERIFY: pubkmd = keydata.KeyMetadata(kmd.name, keyinfo.VERIFY, keyinfo.DSA_PUB) elif kmd.type == keyinfo.RSA_PRIV: if kmd.purpose == keyinfo.DECRYPT_AND_ENCRYPT: pubkmd = keydata.KeyMetadata(kmd.name, keyinfo.ENCRYPT, keyinfo.RSA_PUB) elif kmd.purpose == keyinfo.SIGN_AND_VERIFY: pubkmd = keydata.KeyMetadata(kmd.name, keyinfo.VERIFY, keyinfo.RSA_PUB) if pubkmd is None: raise errors.KeyczarError("Cannot export public key") for v in self.versions: pubkmd.AddVersion(v) pubkey = self.GetKey(v).public_key if mock: # only for testing mock.SetPubKey(v.version_number, pubkey) else: util.WriteFile(str(pubkey), os.path.join(dest, str(v.version_number))) if mock: # only for testing mock.pubkmd = pubkmd else: util.WriteFile(str(pubkmd), os.path.join(dest, "meta"))
def CompareResult(fullName, outputFile, resFile, interpreter, structTest=true): report.Progress( 4, "Comparing result for " + ` fullName ` + " using diff method") # compare the results using normal "diff" actualResult = util.ReadFile(outputFile) expectedResult = util.ReadFile(resFile) # Remove duplicate white spaces and line breaks, spaces around commas and parenthesis. actualResult = string.strip(re.sub("\s+", " ", actualResult)) expectedResult = string.strip(re.sub("\s+", " ", expectedResult)) actualResult = string.strip(re.sub("\s*,\s*", ",", actualResult)) expectedResult = string.strip(re.sub("\s*,\s*", ",", expectedResult)) actualResult = string.strip(re.sub("\s*\(\s*", "(", actualResult)) expectedResult = string.strip(re.sub("\s*\(\s*", "(", expectedResult)) actualResult = string.strip(re.sub("\s*\)\s*", ")", actualResult)) expectedResult = string.strip(re.sub("\s*\)\s*", ")", expectedResult)) if actualResult == expectedResult: return true # Hmmm we need to try to compare using VDMDE then. if structTest and interpreter != None: report.Progress( 4, "Comparing result for " + ` fullName ` + " by build VDM value") # template = util.ReadFile(setup.BaseDir+"/../pogtestcases/compare-pog.vdm") template = util.ReadFile(setup.BaseDir + "/templates/compare-pog.vdm") if template == None: return false data = util.SubString('<<EXPECTED_RESULT>>', expectedResult, template) data = util.SubString('<<ACTUAL_RESULT>>', actualResult, data) ok = util.WriteFile("compare.vdm", data) if not ok: return false ok = util.WriteFile(".vdmtest", "compare.arg") # util.CopyFile(setup.BaseDir+"/../pogtestcases/compare-pog.arg","compare.arg") # util.CopyFile(setup.BaseDir+"/general-files/compare.arg","compare.arg") util.CopyFile(setup.BaseDir + "/general-files/compare-pog.arg", "compare.arg") # cmd = interpreter + " -a -b compare.vdm" cmd = interpreter + " -i compare.arg compare.vdm" (exitcode, dummy1, dummy2) = util.RunCommand(cmd) ok = (exitcode == 0) else: ok = false if not ok: report.Error( "Actual result is different from expected result for " + ` fullName `, "expected result : " + expectedResult + "\n" + "actual result : " + actualResult) if util.CleanFile(ok): util.DeleteFiles(["comapre.arg"]) return ok
def Write(self, loc, encrypter=None): if encrypter: self.metadata.encrypted = True util.WriteFile(str(self.metadata), os.path.join(loc, "meta")) # just plain for v in self.versions: key = str(self.GetKey(v)) if self.metadata.encrypted: key = encrypter.Encrypt( key) # encrypt key info before outputting util.WriteFile(key, os.path.join(loc, str(v.version_number)))
def Create(loc, name, purpose, asymmetric=None): if mock is None and loc is None: # not testing raise errors.KeyczarError("Location missing") kmd = None if purpose == keyinfo.SIGN_AND_VERIFY: if asymmetric is None: kmd = keydata.KeyMetadata(name, purpose, keyinfo.HMAC_SHA1) elif asymmetric.lower() == "rsa": kmd = keydata.KeyMetadata(name, purpose, keyinfo.RSA_PRIV) else: # default to DSA kmd = keydata.KeyMetadata(name, purpose, keyinfo.DSA_PRIV) elif purpose == keyinfo.DECRYPT_AND_ENCRYPT: if asymmetric is None: kmd = keydata.KeyMetadata(name, purpose, keyinfo.AES) else: # default to RSA kmd = keydata.KeyMetadata(name, purpose, keyinfo.RSA_PRIV) else: raise errors.KeyczarError("Missing or unsupported purpose") if mock is not None: # just testing, update mock object mock.kmd = kmd else: fname = os.path.join(loc, "meta") if os.path.exists(fname): raise errors.KeyczarError("File already exists") util.WriteFile(str(kmd), fname)
def CreateArgFile(fullName,lang, modules): global srcext info = convert.ExtractArgFileInfo(fullName) if info == None: return None else: (modcls, op) = info if lang == 'sl': template = util.GetTemplate('arg-file-cpp-sl') str = "" for mod0 in modules: mod = string.replace(mod0, "_", "_u") str = str + "init_" + mod + "();" data = util.SubString('<<INIT>>', str, template) data = util.SubString('<<MOD>>', modcls, data) data = util.SubString('<<OP>>', op, data) else: # Remove any package path if it exists (n/a for c++) match = re.match("^.*\.([^.]+)$", modcls, re.M) if match != None: modcls = match.group(1) template = util.GetTemplate('arg-file-cpp-pp') data = util.SubString('<<CLASS>>',modcls, template) data = util.SubString('<<OP>>', op, data) if data == None: return false return util.WriteFile("icode." + srcext, data)
def RunTestCase(name, lang, type): baseName = util.ExtractName(name) #compile and run compiler = cmdline.LookUpWildCard('metaiv', lang, type, 'compiler') flags = cmdline.LookUpWildCard('metaiv', lang, type, 'cflags') cmd = compiler + " -o driver driver.cc " + name + " " + flags (exitCode, dummy1, dummy2) = util.RunCommand(cmd, 0, "Problem whith compiling") ok = (exitCode == 0) if ok: if not os.path.exists("driver"): report.Error("Driveri binary is not created") (exitCode, stdout, stderr) = util.RunCommand("./driver" + binext, 0, "Error running c++ generated c++ binary", true) ok = (exitCode == 0) #compare results with expected result files if ok: ok = util.WriteFile(baseName + ".res", stdout) else: report.Error("Output", None, stdout, stderr) if ok: resFile = resfile.FindResFile(name) ok = (resFile != None) if ok: resfile.CompareResult(name, baseName + ".res", resFile, None) return ok
def CreateDebugFile(fileName, templateName, extra): base = util.ExtractName(fileName) astFile = base + ".ast" debugFile = base + ".debug" report.Progress(4, "Creating debug (" + debugFile +")") ast = util.ReadFile(astFile) if ast == None: return false template = util.GetTemplate(templateName) if template == None: return false data = util.SubString("<<AST>>", ast, template) for key in extra.keys(): data = util.SubString(key, extra[key], data) ok = util.WriteFile(debugFile, data) if not ok: return false # Make a symlink from the debug file til debug.arg ok = util.SymLink(debugFile, "debug.arg") return ok
def StripSemValue(fullName, lang, dtc): interpreter = cmdline.LookUpWildCard('pog', lang, 'spec', 'sem-backend') inNm = util.ExtractName(fullName) + ".arg.res" outNm = util.ExtractName(fullName) + ".res" # cmd = interpreter + " < " + inNm + " > " + outNm # return util.RunCommand(cmd, 0, "Error while stripping semantic values") return util.WriteFile(outNm, util.ReadFile(inNm))
def WriteMetadata(self, metadata, overwrite=True): """ Write the metadata for the key. """ fname = os.path.join(self.location, "meta") if not overwrite and os.path.exists(fname): raise errors.KeyczarError("File:%s already exists" %fname) util.WriteFile(str(metadata), fname) return
def WriteKey(self, key, version_number, encrypter=None): """ Write out the key at the given version. """ key = str(key) if encrypter: key = encrypter.Encrypt(key) # encrypt key info before outputting util.WriteFile(key, os.path.join(self.location, str(version_number))) return
def UseKey(purpose, loc, dest, crypter=None, msg="This is some test data"): reader = readers.FileReader(loc) answer = "" if crypter: reader = readers.EncryptedReader(reader, crypter) if purpose == keyinfo.DECRYPT_AND_ENCRYPT: answer = keyczar.Crypter(reader).Encrypt(msg) elif purpose == keyinfo.SIGN_AND_VERIFY: answer = keyczar.Signer(reader).Sign(msg) util.WriteFile(answer, dest)
def CreateUserdefFile(modules): # Create _usedef.h files count = 1 ok = true for mod0 in modules: mod = string.replace(mod0, "_", "_u") data = "#define TAG_" + mod + " " + `count*10000`+"\n" count = count +1 ok = util.WriteFile(mod+"_userdef.h", data) and ok return ok
def CompileRunAndCompare(fullName, lang, type, modules): global packageMap baseName = util.ExtractName(fullName) ok = true if ok: ok = CreateArgFile(fullName, lang, modules) standardlibs = convert.GetStandardLibs() libdir = os.path.expandvars(cmdline.LookUp('java-stdlib-dir')) for lib in standardlibs: libfile = lib + ".java" util.CopyFile(libdir + "/" + libfile, libfile) if ok: ok = CompileJavaFiles(fullName, lang, type, modules) interpreter = os.path.expandvars( cmdline.LookUpWildCard('java', lang, type, 'interpreter')) if ok: # Execute the binary flags = os.path.expandvars( cmdline.LookUpWildCard('java', lang, type, 'rtflags')) (exitCode, stdout, stderr) = util.RunCommand(interpreter + " " + flags + " TMAIN", 0, "Error running Java " + "generated binary", true) print("java " + flags + " TMAIN run") ok = (exitCode == 0) if ok: ok = util.WriteFile(baseName + ".res", stdout) if ok: resFile = resfile.FindResFile(fullName) ok = (resFile != None) if ok: ok = CompareRunTimeError(fullName, resFile) lib = "" if ok == None: # It was not a runtime error interpreter = lib + cmdline.LookUpWildCard('java', lang, type, 'spec-compare') # Compare the result ok = resfile.CompareResult(fullName, baseName + ".res", resFile, interpreter) return ok
def SetupArgFile(fullName,lang): argNm = util.StripExt(fullName)+".arg" localNm = util.ExtractName(fullName)+".arg" if os.path.exists(argNm): data = util.ReadFile(argNm) if data == None: return false if lang != 'sl': data = re.sub("^(.*)`(.*)\s*$", "new \\1().\\2", data, re.M) util.DeleteFiles([localNm]) return util.WriteFile(localNm, data) else: argFile = sys.path[0]+"/ip-files/iprun-" + lang + ".arg" return util.CopyFile(argFile,localNm)
def ConvertLanguage(lang, fileName): newName = util.ExtractName(fileName) + ".vdm" report.Progress(4, "Converting testcase to language " + lang + " (creating " + newName +")") data = util.ReadFile(fileName) if data == None: return false if IsLanguageSl(data): if (lang == 'pp'): report.Error("Input file: '" + fileName + "' is a SL file, but a PP file is required") return false else: if (lang == 'sl'): data = PP2SL(data) ok = util.WriteFile(newName, data) return ok
def SaveHaarTransformImage(path, level): #============================================================================== # Carrega a imagem do caminho 'path', aplica a transformada de Haar em # 'level' nÃveis e salva o resultado como imagem PGM e como arquivo de # dados DAT, usando para recuperação da imagem original no caminho 'path' # adicionando o sufixo 'haar' no nome do arquivo. #============================================================================== (image, width, height) = util.LoadImage(path) result = HaarTransform(image, level, width, height) util.WriteImage( str(path).split(".pgm")[0] + '_' + str(level) + '_haar.pgm', result, width, height) util.WriteFile( str(path).split(".pgm")[0] + '_' + str(level) + '_haar.dat', result, width, height)
def CompileRunAndCompare(fullName, lang, type, modules): baseName = util.ExtractName(fullName) ok = true stdout = None stderr = None if ok: CreateUserdefFile(modules) if ok: ok = CreateArgFile(fullName, lang, modules) if ok: ok = CompileCPPFiles(fullName, lang, type, modules) if ok: # Execute the binary (exitCode, stdout, stderr) = util.RunCommand("icode", 0, "Error running c++ generated binary", true) ok = (exitCode == 0) if ok: ok = util.WriteFile(baseName + ".res", stdout) else: report.Error("Output", None, stdout, stderr) if ok: resFile = resfile.FindResFile(fullName) ok = (resFile != None) if ok: ok = CompareRunTimeError(fullName, resFile) if ok == None: # It was not a runtime error interpreter = cmdline.LookUpWildCard('cpp', lang, type, 'spec-compare') # Compare the result ok = resfile.CompareResult(fullName, baseName + ".res", resFile, interpreter) return ok
def CreateArgFile(fileName, templateName, extra): argFile = util.ExtractName(fileName) + ".arg" astFile = util.ExtractName(fileName) + ".ast" report.Progress(4, "Creating arg file (" + argFile +")") template = util.GetTemplate(templateName) if template == None: return false ast = util.ReadFile(astFile) if ast == None: return false data = util.SubString('<<AST>>', ast, template) for key in extra.keys(): data = util.SubString(key, extra[key], data) ok = util.WriteFile(argFile, data) return ok
def CompareRunTimeError(fullName, expResultFile, output): report.Progress(4, "Searching for run time error.") match = re.search("(Run-Time Error *[0-9]+:.*)\"",output, re.M) if not match: match = re.search("(Run-Time Error *[0-9]+:.*)",output, re.M) if not match: report.Error("While searching for runtime error, no runtime error information found for '" + fullName + "'", "Error running interpreter, maybe syntax error") return false resultFile = util.ExtractName(fullName)+".res" ok = util.WriteFile(resultFile,match.group(1)) if not ok: return false if expResultFile: return resfile.CompareResult(fullName, resultFile, expResultFile, None) else: return false
def CreateArgFile(fullName, lang, modules): info = convert.ExtractArgFileInfo(fullName) if info == None: return None else: (modcls, op) = info if lang == 'sl': template = util.GetTemplate('arg-file-java-sl') str = "" for mod in modules: str = str + "init_" + mod + "();" data = util.SubString('<<INIT>>', str, template) data = util.SubString('<<MOD>>', modcls, data) data = util.SubString('<<OP>>', op, data) else: template = util.GetTemplate('arg-file-java-pp') data = util.SubString('<<CLASS>>', modcls, template) data = util.SubString('<<OP>>', op, data) if data == None: return false return util.WriteFile("TMAIN.java", data)
def AppendToDotVdmtest(fileName): newName = util.ExtractName(fileName) + ".arg" report.Progress(4, "Adding " + newName + " to .vdmtest file") return util.WriteFile(".vdmtest", newName+"\n","a")
def PrepareSpecCaseDir(dirName, lang): global runJavacBeforeParser testName = GetTestName(dirName) fullName = os.getcwd() # # Produce a list of java files in dirName # and put it testCases # AddTestsFromHierarchy(dirName, lang) util.DeleteFiles([parseRes]) util.RemoveTestFiles(testName, [ ".arg.pt", ".arg.res", ".arg.err", ".res.arg", ".arg.msg", "vpp.arg", ".res", ".debug", ".ast" ]) ok = convert.CreateOptionsFile(fullName) if runJavacBeforeParser == "yes": ok = CompileJavaFiles(dirName) if not ok: return false javafiles = " " javafiles = javafiles + setup.BaseDir + "/../j2v-test/Object.java " javafiles = javafiles + setup.BaseDir + "/../j2v-test/Exception.java " javafiles = javafiles + setup.BaseDir + "/../j2v-test/String.java " javafiles = javafiles + setup.BaseDir + "/../j2v-test/Character.java " # # Iterate over the list of java files in the dirName (except MAIN.java) # and runs javamain # mainJava = dirName + "/" + "MAIN.java" while ok and (testCases != []): test = GetNextTest() if os.path.abspath(test) != os.path.abspath(mainJava): javafiles = javafiles + test + " " # javafiles = javafiles + setup.BaseDir+"/../j2v-test/Object.java " # javafiles = javafiles + setup.BaseDir+"/../j2v-test/Exception.java " # javafiles = javafiles + setup.BaseDir+"/../j2v-test/String.java " # # Run java parser to get CPP`Files # if not ParseJavaFile(javafiles, lang): ok = false if ok: ast = util.ReadFile(parseRes) if ok: ok = util.WriteFile(testName + ".ast", ast) if ok: convert.CreateDebugFile(dirName, "debug-file-java2vdm", {}) if ok: ok = convert.CreateArgFile(dirName, "arg-file-java2vdm", {}) if ok: ok = convert.AppendToDotVdmtest(dirName) # Clean up if test case failed if not ok and util.CleanFile(ok): util.DeleteFiles([ testName + ".vdm", testName + ".ast", testName + ".arg", testName + ".debug", "debug.arg" ]) return ok
def RunApiTestCase(name, lang, toolMediator): data = util.ReadFile(name) baseName = util.ExtractName(name) neutralName = re.sub('.*/', '', name) # path neutralized! result = baseName + ":\n" last = "" symTable = {} symTable['GENPATH'] = os.path.expanduser(cmdline.LookUp('api-gen-path')) symTable['SOURCEPATH'] = os.path.expanduser( cmdline.LookUp('api-source-path')) print(symTable['GENPATH']) print(symTable['SOURCEPATH']) commands = data.split("\n") line, test = 0, 0 empty = re.compile('^\s*(#.*)|(\s*)$') interactive_mode = cmdline.LookUp('api-run-interactive') for cmd in commands: print("Command: " + cmd) if interactive_mode == 'yes': print("Press <return>") raw_input() line = line + 1 # don't process empty lines if not empty.match(cmd): test = test + 1 try: cmd = re.sub('\$\$', last, cmd) # replace $$ by last result cmd = re.sub('\s+', ' ', cmd).strip() # replace multiple spaces by one cmd = re.sub('\s*\(\s*', '(', cmd) # delete spaces around parentheses cmd = re.sub('\s*\)\s*', ')', cmd) cmd = re.sub('\s*\[\s*', '[', cmd) # delete spaces around parentheses cmd = re.sub('\s*\]\s*', ']', cmd) cmd = re.sub('\s*,\s*', ',', cmd) # delete spaces around commas cmd = re.sub('\s*=\s*', '=', cmd) # delete spaces around = # find variable assignment m = re.compile('^\$(?P<lval>\w+)=(?P<rval>.*)$').match(cmd) if m != None: lval = m.group('lval') cmd = m.group('rval') else: lval = "" # find variable usages and replace them by value m = re.compile('^.*\$(?P<id>\w+).*$').match(cmd) if m != None: while m != None: var = "\$" + m.group('id') try: repl = symTable[m.group('id')] except KeyError: raise Syntax("Unknown variable name $" + var) cmd = re.sub(var, repl, cmd) m = re.compile('^.*\$(?P<id>\w+).*$').match(cmd) result = result + "-- " + str(test) + "(in line " + str( line) + "):\n" # print (cmd) # parse and execute the command last = toolMediator.ParseCommand(cmd, name, line) lines = last.split('\n') pathexp = re.compile('^.*/(.*\.(vdm|vpp|api))$') for l in lines: # neutralize path result = result + pathexp.sub(r'\1', l) + "\n" last = re.sub('\n$', '', last) last = re.sub('\n', ',', last).strip() if lval != "": symTable[lval] = last except CORBA.COMM_FAILURE: raise CORBA.COMM_FAILURE except CORBA.BAD_PARAM: result = result + "CORBA.BAD_PARAM in " + neutralName + ", line " + str( line) + "\n" except ToolboxAPI.APIError: _, e, _ = sys.exc_info() result = result + "APIError in " + neutralName + ", line " + str( line) + ": " + e.msg + "\n" except VDM.VDMError: _, e, _ = sys.exc_info() result = result + "VDMError in " + neutralName + ", line " + str( line) + ": " + str(e.err) + "\n" except Syntax: _, e, _ = sys.exc_info() result = result + "Syntax error in " + neutralName + ", line " + str( line) + ": " + e.GetMessage() + "\n" except CORBA.UserException: result = result + "CORBA.UserException: in " + baseName + ".api, line " + str( line) + "\n" except CORBA.SystemException: _, e, _ = sys.exc_info() print(e) result = result + "CORBA.SystemException: in " + baseName + ".api, line " + str( line) + "\n" except Exception: _, e, _ = sys.exc_info() print("Python exception") print(e.args) result = result + "Python exception in " + baseName + ".api, line " + str( line) + "\n" except: result = result + "Unknown exception in " + baseName + ".api, line " + str( line) + "\n" ok = util.WriteFile(baseName + ".tmp", result) if ok: resFile = resfile.FindResFile(name) ok = (resFile != None) if ok: ok = resfile.CompareResult(name, baseName + ".tmp", resFile, None) if util.CleanFile(ok): util.DeleteFiles([baseName + ".tmp"]) return ok
def RunSpecTestCases(fullNames, lang, coverageFile): global vdmde, speccompare, backend, javac, java, vppde global staticSemanticsOnly, parserOnly testvdm = "test-j2v.vdm" if parserOnly == "yes": testvdm = "test-javamain.vdm" if staticSemanticsOnly == "yes": testvdm = "test-jss.vdm" testvdm = setup.BaseDir + "/../j2v-test/" + testvdm cmd = vdmde + " -DP -a -b -R " + coverageFile + " " + testvdm (exitCode, dummy1, dummy2) = util.RunCommand( cmd, 0, "Possible core dump while interpreting specification.", true) ok = exitCode == 0 if exitCode != 0: report.Error("Toolbox returns an error ", "'" + dummy1 + "'") if ok and dummy1.find("Run-Time Error") != -1: report.Error( "Toolbox returns an run-time error. Look for the latest .arg.err file ", "") modules = [] if parserOnly == "no" and ok: # # Run backend for the VDM++ AS value to get # VDM++ concrete syntax # for fullName in fullNames: bn = GetTestName(fullName) semResName = bn + ".arg.res" if not os.path.exists(semResName): continue if staticSemanticsOnly == "yes": util.CopyFile(semResName, fullName + "/" + semResName) report.setTestCaseName(fullName) modules.append(fullName) # # The second arg should be directory where to put all # generated .vpp # if staticSemanticsOnly == "no": if AS2CS(semResName, os.getcwd()): # # Run java to produce expected result # expRes = os.getcwd() + "/" + GetTestName( fullName) + ".res.exp" # # If test directory contains its own MAIN.java, use it, # otherwise, use the generic MAIN.java in the current dir # j2vutilJava = setup.BaseDir + "/../j2v-test/J2VUTIL.java" genericMainJava = setup.BaseDir + "/../j2v-test/MAIN.java" mainJava = fullName + "/MAIN.java" if not os.path.exists(mainJava): mainJava = genericMainJava javaFiles = fullName + "/*.java " + mainJava + " " + j2vutilJava + " " # # Compile MAIN.java and J2VUTIL.java using # classes of test java files prepared earlier # if not util.IsWindowsOS(): cmd = javac + " -d " + os.getcwd() + " " + javaFiles else: cmd = javac + " -classpath ./VDM.jar -d ./ " + javaFiles (exitCode, dummy1, dummy2) = util.RunCommand(cmd, 0, "Javac returns errors. ") if exitCode != 0: break if not util.IsWindowsOS(): cmd = java + " -classpath $CLASSPATH:" + os.getcwd( ) + " MAIN" else: cmd = java + " MAIN" (exitCode, dummy1, dummy2) = util.RunCommand(cmd, 0, "Java returns errors. ", true) if exitCode != 0: break if not util.WriteFile(expRes, dummy1): break # # Run vppde to produce the result itself # prefix = os.getcwd() + "/" + GetTestName(fullName) res = bn + ".res" vpparg = "vpp.arg" # # Create arg file # util.WriteFile(vpparg, "new A().Test()") objectVpp = setup.BaseDir + "/../j2v-test/Object.vpp" vppFiles = "" if not util.IsWindowsOS(): FID = os.popen("find " + os.getcwd() + " -type f -name \*.vpp") else: FID = os.popen("find ./ -type f -name \*.vpp") line = FID.readline() while (line != ''): vppFiles = vppFiles + line[:-1] + " " line = FID.readline() vppFiles = vppFiles + " " + objectVpp cmd = vppde + " -iD -O " + res + " " + vpparg + " " + vppFiles (exitCode, dummy1, dummy2) = util.RunCommand(cmd, 0, "Vppde returns errors. ", true) if exitCode != 0: report.Error("Vppde returns errors " + fullName, "") filesToDelete = [] FID = os.popen("ls *.vpp *.class 2>/dev/null") line = FID.readline() while (line != ''): filesToDelete.append(line[:-1]) line = FID.readline() util.DeleteFiles(filesToDelete) okNames = [] # # Now validate the results # for fullName in fullNames: argErr = bn + ".arg.err" semResName = bn + ".arg.res" resName = bn + ".res" expSemResName = fullName + "/" + bn + ".arg.res.exp" expResName = bn + ".res.exp" report.setTestCaseName(fullName) ok = true if ok and staticSemanticsOnly == "no" and parserOnly == "no": if not os.path.exists(resName): report.Error( "No result file generated for test case " + fullName, "Maybe the code generation failed for " + "one of the previous test cases") ok = false if ok: if staticSemanticsOnly == "no" and parserOnly == "no": if os.path.exists(expResName): ok = resfile.CompareResult(bn, resName, expResName, speccompare, false) else: if staticSemanticsOnly == "yes" and parserOnly == "no": if os.path.exists(semResName): ok = resfile.CompareResult(bn, semResName, expSemResName, speccompare, false) else: report.Error( "No result file generated for test case " + fullName, "Look for the latest .arg.err file ") ok = false if parserOnly == "yes": if os.path.exists(argErr): errorText = util.ReadFile(argErr) if errorText != "": report.Error("Parser failure " + fullName, errorText) if ok: okNames.append(fullName) return (okNames, modules)
result = result + "APIError in " + neutralName + ", line " + ` line ` + ": " + e.msg + "\n" except VDM.VDMError, e: result = result + "VDMError in " + neutralName + ", line " + ` line ` + ": " + ` e.err ` + "\n" except Syntax, e: result = result + "Syntax error in " + neutralName + ", line " + ` line ` + ": " + e.GetMessage( ) + "\n" except CORBA.UserException: result = result + "CORBA.UserException: in " + baseName + ".api, line " + ` line ` + "\n" except CORBA.SystemException, e: print e result = result + "CORBA.SystemException: in " + baseName + ".api, line " + ` line ` + "\n" except Exception, e: print "Python exception" print e.args result = result + "Python exception in " + baseName + ".api, line " + ` line ` + "\n" except: result = result + "Unknown exception in " + baseName + ".api, line " + ` line ` + "\n" ok = util.WriteFile(baseName + ".tmp", result) if ok: resFile = resfile.FindResFile(name) ok = (resFile != None) if ok: ok = resfile.CompareResult(name, baseName + ".tmp", resFile, None) if util.CleanFile(ok): util.DeleteFiles([baseName + ".tmp"]) return ok
unconditional_prob = ml.NormalizedHistogram(unlabelled_data) conditional_prob = ml.NormalizedHistogram( map(dataset.FilterLabel, labelled_data)) def density_threshold(sample): return conditional_prob(sample) def semi_threshold(sample): return conditional_prob(sample) / unconditional_prob(sample) util.WriteFile( 'explain.tex', explain.ExplainDistributions(features, [('office', office), ('corridor', corridor), ('kitchen', kitchen)])) def PrintSample(sample): return ' - '.join(map(lambda x: x[1], sample)) pretty_input_space = map(lambda x: (PrintSample(x), x), input_space) for filename, title, threshold_func in [ ('density_threshold.tex', '$P(x|class \in known)$', density_threshold), ('semi_threshold.tex', '$P(x|class \in known) / P(x)$', semi_threshold) ]: util.WriteFile(
def CompileRunAndCompare(fullName, lang, type, modules): global packageMap baseName = util.ExtractName(fullName) ok = true if ok: ok = CreateArgFile(fullName, lang, modules) standardlibs = convert.GetStandardLibs() #libdir = cmdline.LookUp('java-stdlib-dir') libdir = os.path.expandvars(cmdline.LookUp('java-stdlib-dir')) for lib in standardlibs: libfile = lib + ".java" util.CopyFile(libdir + "/" + libfile, libfile) if ok: ok = CompileJavaFiles(fullName, lang, type, modules) for mod in packageMap.keys(): packagePaths = string.split(packageMap[mod], '.') packageDir = string.join(packagePaths, '/') if not os.path.exists(packageDir): os.makedirs(packageDir) report.Progress(4, "cp " + mod + "*.class " + packageDir + "/.") exitCode = os.system("cp " + mod + "*.class " + packageDir + "/.") interpreter = os.path.expandvars( cmdline.LookUpWildCard('java', lang, type, 'interpreter')) if ok: # Execute the binary #flags = cmdline.LookUpWildCard('java',lang,type,'rtflags') flags = os.path.expandvars( cmdline.LookUpWildCard('java', lang, type, 'rtflags')) #if util.IsWindowsOS(): # flags = string.replace(flags, ":", ";") (exitCode, stdout, stderr) = util.RunCommand(interpreter + " " + flags + " TMAIN", 0, "Error running Java " + "generated binary", true) print("java " + flags + " TMAIN run") ok = (exitCode == 0) if ok: ok = util.WriteFile(baseName + ".res", stdout) if ok: resFile = resfile.FindResFile(fullName) ok = (resFile != None) if ok: ok = CompareRunTimeError(fullName, resFile) lib = "" if ok == None: # It was not a runtime error interpreter = lib + cmdline.LookUpWildCard('java', lang, type, 'spec-compare') # Compare the result ok = resfile.CompareResult(fullName, baseName + ".res", resFile, interpreter) return ok
def WriteResult(fullName, result): name = util.ExtractName(fullName) + ".res" util.WriteFile(name, str(result))
def CompareResult(fullName, outputFile, resFile, interpreter, structTest=true): # compare the results using normal "diff" actualResult = util.ReadFile(outputFile) expectedResult = util.ReadFile(resFile) # Remove duplicate white spaces and line breaks, spaces around commas and parenthesis. actualResult = re.sub("\s+", " ", actualResult).strip() expectedResult = re.sub("\s+", " ", expectedResult).strip() actualResult = re.sub("\s*,\s*", ",", actualResult).strip() expectedResult = re.sub("\s*,\s*", ",", expectedResult).strip() actualResult = re.sub("\s*\(\s*", "(", actualResult).strip() expectedResult = re.sub("\s*\(\s*", "(", expectedResult).strip() actualResult = re.sub("\s*\)\s*", ")", actualResult).strip() expectedResult = re.sub("\s*\)\s*", ")", expectedResult).strip() if actualResult == expectedResult: return true # Expected Result is Run Time Error if expectedResult.startswith("Run-Time Error"): report.Error( "Actual result is different from expected result for '" + fullName + "'", "expected result : " + expectedResult + "\n" + "actual result : " + actualResult) return false if expectedResult.startswith( "<UTIL.NumberToInt>:") or actualResult.startswith( "<UTIL.NumberToInt>:"): report.Error( "Actual result is different from expected result for '" + fullName + "'", "expected result : " + expectedResult + "\n" + "actual result : " + actualResult) return false if expectedResult.startswith( "The construct is not supported:") or actualResult.startswith( "The construct is not supported:"): report.Error( "Actual result is different from expected result for '" + fullName + "'", "expected result : " + expectedResult + "\n" + "actual result : " + actualResult) return false report.Progress( 4, "Comparing result for '" + fullName + "' using diff method") # Hmmm we need to try to compare using VDMDE then. if structTest and interpreter != None: report.Progress( 4, "Comparing result for '" + fullName + "' by build VDM value") template = util.GetTemplate("compare-result") if template == None: return false data = util.SubString('<<EXPECTED_RESULT>>', expectedResult, template) data = util.SubString('<<ACTUAL_RESULT>>', actualResult, data) ok = util.WriteFile("compare.vdm", data) if not ok: return false util.CopyFile(setup.BaseDir + "/general-files/compare.arg", "compare.arg") cmd = interpreter + " -i compare.arg compare.vdm" (exitcode, dummy1, dummy2) = util.RunCommand(cmd) ok = (exitcode == 0) else: ok = false if not ok: report.Error( "Actual result is different from expected result for '" + fullName + "'", "expected result : " + expectedResult + "\n" + "actual result : " + actualResult) if util.CleanFile(ok): util.DeleteFiles(["comapre.arg"]) return ok
def RunImplTestCase(fullName, lang, dtc): # Remove the files we expect output in to util.RemoveTestFiles(fullName, [".arg.res"]) # Run the test interpreter = cmdline.LookUpWildCard('ip', lang, 'impl', 'interpreter', dtc) localName = util.ExtractName(fullName) + ".vdm" outputFile = util.ExtractName(fullName) + ".res" argFile = util.ExtractName(fullName) + ".arg" logFile = argFile + ".logfile" ok = SetupArgFile(fullName,lang) if not ok: return false if dtc == 'dtcon': dtccmd = "-DPIQ" else: dtccmd = "" cmd = interpreter + " -i " + dtccmd + " -O " + outputFile + " " + argFile + " " + localName util.DeleteFiles([outputFile]) if lang == "rt": if(os.path.exists("logfile")): os.unlink("logfile") # Now run the interpreter (exitCode, stdout, stderr) = util.RunCommand(cmd, None, None, true) if lang == "rt": if(os.path.exists("logfile")): os.rename('logfile', logFile); if not os.path.exists(outputFile): util.WriteFile(outputFile,stdout); exitCode = 1 expResultFile = resfile.FindResFile(fullName) if expResultFile == None: ok = false if ok: if exitCode != 0: ## Maybe a runtime error occured. # Note the order of the next binary expression ensures that # CompareRunTimeError is executed even though no expected result was # found! This is necesary as this function generates the result one # can copy in place ok = CompareRunTimeError(fullName, expResultFile, stdout) and ok return ok # Was output produced? if not os.path.exists(outputFile): report.Error("'" + cmd + "' didn't produce the expected result file: '" + outputFile + "'", "Either command was malformed, or the interpreter crashed.") return false interpreter = cmdline.LookUpWildCard('ip', lang, 'impl', 'spec-compare', dtc) ok = resfile.CompareResult(fullName, outputFile, expResultFile, interpreter) return ok