Skip to content

Instantly share code, notes, and snippets.

@yamaguchi1024
Created March 14, 2018 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamaguchi1024/d95843a5e549fcc6ba0e6e23da5c132a to your computer and use it in GitHub Desktop.
Save yamaguchi1024/d95843a5e549fcc6ba0e6e23da5c132a to your computer and use it in GitHub Desktop.
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