1 #!/usr/local/bin/python
2 # $DragonFly: src/tools/tools/splitpatch/splitpatch.py,v 1.2 2005/05/21 10:47:43 corecode Exp $
4 """Split a patch file into one patch for each file."""
7 Copyright (c) 2004 Joerg Sonnenberger. All rights reserved.
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
20 * Neither the name of the authors nor the names of there
21 contributors may be used to endorse or promote products derived from
22 this software without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 def directory_save(filename, patch, suffix = None, root = None, forceful = False):
42 Saves patch into filename.suffix relative to root.
48 output_name = os.path.join(root, "%s%s" % (filename.replace('/',','), suffix))
50 if os.path.exists(output_name) and not forceful:
51 raise IOError, 'file exists'
52 f = open(output_name,"w")
56 def splitpatch(source, output = directory_save, quiet = False):
58 Split the patch in source into independent pieces
59 and call output on the result with the guessed filename
62 source has to provide an iterator like file objects.
64 diff_line = { " ": True, "+": True, "-": True, "@": True, "!": True, '*': True }
69 if line.startswith('***'):
71 filename = line.split()[1]
72 elif line.startswith('+++'):
74 filename = line.split()[1]
76 if filename and not quiet:
77 print "Found patch for %s" % filename
80 elif diff_line.get(line[0]):
81 # valid line for a patch
84 # invalid line for a patch, write out current buffer
85 output(filename, "".join(buf))
91 output(filename, "".join(buf))
94 from optparse import OptionParser
96 parser = OptionParser("usage: %prog [-q] [-f] [-s suffix] [input]")
97 parser.add_option("-q", "--quiet", action="store_true", dest="quiet", help="do not print names of the subpatches")
98 parser.add_option("-f", "--force", action="store_true", dest="force", help="overwrite existing patches")
99 parser.add_option("-s", "--suffix", type="string", dest="suffix", help="use SUFFIX instead of .patch for the created patches")
100 parser.add_option("-d", "--directory", type="string", dest="directory", help="create patches in DIRECTORY")
101 (options, args) = parser.parse_args()
103 parser.error("incorrect number of arguments")
105 source = open(args[0])
108 splitpatch(source, lambda filename, patch: directory_save(filename, patch, forceful = options.force,
109 suffix = options.suffix, root = options.directory), quiet = options.quiet)
111 if __name__ == '__main__':