Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os
import sys
import subprocess
import re
args = sys.argv
directory = args[1]
if not directory.endswith("/"):
directory += "/"
files = os.listdir(directory)
for file in files:
e = "include-what-you-use " + directory + file
res = subprocess.Popen(e, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderror = res.communicate()
e_array = stderror.decode('utf-8').split("\n")
# Make list of lines which has to be removed
pattern = r"[0-9]+\.?[0-9]*"
rm_number = []
for r_line in e_array:
if ("// lines") in r_line:
rm_number.append(int(re.search(pattern, r_line).group()))
print(rm_number)
# Open, readlines, close
f = open(directory + file, "r")
lines = f.readlines()
f.close()
# Reopen and rewrite
f = open(directory + file, "w")
for number, line in enumerate(lines):
if (number in rm_number) and (("#include <") in line):
continue
else:
f.write(line)
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment