blob: 355474f58c6bfecde20c9963624db4439e89477a [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")
55
56 return parser.parse_args()
57
58def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +053059 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +053060 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
Rushabh Mehtacc54fd42012-02-06 13:09:08 +010071 # connect
72 if options.db_name is not None:
73 webnotes.connect(options.db_name)
74
Rushabh Mehtae219db02012-02-06 15:33:08 +053075 # build
76 if options.build:
77 import build.project
78 build.project.build()
79
80 elif options.clear:
81 from build.project import increment_version
82 print "Version:" + str(increment_version())
83
84 # code replace
85 elif options.replace:
86 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
87
88 # git
89 elif options.status:
90 os.system('git status')
91 os.chdir('lib')
92 os.system('git status')
93
94 elif options.pull:
95 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
96 os.chdir('lib')
97 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
98
99 elif options.push:
100 os.system('git commit -a -m "%s"' % options.push[2])
101 os.system('git push %s %s' % (options.push[0], options.push[1]))
102 os.chdir('lib')
103 os.system('git commit -a -m "%s"' % options.push[2])
104 os.system('git push %s %s' % (options.push[0], options.push[1]))
105
Rushabh Mehtae219db02012-02-06 15:33:08 +0530106 # patch
107 elif options.patch_list:
108 # clear log
109 webnotes.modules.patch_handler.log_list = []
110
Rushabh Mehtae219db02012-02-06 15:33:08 +0530111 # run individual patches
112 for patch in options.patch_list:
113 webnotes.modules.patch_handler.run_single(\
114 patchmodule = patch, force = options.force)
115
116 print '\n'.join(webnotes.modules.patch_handler.log_list)
117
118 # reload
119 elif options.reload_doc:
120 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530121 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530122 print '\n'.join(webnotes.modules.patch_handler.log_list)
123
124 # run all pending
125 elif options.run_latest:
126 webnotes.modules.patch_handler.run_all()
127 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100128
129 # print messages
130 if webnotes.message_log:
131 print '\n'.join(webnotes.message_log)
132
Rushabh Mehtae219db02012-02-06 15:33:08 +0530133if __name__=='__main__':
134 run()