def __get_file_content(self): format_c_code([self.__file_path, ], column_limit=False) try: with open(self.__file_path, 'r') as input_file: self.__file_content = input_file.read() except FileNotFoundError: raise FileError(f'File {self.__file_path} dose not exist') if not self.__file_content: raise FileError(f'The file {self.__file_path} is empty')
def format_c_code(c_files_path_list: list, column_limit: bool = True): try: run_subprocess(get_format_command(c_files_path_list, column_limit)) directives_handler(c_files_path_list) run_subprocess(get_format_command(c_files_path_list, column_limit=True)) directives_handler(c_files_path_list, back=True) except subprocess.CalledProcessError as e: raise FileError(f'Format Code Error: clang-format return with {e.returncode} code: {e.output}') except FileNotFoundError as e: raise FileError(e)
def __create_enviroment(self): with Templater.cd(self.template_folder): additional_files = [ f for f in os.listdir(".") if not f.endswith(".html") ] for f in additional_files: try: if os.path.isdir(f): name = self.deploy_folder + "/" + os.path.basename(f) try: copytree(f, name) print "[OK] Copied folder {0} to {1}".format( f, name) except OSError: pass else: copy2(f, self.deploy_folder) print "[OK] Copied {0} to {1}".format( f, self.deploy_folder) except IOError as e: print "[WARN] Can't copy {}".format(f) self.templates_content = {} for t, filename in self.templates.iteritems(): try: with open(filename) as fh: content = fh.read().decode("utf8") except: raise FileError(filename) if t == "page": self.templates_content[t] = Page(content) elif t == "map": self.templates_content[t] = Map(content) elif t == "index": self.templates_content[t] = Index(content)
def inject_code_at_the_top(file_path: str, code_to_be_injected: str): try: with open(file_path, 'r+') as f: file_content = f.read() file_content = code_to_be_injected + '\n' + file_content f.seek(0) f.write(file_content) f.truncate() except Exception as e: raise FileError(str(e))
def remove_code_from_file(file_path: str, code_to_be_removed: str): try: with open(file_path, 'r+') as f: file_content = f.read() file_content = file_content.replace(code_to_be_removed, '') f.seek(0) f.write(file_content) f.truncate() except Exception as e: raise FileError(str(e))
def writeTokenFile(self, data): """Writes the updated token details to the token file. Takes list or tuple, and writes each element to a new line. """ try: tokenFile = open(self.__fileName, 'w') dataStr = tuple(map(str, data)) toWrite = '\n'.join(dataStr) tokenFile.write(toWrite) except: raise FileError("Cannot create token file") from None
def __analysis_output_file(self): combination_id = self.get_job().get_combination().get_combination_id() logger.info( f'{ExecuteJob.__name__}: analyzing job run time results of {combination_id} combination' ) for root, dirs, files in os.walk(self.get_job().get_directory_path()): for file in files: # total run time analysis if re.search(rf"{TimerConfig.TOTAL_RUNTIME_FILENAME}$", file): total_runtime_file_path = os.path.join(root, file) with open(total_runtime_file_path, 'r') as f: self.get_job().set_total_run_time(float(f.read())) # loops runtime analysis if re.search(f"{TimerConfig.LOOPS_RUNTIME_RESULTS_SUFFIX}$", file): loops_dict = {} file_full_path = os.path.join(root, file) file_id_by_rel_path = os.path.relpath( file_full_path, self.job.directory) file_id_by_rel_path = file_id_by_rel_path.replace( f"{TimerConfig.LOOPS_RUNTIME_RESULTS_SUFFIX}", ".c") self.get_job().set_file_results(file_id_by_rel_path) try: with open(file_full_path, 'r') as input_file: for line in input_file: if TimerConfig.LOOPS_RUNTIME_SEPARATOR in line: loop_label, loop_runtime = line.replace( '\n', '').split(TimerConfig. LOOPS_RUNTIME_SEPARATOR) loop_runtime = float(loop_runtime) loops_dict[loop_label] = loop_runtime ran_loops = list(loops_dict.keys()) for i in range( 1, self.num_of_loops_in_files[file_id_by_rel_path] [0] + 1): if str(i) not in ran_loops: self.get_job().set_loop_in_file_results( file_id_by_rel_path, i, None, dead_code=True) else: self.get_job().set_loop_in_file_results( file_id_by_rel_path, str(i), loops_dict[str(i)]) except OSError as e: raise FileError(str(e))
def __write_to_file(self, content: str): try: with open(self.__file_path, 'w') as output_file: output_file.write(content) except OSError as e: raise FileError(str(e))
def get_file_content(file_path: str): try: with open(file_path, 'r') as input_file: return input_file.read() except FileNotFoundError: raise FileError(f'File {file_path} dose not exist')