-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpatchtool.py
executable file
·171 lines (138 loc) · 4.98 KB
/
patchtool.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# vim:sw=8:ts=8:et:nowrap
import optparse
import os
import re
import shutil
import sys
import tempfile
import miscfuncs
# change current directory to pyscraper folder script is in
os.chdir(os.path.dirname(sys.argv[0]) or ".")
from resolvemembernames import memberList
toppath = miscfuncs.toppath
# File names of patch files
# this is horid since it shadows stuff that's done distributively in the scrapers
def GenPatchFileNames(typ, sdate):
qfolder = toppath
qfolder = os.path.join(qfolder, "cmpages")
# transform the typ into the file stub
if typ == "wrans":
stub = "answers"
elif typ == "lords" or typ == "lordspages":
typ = "lordspages"
stub = "daylord"
elif typ == "westminhall":
stub = "westminster"
elif typ == "wms":
stub = "ministerial"
elif typ == "standing":
stub = "standing"
elif typ[0:9] == "chgpages/":
stub = re.sub("chgpages/", "", typ)
else:
stub = typ
extension = "json" if typ == "ni" else "html"
folder = os.path.join(qfolder, typ)
# lords case where we use the new top level patch directory
pdire = os.path.join(toppath, "patches")
# all patches will be moved to where they belong
# if typ != "lordspages":
# pdire = "patches" # as local directory
pdire = os.path.join(pdire, typ)
if not os.path.isdir(pdire):
os.mkdir(pdire)
patchfile = os.path.join(pdire, "%s%s.%s.patch" % (stub, sdate, extension))
orgfile = os.path.join(folder, "%s%s.%s" % (stub, sdate, extension))
tmpfile = tempfile.mktemp(
".%s" % extension, "patchtmp-%s%s-" % (stub, sdate), miscfuncs.tmppath
)
tmppatchfile = os.path.join(pdire, "%s%s.%s.patch.new" % (stub, sdate, extension))
return patchfile, orgfile, tmpfile, tmppatchfile
# Launches editor on copy of file, and makes patch file of changes the user
# makes interactively
def RunPatchToolW(typ, sdate, stamp, frag):
(patchfile, orgfile, tmpfile, tmppatchfile) = GenPatchFileNames(typ, sdate)
shutil.copyfile(orgfile, tmpfile)
if os.path.isfile(patchfile):
print("Patching ", patchfile)
status = os.system('patch --quiet "%s" < "%s"' % (tmpfile, patchfile))
# run the editor (first finding the line number to be edited)
gp = 0
finforlines = open(tmpfile, "r")
rforlines = finforlines.read()
finforlines.close()
if stamp:
aname = stamp.GetAName()
ganamef = re.search(
('<a name\s*=\s*"%s">([\s\S]*?)<a name(?i)' % aname), rforlines
)
if ganamef:
gp = ganamef.start(1)
else:
ganamef = None
if not frag:
fragl = -1
elif ganamef:
fragl = ganamef.group(1).find(str(frag))
else:
fragl = rforlines.find(str(frag))
if fragl != -1:
gp += fragl
gl = rforlines.count("\n", 0, gp)
gc = 0
if gl:
gc = gp - rforlines.rfind("\n", 0, gp)
# print "find loc codes ", gp, gl, gc
if 1 == 0 and sys.platform == "win32":
os.system(
'"C:\Program Files\ConTEXT\ConTEXT" %s /g%d:%d' % (tmpfile, gc + 1, gl + 1)
)
else:
# TODO add column support using gc + 1, if you can work out vim's syntax
editor = os.getenv("EDITOR")
if not editor:
editor = "vim"
os.system('%s "%s" +%d' % (editor, tmpfile, gl + 1))
# now create the diff file
if os.path.isfile(tmppatchfile):
os.remove(tmppatchfile)
ern = os.system('diff -u "%s" "%s" > "%s"' % (orgfile, tmpfile, tmppatchfile))
if ern == 2:
print("Error running diff")
sys.exit(1)
os.remove(tmpfile)
if os.path.isfile(patchfile):
os.remove(patchfile)
if os.path.getsize(tmppatchfile):
os.rename(tmppatchfile, patchfile)
print("Making patchfile ", patchfile)
def RunPatchTool(typ, sdatext, ce):
if not ce.stamp:
print("No stamp available, so won't move your cursor to right place")
else:
assert ce.stamp.sdate[:10] == sdatext[:10] # omitting the letter extension
print("\nHit RETURN to launch your editor to make patches ")
sys.stdin.readline()
RunPatchToolW(typ, sdatext, ce.stamp, ce.fragment)
memberList.reloadJSON()
# So it works from the command line
if __name__ == "__main__":
parser = optparse.OptionParser()
(options, args) = parser.parse_args()
args = [sys.argv[0]] + args
# print args
if len(args) != 3:
print("""
This generates files for the patchfilter.py filter.
They are standard patch files which apply to the glued HTML files which we
download from Hansard. Any special errors in Hansard are fixed by
these patches.
Run this tool like this:
./patchtool.py wrans 2004-03-25
This will launch your editor, and upon exit write out a patch of your changes
in the patches folder underneath this folder. The original file is
untouched. We consider the patches permanent data, so add them to CVS.
""")
sys.exit(1)
RunPatchToolW(args[1], args[2], None, "")