def convert_tabs_to_spaces(file_path): with open(file_path, mode='rt') as infile: lines = infile.readlines() with open(file_path, mode='wt') as outfile: for line in lines: stripped = re.sub('[\t]+', ' ', line) outfile.write(stripped) if sys.platform.startswith("win"): convert_line_endings(file_path)
def remove_bom(file_path): with open(file_path, mode='rt') as infile: lines = infile.readlines() if len(lines) == 0: return with open(file_path, mode='wt') as outfile: first_line = lines[0] remaining_lines = lines[1:] first_line_offset = 0 for bom, length in boms: if first_line.startswith(bom): first_line_offset = length print "%s has BOM" % file_path break outfile.write(first_line[first_line_offset:]) for line in remaining_lines: outfile.write(line) if sys.platform.startswith("win"): convert_line_endings(file_path)
def assign_license(file_path): with open(file_path, mode='rt') as infile: lines = infile.readlines() first_code_line = 0 has_region = False has_c_style_comment = False is_python = os.path.splitext(file_path)[1] == '*.py' shebang = '' for index, line in enumerate(lines): if not line: continue if is_python and line.startswith('#!'): shebang = line continue if line.startswith('#region License'): has_region = True if line.startswith('/*'): has_c_style_comment = True if has_region: if line.startswith('#endregion'): first_code_line = index + 1 break elif has_c_style_comment: if line.strip().endswith('*/'): first_code_line = index + 1 break else: if not line.startswith('//'): first_code_line = index break code_lines = lines[first_code_line:] with open(file_path, mode='wt') as outfile: if is_python and shebang: outfile.write(shebang) write_license_text(outfile, file_path) for line in code_lines: outfile.write(line) if sys.platform.startswith("win"): convert_line_endings(file_path)
def assign_license(file_path): with open(file_path, mode='rt') as infile: lines = infile.readlines() first_code_line = 0 has_region = False has_c_style_comment = False is_python = os.path.splitext(file_path)[1] == '*.py' shebang = '' for index, line in enumerate(lines): if not line: continue if is_python and line.startswith('#!'): shebang = line continue if line.startswith('#region License'): has_region = True if line.startswith('/*'): has_c_style_comment = True if has_region: if line.startswith('#endregion'): first_code_line = index + 1 break elif has_c_style_comment: if line.strip().endswith('*/'): first_code_line = index + 1 break else: if not line.startswith('//'): first_code_line = index break code_lines = lines[first_code_line:] with open(file_path, mode='wt') as outfile: if is_python and shebang: outfile.write(shebang) write_license_text(outfile, file_path) for line in code_lines: outfile.write(line) if sys.platform.startswith("win"): convert_line_endings(file_path)