blob: 52308d0902a0c9627114d4f69ac24a460e3475fe [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")
Rushabh Mehtae6516482012-02-06 16:28:06 +053053 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +053054 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +053055 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
56 help="export doc")
Rushabh Mehtae219db02012-02-06 15:33:08 +053057
58 return parser.parse_args()
59
60def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +053061 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +053062 sys.path.append('lib/py')
63 import webnotes
64 import webnotes.defs
65 sys.path.append(webnotes.defs.modules_path)
66
67 (options, args) = setup_options()
68
69
70 from webnotes.db import Database
71 import webnotes.modules.patch_handler
72
Rushabh Mehtacc54fd42012-02-06 13:09:08 +010073 # connect
74 if options.db_name is not None:
75 webnotes.connect(options.db_name)
76
Rushabh Mehtae219db02012-02-06 15:33:08 +053077 # build
78 if options.build:
79 import build.project
80 build.project.build()
81
82 elif options.clear:
83 from build.project import increment_version
84 print "Version:" + str(increment_version())
85
86 # code replace
87 elif options.replace:
88 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
89
90 # git
91 elif options.status:
92 os.system('git status')
93 os.chdir('lib')
94 os.system('git status')
95
96 elif options.pull:
97 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
98 os.chdir('lib')
99 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
100
101 elif options.push:
102 os.system('git commit -a -m "%s"' % options.push[2])
103 os.system('git push %s %s' % (options.push[0], options.push[1]))
104 os.chdir('lib')
105 os.system('git commit -a -m "%s"' % options.push[2])
106 os.system('git push %s %s' % (options.push[0], options.push[1]))
107
Rushabh Mehtae219db02012-02-06 15:33:08 +0530108 # patch
109 elif options.patch_list:
110 # clear log
111 webnotes.modules.patch_handler.log_list = []
112
Rushabh Mehtae219db02012-02-06 15:33:08 +0530113 # run individual patches
114 for patch in options.patch_list:
115 webnotes.modules.patch_handler.run_single(\
116 patchmodule = patch, force = options.force)
117
118 print '\n'.join(webnotes.modules.patch_handler.log_list)
119
120 # reload
121 elif options.reload_doc:
122 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530123 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530124 print '\n'.join(webnotes.modules.patch_handler.log_list)
125
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530126 elif options.export_doc:
127 from webnotes.modules import export_doc
128 export_doc(options.export_doc[0], options.export_doc[1])
129
Rushabh Mehtae219db02012-02-06 15:33:08 +0530130 # run all pending
131 elif options.run_latest:
132 webnotes.modules.patch_handler.run_all()
133 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100134
135 # print messages
136 if webnotes.message_log:
137 print '\n'.join(webnotes.message_log)
138
Rushabh Mehtae219db02012-02-06 15:33:08 +0530139if __name__=='__main__':
140 run()