blob: cc82eb56ed934d7b5436c0ec89a9e27c09dfafdc [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 Mehta0b2874a2012-02-09 12:45:50 +053057 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
58 help="install fresh db")
Rushabh Mehtae219db02012-02-06 15:33:08 +053059
60 return parser.parse_args()
61
62def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +053063 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +053064 sys.path.append('lib/py')
65 import webnotes
66 import webnotes.defs
67 sys.path.append(webnotes.defs.modules_path)
68
69 (options, args) = setup_options()
70
71
72 from webnotes.db import Database
73 import webnotes.modules.patch_handler
74
Rushabh Mehtacc54fd42012-02-06 13:09:08 +010075 # connect
76 if options.db_name is not None:
77 webnotes.connect(options.db_name)
78
Rushabh Mehtae219db02012-02-06 15:33:08 +053079 # build
80 if options.build:
81 import build.project
82 build.project.build()
83
84 elif options.clear:
85 from build.project import increment_version
86 print "Version:" + str(increment_version())
87
88 # code replace
89 elif options.replace:
90 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
91
92 # git
93 elif options.status:
94 os.system('git status')
95 os.chdir('lib')
96 os.system('git status')
97
98 elif options.pull:
99 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
100 os.chdir('lib')
101 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
102
103 elif options.push:
104 os.system('git commit -a -m "%s"' % options.push[2])
105 os.system('git push %s %s' % (options.push[0], options.push[1]))
106 os.chdir('lib')
107 os.system('git commit -a -m "%s"' % options.push[2])
108 os.system('git push %s %s' % (options.push[0], options.push[1]))
109
Rushabh Mehtae219db02012-02-06 15:33:08 +0530110 # patch
111 elif options.patch_list:
112 # clear log
113 webnotes.modules.patch_handler.log_list = []
114
Rushabh Mehtae219db02012-02-06 15:33:08 +0530115 # 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(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530125 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530126 print '\n'.join(webnotes.modules.patch_handler.log_list)
127
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530128 elif options.export_doc:
129 from webnotes.modules import export_doc
130 export_doc(options.export_doc[0], options.export_doc[1])
131
Rushabh Mehtae219db02012-02-06 15:33:08 +0530132 # run all pending
133 elif options.run_latest:
134 webnotes.modules.patch_handler.run_all()
135 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100136
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530137 elif options.install:
138 from webnotes.install_lib.install import Installer
139 inst = Installer('root', options.install[0])
140 inst.import_from_db(options.install[1], source_path=options.install[2], \
141 password='admin', verbose = 1)
142
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100143 # print messages
144 if webnotes.message_log:
145 print '\n'.join(webnotes.message_log)
146
Rushabh Mehtae219db02012-02-06 15:33:08 +0530147if __name__=='__main__':
148 run()