Add mtnpatch.py, a script to parse and import a full monotone diff
[vuplus_openembedded] / contrib / mtnpatch.py
1 #!/usr/bin/env python
2 import sys, os, string, getopt
3
4 mtncmd = "monotone"
5
6 def main(argv = None):
7     if argv is None:
8         argv = sys.argv
9     opts, list = getopt.getopt(sys.argv[1:], ':R')
10     if len(list) < 1:
11         print "You must specify a file"
12         return 2
13     reverse = False
14     for o, a in opts:
15         if o == "-R":
16             reverse = True
17     if os.path.exists(list[0]):
18         input = open(list[0], 'r')
19         renameFrom = ""
20         cmd = ""
21         if reverse:
22             print "patch -R -p0 < %s" % list[0]
23         else:
24             print "patch -p0 < %s" % list[0]
25         for line in input:
26             if len(line) > 0:
27                 if line[0] == '#':
28                     parts = line.split()
29                     if len(parts) > 2:
30                         cmd = parts[1]
31                         # deal with whilespace in filenames (badly)
32                         fileName = parts[2]
33                         i = 3
34                         while i < len(parts) and fileName.count('"') % 2:
35                             fileName += " %s" % parts[i]
36                         if cmd == "delete_file":
37                             if reverse:
38                                 print "%s add %s" % (mtncmd, fileName)
39                             else:
40                                 print "%s drop -e %s" % (mtncmd, fileName)
41                         elif cmd == "add_file":
42                             if reverse:
43                                 print "%s drop -e %s" % (mtncmd, fileName)
44                             else:
45                                 print "%s add %s" % (mtncmd, fileName)
46                         elif cmd == "rename_file":
47                             renameFrom = fileName
48                         elif cmd == "to" and renameFrom != "":
49                             if reverse:
50                                 print "%s rename -e %s %s" % (mtncmd, fileName, renameFrom)
51                             else:
52                                 print "%s rename -e %s %s" % (mtncmd, renameFrom, fileName)
53                             renameFrom = ""
54                         else:
55                             cmd = ""
56
57 if __name__ == "__main__":
58     sys.exit(main())