Whamcloud - gitweb
- make HEAD from b_post_cmd3
[fs/lustre-release.git] / lustre / kernel_patches / scripts / unitdiff.py
1 #!/usr/bin/python
2
3 import sys
4 import re
5 import string
6
7 #TODO
8 # clean up rest/file
9 # clean up +6 and like (assumptions). should be turned into 'find'
10 # make regession tests for all cases (Only in, etc)
11
12 try:
13         filename = sys.argv[1]
14 except:
15         print 'requires a file name'
16         sys.exit(1)
17
18 filefd = open(filename)
19 file = filefd.read()
20 filefd.close()
21
22 rest = file
23 pat = "(^(?:diff .*\n)?--- .*\n\+\+\+ .*)?\n@@ -(\d+),?(\d+)? \+(\d+),?(\d+)? @@|^(Only in .*)"
24 startpat = re.compile(pat, re.M)
25
26 pos = 0
27 oldpos = 0
28 filelen = len(rest)
29 oldrest = ""
30 while(1):
31         rexp = startpat.search(rest)
32         if not rexp:
33                 break
34
35         if rexp.group(6):
36                 print rexp.group(6)
37                 rest = rest[rexp.end(6)+1:]
38                 continue
39                 
40         header = rexp.group(1)
41         orgfile_start = string.atoi(rexp.group(2))
42         if rexp.group(3):
43                 orgfile_len = string.atoi(rexp.group(3))
44         else:
45                 orgfile_len = -1
46         newfile_start = string.atoi(rexp.group(4))
47         if rexp.group(5):
48                 newfile_len = string.atoi(rexp.group(5))
49         else:
50                 newfile_len = -1
51         rest = rest[rexp.start(2):]
52         rest = rest[string.find(rest, "\n")+1:]
53
54         rexp2 = startpat.search(rest)
55         if rexp2:
56                 if rexp2.start(6) != -1:
57                         oldrest = rest[rexp2.start(6)-1:]
58                         rest = rest[:rexp2.start(6)]
59                 elif rexp2.start(1) == -1:
60                         oldrest = rest[rexp2.start(2)-5:]
61                         rest = rest[:rexp2.start(2)-4]
62                 else:
63                         oldrest = rest[rexp2.start(1)-1:]
64                         rest = rest[:rexp2.start(1)]
65         else:
66                 oldrest = rest
67
68 #       pos = filelen - len(oldrest)
69 #       if pos - oldpos > 100:
70 #               sys.stderr.write(`pos`+'/'+`filelen`+'\n')
71 #               oldpos = pos
72
73         first = 1
74         oldminuses = 0
75         oldplusses = 0
76         oldoffset = 0
77         while(1):
78                 #erstat early line stuff med lookbehind paa {1,2}-dims
79                 #nedenfor RAA
80                 linepat = "^([^-+\n]*)\n?(((^[-+].*\n)|^(.*\n){1,2}(?=^[-+].*\n))+)(.*)\n?"
81                 compat = re.compile(linepat, re.M)
82                 rexp = compat.search(rest)
83                 if not rexp:
84                         break
85
86                 prematch = rexp.group(1)
87                 match = rexp.group(2)
88                 muddle = len(match)
89
90 #               print rest
91 #               print 'prematch ', rexp.start(1), rexp.end(1), prematch
92 #               print 'match ---------'
93 #               print match
94 #               print 'match --------'
95
96                 # dump unwanted early lines...
97                 if match[0] != "+" and match[0] != "-":
98                         while(1):
99                                 next = string.find(match, '\n')
100                                 if next == -1:
101                                         break
102                                 if match[next+1] == "+" or match[next+1] == "-":
103                                         prematch = match[:next]
104                                         match = match[next+1:]
105                                         break
106                                 match = match[next+1:]
107
108
109 #               print 'prematch ', rexp.start(1), rexp.end(1), len(prematch)
110 #               print '('+prematch+')'
111 #               if prematch == ' ':
112 #                       print 'space'
113                 muddle = muddle - len(match)
114
115                 lines = string.count(match, "\n")
116                 compat = re.compile("^-", re.M)
117                 minuses = len(compat.findall(match))
118                 compat = re.compile("^\+", re.M)
119                 plusses = len(compat.findall(match))
120                 orgsize = minuses + 2 + (lines - minuses - plusses)
121                 newsize = plusses + 2 + (lines - minuses - plusses)
122
123                 noeol = "^(\\\ No newline at end of file)$"
124                 compnoeol = re.compile(noeol, re.M)
125                 if compnoeol.search(match) or compnoeol.search(rexp.group(6)):
126                         orgsize = orgsize - 1
127                         newsize = newsize - 1
128                         
129                 coherent = 0
130                 if lines - plusses == 0:
131                         coherent = 1
132                 elif lines - minuses == 0:
133                         coherent = 1
134
135                 # RAA FIXME
136                 if not len(prematch):#or len(prematch) == 1 and prematch == ' ':
137                         orgsize = orgsize -1
138                         newsize = newsize -1
139                 if rexp.start(6) == rexp.end(6):
140                         orgsize = orgsize -1
141                         newsize = newsize -1
142
143 #               print "lines in match: ", lines
144 #               print "number of minuses: ", minuses
145 #               print "number of plusses: ", plusses
146         
147                 matchpos = rexp.start(2) + muddle
148                 offset =  string.count(rest[:matchpos], "\n")
149
150 #               print 'offset/oldoffset: ', offset,oldoffset
151 #               print 'oldplusses/oldminuses: ', oldplusses, oldminuses
152 #               print 'orgfile_start/newfile_start: ', orgfile_start, newfile_start
153
154                 orgstart = orgfile_start + offset + oldoffset - oldplusses
155                 newstart = newfile_start + offset - oldminuses + oldoffset
156
157                 # RAA: Bwadr. Fix antagelse om prematch paa en anden
158                 # maade
159                 orgstartmod = 0
160                 newstartmod = 0
161                 if orgfile_start == 1 and not len(prematch):
162                         orgstartmod = 1
163                 if newfile_start == 1 and not len(prematch):
164                         newstartmod = 1
165                 if orgfile_start == 0 and orgfile_len == 0:
166                         orgstartmod = 1
167                         # RAA Hack!
168                         plusses = plusses + 1
169                         minuses = minuses +1
170                 if newfile_start == 0 and newfile_len == 0:
171                         newstartmod = 1
172                         # RAA Hack!
173                         plusses = plusses + 1
174                         minuses = minuses +1
175                 
176                 if header and first:
177                         print header
178                         first = 0
179
180                 # should the start(1) == 0 be orgstart == 1? RAA
181                 if orgstart == 1 and newstart == 1 and plusses == 0 and coherent:
182                         print "@@ -"+`orgstart`+","+`orgsize`+" +"+`newstart`+" @@"
183                         print match[:string.rfind(match, "\n")]
184                         print rexp.group(6)
185                 elif rexp.start(6) == rexp.end(6) and plusses == 0 and coherent:
186                         if orgstartmod:
187                                 orgstart = orgstart + 1
188                         if newstartmod:
189                                 newstart = newstart + 1
190                         print "@@ -"+`orgstart-1`+","+`orgsize`+" +"+`newstart-1`+" @@"
191                         print prematch
192                         print match[:string.rfind(match, "\n")]
193                 elif orgstart == 1 and orgstart == 1 and minuses == 0 and coherent:
194                         print "@@ -"+`orgstart`+" +"+`newstart`+","+`newsize`+" @@"
195                         print match[:string.rfind(match, "\n")]
196                         print rexp.group(6)
197                 elif rexp.start(6) == rexp.end(6) and minuses == 0 and coherent:
198                         if orgstartmod:
199                                 orgstart = orgstart + 1
200                         if newstartmod:
201                                 newstart = newstart + 1
202                         print "@@ -"+`orgstart-1`+" +"+`newstart-1`+","+`newsize`+" @@"
203                         print prematch
204                         print match[:string.rfind(match, "\n")]
205                 else:
206                         if orgstartmod:
207                                 orgstart = orgstart + 1
208                         if newstartmod:
209                                 newstart = newstart + 1
210                         print "@@ -"+`orgstart-1`+","+`orgsize`+" +"+`newstart-1`+","+`newsize`+" @@"
211                         if len(prematch):
212                                 print prematch
213                         print match[:string.rfind(match, "\n")]
214                         if rexp.start(6) != rexp.end(6):
215                                 print rexp.group(6)
216         
217                 rest = rest[rexp.end(6):]
218                 oldminuses = minuses + oldminuses
219                 oldplusses = plusses + oldplusses
220                 oldoffset = oldoffset + offset + lines #include match()-lines
221
222
223         rest = oldrest