def create_batch_file(dirs, cflags, lflags): contents = "" contents += "\nrem NOTE: this batch file is to be run in a Visual Studio command prompt\n" contents += "\nrem Delete old files\n" contents += "del " + "*.obj\n" contents += "del " + "*.ilk\n" contents += "del " + "*.exe\n" contents += "\nrem Delete, compile and link files into .obj files in current directory\n" contents += 'cl ' + cflags + \ ' "testcasesupport\main.cpp"' + \ ' "testcasesupport\io.c"' + \ ' "testcasesupport\std_thread.c"\n' for dir in sorted(dirs): # testcasesupport is a special directory and needs to be handled separately if 'testcasesupport' not in dir: contents += 'cl ' + cflags + " " # Only add *.c if there is a .c file in the dir - there will always be at least 1 .cpp file (main.cpp) if py_common.find_files_in_dir(dir, "CWE.*\.c$"): contents += '"' + os.path.relpath(dir) + '\\CWE*.c" ' if py_common.find_files_in_dir(dir, "CWE.*\.cpp$"): contents += '"' + os.path.relpath(dir) + '\\CWE*.cpp"' contents += '\n' contents += 'cl ' + "/Fe" + "Testcases" + " *.obj " + lflags + "\n" return contents
def create_batch_file(dirs, cflags, lflags): contents = "" contents += "\nrem NOTE: this batch file is to be run in a Visual Studio command prompt\n" contents += "\nrem Delete old files\n" contents += "del " + "*.obj\n" contents += "del " + "*.ilk\n" contents += "del " + "*.exe\n" contents += "\nrem Delete, compile and link files into .obj files in current directory\n" contents += 'cl ' + cflags + \ ' "testcasesupport\main.cpp"' + \ ' "testcasesupport\io.c"' + \ ' "testcasesupport\std_thread.c"\n' for dir in sorted(dirs): # testcasesupport is a special directory and needs to be handled separately if 'testcasesupport' not in dir: contents += 'cl ' + cflags + " " # Only add *.c if there is a .c file in the dir - there will always be at least 1 .cpp file (main.cpp) if py_common.find_files_in_dir(dir, "CWE.*\.c$"): contents += '"' + os.path.relpath(dir) + '\\CWE*.c" ' if py_common.find_files_in_dir(dir, "CWE.*\.cpp$"): contents += '"' + os.path.relpath(dir) + '\\CWE*.cpp"' contents += '\n' contents += 'cl ' + "/Fe" + "Testcases" + " *.obj " + lflags + "\n" return contents
def create_xml_dir(self): # create, or empty, 'xmls' folder # # Note: Deleting entire folder and then re-creating it immediately sometimes conflicts # with anti-virus sortware and cannot always release handles quick enough, so the entire # parent folder is not deleted, only the files withing it. This prevents this problem # if not os.path.exists(self.dest_path): py_common.print_with_timestamp("The path \"" + self.dest_path + "\" does not exist") py_common.print_with_timestamp("creating directory \"" + self.dest_path + "\"") os.makedirs(self.dest_path) else: py_common.print_with_timestamp( self.dest_path + " already exists. Cleaning before use...") fileList = os.listdir(self.dest_path) for fileName in fileList: # os.remove(self.dest_path + "//" + fileName) os.remove(os.path.join(self.dest_path, fileName)) # fortify files are not in standard xml format if self.tool_name == 'fortify': self.scan_data_files = py_common.find_files_in_dir( self.source_path, '.*?\.fpr$') else: self.scan_data_files = py_common.find_files_in_dir( self.source_path, '.*?\.xml$')
def check_if_cpp_files_exist(directory): files = py_common.find_files_in_dir(directory, "CWE.*\.cpp$") if len(files) > 0: return True return False
def check_if_cpp_files_exist(directory): files = py_common.find_files_in_dir(directory, "CWE.*\.cpp$") if len(files) > 0: return True return False
def get_directory_names_to_compile(directory): files = py_common.find_files_in_dir(directory, testregex) dirs = set() for file in files: base_dir = os.path.dirname(file) dirs.add(base_dir) return dirs
def get_directory_names_to_compile(directory): files = py_common.find_files_in_dir(directory, "CWE.*(\.c|\.cpp)$") dirs = set() for file in files: base_dir = os.path.dirname(file) dirs.add(base_dir) return dirs
def get_directory_names_to_compile(directory): files = py_common.find_files_in_dir(directory, "(\.c|\.cpp)$") dirs = set() for file in files: base_dir = os.path.dirname(file) dirs.add(base_dir) return dirs
def update_and_rename_Main_csproj(file_path, cwe_name, csproj_guid, testcasesupport_csproj_guid): is_split = False if os.path.basename(file_path).startswith('s'): is_split = True cwe = cwe_name if is_split: cwe += "_" + os.path.basename(file_path) src_file = os.path.join(file_path, "Main.csproj") dest_file = os.path.join(file_path, cwe + ".csproj") # If the project file already exists, delete it first or the rename function won't work on Windows if os.path.isfile(dest_file): os.remove(dest_file) os.rename(src_file, dest_file) contents = py_common.open_file_and_get_contents(dest_file) contents = contents.replace("$CWE$", cwe) contents = contents.replace("$CWE_ROOT$", cwe_name) contents = contents.replace("$CWE_CSPROJ_GUID$", csproj_guid) contents = contents.replace("$TCS_CSPROJ_GUID$", testcasesupport_csproj_guid) test_case_files = py_common.find_files_in_dir(file_path, "^CWE.*\.cs$") items = "" for test_case_file in test_case_files: # This check is necessary due to CWE468 as this CWE contains 2 identical .cs files, # however they are in different directories, so we need to ensure they both get added # correctly by adding the path to the 2nd .cs file (HelperClass) test_case_file_dir = os.path.basename(os.path.dirname(test_case_file)) if test_case_file_dir.lower().startswith('cwe') or re.match( 's\d\d', test_case_file_dir, re.IGNORECASE): items += ' <Compile Include="' + os.path.basename( test_case_file) + '" />\n' else: items += ' <Compile Include="' + os.path.join( test_case_file_dir, os.path.basename(test_case_file)) + '" />\n' contents = contents.replace("$FILE_NAMES$", items) if os.path.basename(file_path).startswith('s'): contents = contents.replace("$SPLIT_DIR$", '..\\') else: contents = contents.replace("$SPLIT_DIR$", '') py_common.write_file(dest_file, contents)