forked from cmccabe/cmccabe-hbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trimr.py
executable file
·150 lines (139 loc) · 4.15 KB
/
trimr.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
#!/usr/bin/python
#
# On the Hadoop JIRA bug tracker, sometimes large patches are presented as two
# files: one file which has a list of "svn rm" commands, and another with the
# actual diff.
# This script takes a "normal" patch file generated by git diff, svn diff, or
# the patchr.py script, and turns it into this two file hybrid.
#
import getopt
import os
import re
import stat
import string
import subprocess
import sys
output_re = re.compile("^(.*).patch$")
svn_rm_re = re.compile("svn rm ([^ ]*)")
git_diff_re= re.compile("diff --git ([^ ]*) ([^ ]*)")
deletion_re = re.compile("\+\+\+ /dev/null")
## Parse options
def print_usage():
print os.path.basename(
sys.argv[0]) + ": a program to remove files from a patch.\n\
The files to be removed must be specified in a special 'svn rm' file.\n\
\n\
Usage: " + os.path.basename(sys.argv[0]) + " [options]\n\
\n\
Options: -h: this help message\n\
-p <file-name>: specify patch file (required)\n\
-v: turn on verbose mode (to stderr)\n\
"
try:
optlist, dirs = getopt.getopt(sys.argv[1:], ':hOp:r:v')
except getopt.GetoptError:
print_usage()
sys.exit(1)
in_fname = None
verbose = False
for opt in optlist:
if opt[0] == '-h':
print_usage()
sys.exit(0)
if opt[0] == '-p':
in_fname = opt[1]
if opt[0] == '-v':
verbose = True
if (in_fname == None):
print >>sys.stderr, "You must specify a patch name."
print_usage()
sys.exit(1)
m = output_re.match(in_fname)
if (not m):
print >>sys.stderr, "Failed to automatically determine name of \
output(s). Aborting."
sys.exit(1)
pout_fname = m.group(1) + ".trimmed.patch"
if (os.path.exists(pout_fname)):
print >>sys.stderr, "File '" + pout_fname + "' already exists! \
Aborting"
sys.exit(1)
poutf = open(pout_fname, "w")
rout_fname = m.group(1) + ".rm.patch"
rms = {}
if (os.path.exists(rout_fname)):
if (verbose):
print >>sys.stderr, "Using existing svn rm file."
f = open(rout_fname, "r")
try:
lineno = 0
for line in f:
lineno = lineno + 1
m = svn_rm_re.match(line)
if (not m):
raise RuntimeError("failed to parse line " + lineno + " of " +
rout_fname)
rms[str(m.group(1)).rstrip()] = 1
finally:
f.close()
else:
if (verbose):
print >>sys.stderr, "Creating new svn rm file."
f = open(in_fname, "r")
try:
lineno = 0
cur_file = None
for line in f:
lineno = lineno + 1
m = deletion_re.match(line)
if (m):
if (cur_file == None):
raise RuntimeError("saw deletion, but not the file name \
it was associated with!")
rms[cur_file] = 1
continue
m = git_diff_re.match(line)
if (m):
cur_file = str(m.group(1))
finally:
f.close()
if len(rms.keys()) != 0:
f = open(rout_fname, "w")
try:
for k in rms.keys():
print >>f, "svn rm " + k
finally:
f.close()
if (len(rms.keys()) == 0):
print >>sys.stderr, "There were no deleted files found. The trimmed \
patch would be the same as the original patch."
sys.exit(0)
if (verbose):
print >>sys.stderr, "removing " + str(len(rms.keys())) + " files from the patch..."
printing = True
f = open(in_fname, "r")
try:
for line in f:
m = git_diff_re.match(line)
if (not m):
if (printing):
print >>poutf, line,
else:
fname = str(m.group(1))
#print >>sys.stderr, "fname = '" + fname + "'"
if (rms.has_key(fname)):
if (verbose):
print >>sys.stderr, "skipping " + fname
del rms[fname]
printing = False
else:
printing = True
print >>poutf, line,
finally:
f.close()
if (len(rms.keys()) != 0):
print >>sys.stderr, "ERROR! Failed to use all svn rm directives! Unused:"
for k in rms.keys():
print >>sys.stderr, str(k)
raise RuntimeError("failed to use all svn directives")
sys.exit(0)