Create tool to rename lines recursively

This commit is contained in:
Andrew Hyatt 2018-06-03 15:02:43 -04:00
parent 58e53acb18
commit 65ad2b5104
No known key found for this signature in database
GPG Key ID: 5456D0F696719C24

40
tools/rename-line Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python3
import argparse
import os
import re
parser = argparse.ArgumentParser(description="Renames the starts of lines in every file")
parser.add_argument("old", help="old start of line", type=str)
parser.add_argument("new", help="new start of line", type=str)
args = parser.parse_args()
lineCount = 0
fileCount = 0
def editfile(f, old, new):
global lineCount, fileCount
contents = f.readlines()
newContents = ""
f.seek(0)
edited = False
for line in contents:
if line.startswith(old):
edited = True
lineCount += 1
line = new + line[len(old):]
newContents += line
if edited:
fileCount += 1
f.write(newContents)
f.truncate()
for dirpath, dirname, filenames in os.walk("."):
for filename in filenames:
if filename.endswith("CMakeLists.txt"):
with open(dirpath +"/" + filename, "r+") as f:
editfile(f, args.old, args.new)
print("Edited " + str(lineCount) + " lines in " + str(fileCount) + " files.")