blob: bc113879291fe8f66e3b63140aa89362b1259f74 [file] [log] [blame]
Rushabh Mehtae219db02012-02-06 15:33:08 +05301#!/usr/bin/env python
2
3import os, sys
4
5def replace_code(start, txt1, txt2, extn):
6 """replace all txt1 by txt2 in files with extension (extn)"""
7 import os, re
8 for wt in os.walk(start, followlinks=1):
9 for fn in wt[2]:
10 if fn.split('.')[-1]==extn:
11 fpath = os.path.join(wt[0], fn)
12 with open(fpath, 'r') as f:
13 content = f.read()
14
15 if re.search(txt1, content):
16 a = raw_input('Change in %s [y/n]?' % fpath)
17 if a=='y':
18 with open(fpath, 'w') as f:
19 f.write(re.sub(txt1, txt2, content))
20
21 print 'updated in %s' % fpath
22
23def setup_options():
24 from optparse import OptionParser
25 parser = OptionParser()
26 parser.add_option("-b", "--build", default=False, action="store_true",
27 help="minify + concat js files")
28 parser.add_option("-c", "--clear", default=False, action="store_true",
29 help="increment version")
30 parser.add_option("--replace", nargs=3, default=False,
31 metavar = "search replace_by extension",
32 help="file search-replace")
33 parser.add_option("--status", default=False, action="store_true",
34 help="git status")
35 parser.add_option("--pull", nargs=2, default=False,
36 metavar = "remote branch",
37 help="git pull (both repos)")
38 parser.add_option("--push", nargs=3, default=False,
39 metavar = "remote branch comment",
40 help="git commit + push (both repos) [remote] [branch] [comment]")
41 parser.add_option("-l", "--latest",
42 action="store_true", dest="run_latest", default=False,
43 help="Apply the latest patches")
44 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
45 action="append",
46 help="Apply patch")
47 parser.add_option("-f", "--force",
48 action="store_true", dest="force", default=False,
49 help="Force Apply all patches specified using option -p or --patch")
50 parser.add_option("-d", "--db",
51 dest="db_name",
52 help="Apply the patches on given db")
53 parser.add_option('-r', '--reload_doc', nargs=3, metavar = "module doctype docname",
54 help="reload doc")
55
56 return parser.parse_args()
57
58def run():
59 sys.path.append('.')
60 sys.path.append('lib/py')
61 import webnotes
62 import webnotes.defs
63 sys.path.append(webnotes.defs.modules_path)
64
65 (options, args) = setup_options()
66
67
68 from webnotes.db import Database
69 import webnotes.modules.patch_handler
70
71 # build
72 if options.build:
73 import build.project
74 build.project.build()
75
76 elif options.clear:
77 from build.project import increment_version
78 print "Version:" + str(increment_version())
79
80 # code replace
81 elif options.replace:
82 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
83
84 # git
85 elif options.status:
86 os.system('git status')
87 os.chdir('lib')
88 os.system('git status')
89
90 elif options.pull:
91 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
92 os.chdir('lib')
93 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
94
95 elif options.push:
96 os.system('git commit -a -m "%s"' % options.push[2])
97 os.system('git push %s %s' % (options.push[0], options.push[1]))
98 os.chdir('lib')
99 os.system('git commit -a -m "%s"' % options.push[2])
100 os.system('git push %s %s' % (options.push[0], options.push[1]))
101
102
103
104 # patch
105 elif options.patch_list:
106 # clear log
107 webnotes.modules.patch_handler.log_list = []
108
109 # connect to db
110 if options.db_name is not None:
111 webnotes.connect(options.db_name)
112 else:
113 webnotes.connect()
114
115 # run individual patches
116 for patch in options.patch_list:
117 webnotes.modules.patch_handler.run_single(\
118 patchmodule = patch, force = options.force)
119
120 print '\n'.join(webnotes.modules.patch_handler.log_list)
121
122 # reload
123 elif options.reload_doc:
124 webnotes.modules.patch_handler.reload_doc(\
125 {"module":args[0], "dt":args[1], "dn":args[2]})
126 print '\n'.join(webnotes.modules.patch_handler.log_list)
127
128 # run all pending
129 elif options.run_latest:
130 webnotes.modules.patch_handler.run_all()
131 print '\n'.join(webnotes.modules.patch_handler.log_list)
132
133if __name__=='__main__':
134 run()