/
safewrite.py
53 lines (49 loc) · 1.78 KB
/
safewrite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import unicode_literals
from __future__ import print_function
from io import StringIO
import os
from difflib import context_diff
import sys
from subprocess import Popen, PIPE
from colored import colored
try:
from clang_format import _get_executable as get_executable
except:
def get_executable(_):
return "/bin/cat"
clang_formatter = get_executable("clang-format")
verbose = False
nochange = False
class SafeWrite:
def __init__(self, fname, do_format=False):
self.fname = os.path.abspath(fname)
self.fd = None
self.do_format = do_format
def __enter__(self):
self.fd = StringIO()
return self.fd
def __exit__(self, ty, val, tb):
print("Checking",self.fname,end="...")
newcontent = self.fd.getvalue()
if self.do_format:
pipe = Popen([clang_formatter],stdout=PIPE,stdin=PIPE,universal_newlines=True)
out, err = pipe.communicate(newcontent)
newcontent = out
if os.path.exists(self.fname):
with open(self.fname) as fd:
oldcontent = fd.read()
do_write = newcontent.strip() != oldcontent.strip()
if do_write and verbose:
print("Diff for:",self.fname)
oldlines=[line+"\n" for line in oldcontent.strip().split("\n")]
newlines=[line+"\n" for line in newcontent.strip().split("\n")]
sys.stdout.writelines(context_diff(oldlines,newlines,fromfile='before',tofile='after'))
else:
do_write = True
if do_write:
assert nochange == False
with open(self.fname, "w") as fd:
fd.write(newcontent)
print(colored("[written]","red"))
else:
print(colored("[no changes]","green"))