-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathmark_change.py
80 lines (70 loc) · 2.88 KB
/
mark_change.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import sys
import subprocess
import argparse
import shutil
class ChangeReview(object):
def __init__(self, filenames):
self.files = filenames
self.check_file_names()
#self.files = ['planning/planning_index.adoc', 'creating/creating_index.adoc']
self.projdir = os.getcwd()
self.outdir = os.path.join(self.projdir, 'output')
def check_file_names(self):
for fname in self.files:
if not (fname.endswith(".adoc") or fname.endswith(".asciidoc")):
sys.exit("{} does not end in .adoc or .asciidoc".format(fname))
if not os.path.exists(fname):
sys.exit("Unable to find {}".format(fname))
def process_file(self):
for fname in self.files:
if self.create_original(fname):
if self.create_patch(fname):
self.patch_file(fname)
def create_original(self, cfile):
basename = os.path.basename(cfile)
outdir = os.path.join(self.outdir, os.path.dirname(cfile))
cmd = ['git', 'show', 'HEAD~1:{}'.format(cfile)]
print cmd
result = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, stderr = result.communicate()
if not os.path.exists(outdir):
os.makedirs(outdir)
if result.returncode != 0:
foo = subprocess.Popen(['pwd'])
foo.communicate()
shutil.copyfile(os.path.join(self.projdir, cfile), os.path.join(outdir, os.path.basename(cfile)))
return False
output = open(os.path.join(outdir, basename), 'w')
output.write(stdout)
output.close()
return True
def create_patch(self, cfile):
outdir = os.path.join(self.outdir, os.path.dirname(cfile))
basename = os.path.basename(cfile)
cmd = ['git', 'diff', 'HEAD~1', cfile]
foo = subprocess.check_output(cmd)
if foo == '':
return False
new_file_lines = []
for line in foo.splitlines():
if line.startswith('+') and not line.startswith('++'):
new_file_lines.append(line[:1] + '[aqua-background]##' + line[1:] + "##")
else:
new_file_lines.append(line)
output = open(os.path.join(outdir, basename + '.patch'), 'w')
output.write("\n".join(new_file_lines) + "\n")
output.close()
return True
def patch_file(self, cfile):
outdir = os.path.join(self.outdir, os.path.dirname(cfile))
basename = os.path.basename(cfile)
os.chdir(outdir)
print os.curdir
cmd = ['patch', '-p1', basename, basename + '.patch']
subprocess.check_call(cmd)
parser = argparse.ArgumentParser(description='Change markup based on a diff')
parser.add_argument('filenames', nargs='+', help='filenames')
args = parser.parse_args()
cr = ChangeReview(args.filenames)
cr.process_file()