mtnpatch: replace split with regex
[vuplus_openembedded] / contrib / mtnpatch.py
1 #!/usr/bin/env python
2 import sys, os, string, getopt, re
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                     matches = re.search("#\s+(\w+)\s+\"(.*)\"", line)
29                     if matches is not None:
30                         cmd = matches.group(1)
31                         fileName = matches.group(2)
32                         if cmd == "delete_file":
33                             if reverse:
34                                 print "%s add %s" % (mtncmd, fileName)
35                             else:
36                                 print "%s drop -e %s" % (mtncmd, fileName)
37                         elif cmd == "add_file":
38                             if reverse:
39                                 print "%s drop -e %s" % (mtncmd, fileName)
40                             else:
41                                 print "%s add %s" % (mtncmd, fileName)
42                         elif cmd == "rename_file":
43                             renameFrom = fileName
44                         elif cmd == "to" and renameFrom != "":
45                             if reverse:
46                                 print "%s rename -e %s %s" % (mtncmd, fileName, renameFrom)
47                             else:
48                                 print "%s rename -e %s %s" % (mtncmd, renameFrom, fileName)
49                             renameFrom = ""
50                         else:
51                             cmd = ""
52
53 if __name__ == "__main__":
54     sys.exit(main())