Add Windows version info resource.
[obnox/wireshark/wip.git] / tools / cvsdiff-fix.py
1 #!/usr/bin/env python
2 #
3 # cvsdiff-fix
4 #
5 # Takes the output of "cvs diff", which produces a flattened
6 # recursive diff, and unflattens it so that it can be
7 # applied correctly with "patch".
8 #
9 # $Id$
10 #
11 # Copyright (C) 2001 by Gilbert Ramirez <gram@alumni.rice.edu>
12 #  
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26
27 import sys
28 import re
29 import string
30
31 # Open input stream
32 if len(sys.argv) == 1:
33         input = sys.stdin
34 elif len(sys.argv) == 2:
35         try:
36                 input = open(sys.argv[1])
37         except IOError:
38                 (exc_type, exc_value, exc_traceback) = sys.exc_info()
39                 print "Cannot open %s for input: %s" % (sys.argv[1], exc_value)
40                 sys.exit(1)
41 else:
42         print "Usage: %s [diff_file]" % (sys.argv[0])
43         sys.exit(1)
44
45 # State Meaning
46 # ----- -------
47 # 0     Looking for "^Index: "
48 # 1     Looking for "^diff "
49 # 2     Looking for "^---"
50 # 3     Looking for "^+++"
51
52 state = 0
53 pathname = None
54 basename = None
55
56 re_index = re.compile(r"^Index: (?P<pathname>\S+)")
57 re_diff = re.compile(r"^diff ")
58 re_diff_filename = re.compile(r"\S+$")
59 re_from = re.compile(r"^--- \S+(?P<after>.*)")
60 re_to = re.compile(r"^\+\+\+ \S+(?P<after>.*)")
61
62
63 for line in input.readlines():
64         if line[-1] == "\n":
65                 line = line[:-1]
66
67         if state == 0:
68                 match = re_index.search(line)
69                 if match:
70                         pathname = match.group("pathname")
71
72                         # Find basename
73                         i = string.rfind(pathname, "/")
74                         if i == -1:
75                                 i = string.rfind(pathname, "\\")
76
77                         if i == -1:
78                                 # if there's no dir info,
79                                 # then there's no reason to
80                                 # process this section
81                                 pass
82                         else:
83                                 basename = line[i+1:]
84                                 state = 1
85
86         elif state == 1:
87                 match = re_diff.search(line)
88                 if match:
89                         line = re_diff_filename.sub(pathname, line)
90                         state = 2
91         
92         elif state == 2:
93                 if line.find("Binary files") == 0:
94                         state = 0
95                         continue
96
97                 match = re_from.search(line)
98                 if match:
99                         new_line = "--- %s\\g<after>" % (pathname)
100                         line = re_from.sub(new_line, line)
101                         state = 3
102                 else:
103                         sys.stderr.write("Expected ^---, but found: %s\n" \
104                                 % (line))
105
106         elif state == 3:
107                 match = re_to.search(line)
108                 if match:
109                         new_line = "+++ %s\\g<after>" % (pathname)
110                         line = re_to.sub(new_line, line)
111                         state = 0
112                 else:
113                         sys.stderr.write("Expected ^+++, but found: %s\n" \
114                                 % (line))
115
116         print line