blob: 0a4ff4bfd74355fc729a519b56e3650f38d70f49 [file] [log] [blame]
Rushabh Mehtae219db02012-02-06 15:33:08 +05301#!/usr/bin/env python
2
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05303# ERPNext - web based ERP (http://erpnext.com)
4# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
Rushabh Mehtae219db02012-02-06 15:33:08 +053019import os, sys
20
21def replace_code(start, txt1, txt2, extn):
22 """replace all txt1 by txt2 in files with extension (extn)"""
23 import os, re
24 for wt in os.walk(start, followlinks=1):
25 for fn in wt[2]:
26 if fn.split('.')[-1]==extn:
27 fpath = os.path.join(wt[0], fn)
28 with open(fpath, 'r') as f:
29 content = f.read()
30
31 if re.search(txt1, content):
32 a = raw_input('Change in %s [y/n]?' % fpath)
33 if a=='y':
34 with open(fpath, 'w') as f:
35 f.write(re.sub(txt1, txt2, content))
36
37 print 'updated in %s' % fpath
38
39def setup_options():
40 from optparse import OptionParser
41 parser = OptionParser()
Rushabh Mehta1a355742012-02-23 11:46:28 +053042
43 parser.add_option("-d", "--db",
44 dest="db_name",
45 help="Apply the patches on given db")
46
47 # build
Rushabh Mehtae219db02012-02-06 15:33:08 +053048 parser.add_option("-b", "--build", default=False, action="store_true",
49 help="minify + concat js files")
50 parser.add_option("-c", "--clear", default=False, action="store_true",
51 help="increment version")
Rushabh Mehta1a355742012-02-23 11:46:28 +053052
53 # git
Rushabh Mehtae219db02012-02-06 15:33:08 +053054 parser.add_option("--status", default=False, action="store_true",
55 help="git status")
56 parser.add_option("--pull", nargs=2, default=False,
57 metavar = "remote branch",
58 help="git pull (both repos)")
59 parser.add_option("--push", nargs=3, default=False,
60 metavar = "remote branch comment",
61 help="git commit + push (both repos) [remote] [branch] [comment]")
62 parser.add_option("-l", "--latest",
63 action="store_true", dest="run_latest", default=False,
64 help="Apply the latest patches")
Rushabh Mehta1a355742012-02-23 11:46:28 +053065
66 # patch
Rushabh Mehtae219db02012-02-06 15:33:08 +053067 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
68 action="append",
69 help="Apply patch")
70 parser.add_option("-f", "--force",
71 action="store_true", dest="force", default=False,
72 help="Force Apply all patches specified using option -p or --patch")
Rushabh Mehtae6516482012-02-06 16:28:06 +053073 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +053074 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +053075 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
76 help="export doc")
Rushabh Mehta1a355742012-02-23 11:46:28 +053077
78 # install
Rushabh Mehta0b2874a2012-02-09 12:45:50 +053079 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
80 help="install fresh db")
Anand Doshi12b6da22012-03-20 14:55:16 +053081
Rushabh Mehta1a355742012-02-23 11:46:28 +053082 # diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +053083 parser.add_option('--diff_ref_file', nargs=0, \
84 help="Get missing database records and mismatch properties, with file as reference")
85 parser.add_option('--diff_ref_db', nargs=0, \
86 help="Get missing .txt files and mismatch properties, with database as reference")
Anand Doshibae69082012-02-09 13:34:12 +053087
Rushabh Mehta1a355742012-02-23 11:46:28 +053088 # scheduler
89 parser.add_option('--run_scheduler', default=False, action="store_true",
90 help="Trigger scheduler")
91 parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
92 help="Run scheduler event")
93
94 # misc
95 parser.add_option("--replace", nargs=3, default=False,
96 metavar = "search replace_by extension",
97 help="file search-replace")
Anand Doshi7854f812012-03-14 12:01:13 +053098
99 parser.add_option("--cci", nargs=1, metavar="CacheItem Key",
100 help="Clear Cache Item")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530101
102
Rushabh Mehtae219db02012-02-06 15:33:08 +0530103 return parser.parse_args()
104
105def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +0530106 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +0530107 sys.path.append('lib/py')
108 import webnotes
109 import webnotes.defs
110 sys.path.append(webnotes.defs.modules_path)
111
112 (options, args) = setup_options()
113
114
115 from webnotes.db import Database
116 import webnotes.modules.patch_handler
117
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100118 # connect
119 if options.db_name is not None:
120 webnotes.connect(options.db_name)
121
Rushabh Mehtae219db02012-02-06 15:33:08 +0530122 # build
123 if options.build:
124 import build.project
125 build.project.build()
126
127 elif options.clear:
128 from build.project import increment_version
129 print "Version:" + str(increment_version())
130
131 # code replace
132 elif options.replace:
133 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
134
135 # git
136 elif options.status:
137 os.system('git status')
138 os.chdir('lib')
139 os.system('git status')
140
141 elif options.pull:
142 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
143 os.chdir('lib')
144 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
145
146 elif options.push:
147 os.system('git commit -a -m "%s"' % options.push[2])
148 os.system('git push %s %s' % (options.push[0], options.push[1]))
149 os.chdir('lib')
150 os.system('git commit -a -m "%s"' % options.push[2])
151 os.system('git push %s %s' % (options.push[0], options.push[1]))
152
Rushabh Mehtae219db02012-02-06 15:33:08 +0530153 # patch
154 elif options.patch_list:
155 # clear log
156 webnotes.modules.patch_handler.log_list = []
157
Rushabh Mehtae219db02012-02-06 15:33:08 +0530158 # run individual patches
159 for patch in options.patch_list:
160 webnotes.modules.patch_handler.run_single(\
161 patchmodule = patch, force = options.force)
162
163 print '\n'.join(webnotes.modules.patch_handler.log_list)
164
165 # reload
166 elif options.reload_doc:
167 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530168 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530169 print '\n'.join(webnotes.modules.patch_handler.log_list)
170
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530171 elif options.export_doc:
172 from webnotes.modules import export_doc
173 export_doc(options.export_doc[0], options.export_doc[1])
174
Rushabh Mehtae219db02012-02-06 15:33:08 +0530175 # run all pending
176 elif options.run_latest:
177 webnotes.modules.patch_handler.run_all()
178 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100179
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530180 elif options.install:
181 from webnotes.install_lib.install import Installer
182 inst = Installer('root', options.install[0])
183 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530184 password='admin', verbose = 1)
185
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530186 elif options.diff_ref_file is not None:
Rushabh Mehta17773d02012-02-22 15:55:41 +0530187 import webnotes.modules.diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530188 webnotes.modules.diff.diff_ref_file()
189
190 elif options.diff_ref_db is not None:
191 import webnotes.modules.diff
192 webnotes.modules.diff.diff_ref_db()
Rushabh Mehta17773d02012-02-22 15:55:41 +0530193
Rushabh Mehta1a355742012-02-23 11:46:28 +0530194 elif options.run_scheduler:
195 import webnotes.utils.scheduler
196 print webnotes.utils.scheduler.execute()
197
198 elif options.run_scheduler_event is not None:
199 import webnotes.utils.scheduler
200 print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
201
Anand Doshi7854f812012-03-14 12:01:13 +0530202 elif options.cci is not None:
203 if options.cci=='all':
204 webnotes.conn.sql("DELETE FROM __CacheItem")
205 else:
206 from webnotes.utils.cache import CacheItem
207 CacheItem(options.cci).clear()
208
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100209 # print messages
210 if webnotes.message_log:
211 print '\n'.join(webnotes.message_log)
212
Rushabh Mehtae219db02012-02-06 15:33:08 +0530213if __name__=='__main__':
214 run()