blob: 94450d8e7457345933de57c41b2c3272622a06e7 [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
Anand Doshifa0cd892012-04-05 13:03:35 +053021def replace_code(start, txt1, txt2, extn, search=None):
Rushabh Mehtae219db02012-02-06 15:33:08 +053022 """replace all txt1 by txt2 in files with extension (extn)"""
Anand Doshifa0cd892012-04-05 13:03:35 +053023 import webnotes.utils
Rushabh Mehtae219db02012-02-06 15:33:08 +053024 import os, re
Anand Doshifa0cd892012-04-05 13:03:35 +053025 esc = webnotes.utils.make_esc('[]')
26 if not search: search = esc(txt1)
Rushabh Mehtae219db02012-02-06 15:33:08 +053027 for wt in os.walk(start, followlinks=1):
28 for fn in wt[2]:
29 if fn.split('.')[-1]==extn:
30 fpath = os.path.join(wt[0], fn)
Nabin Hait19ee00b2012-03-16 16:22:30 +053031 if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary
32 with open(fpath, 'r') as f:
33 content = f.read()
Rushabh Mehtae219db02012-02-06 15:33:08 +053034
Anand Doshifa0cd892012-04-05 13:03:35 +053035 if re.search(search, content):
Anand Doshi5112b5b2012-04-02 19:12:29 +053036 res = search_replace_with_prompt(fpath, txt1, txt2)
37 if res == 'skip':
38 return 'skip'
Nabin Hait26889bd2012-03-14 11:08:15 +053039
40
41
42def search_replace_with_prompt(fpath, txt1, txt2):
43 """ Search and replace all txt1 by txt2 in the file with confirmation"""
44
45 from termcolor import colored
46 with open(fpath, 'r') as f:
47 content = f.readlines()
48
49 tmp = []
50 for c in content:
51 if c.find(txt1) != -1:
52 print '\n', fpath
53 print colored(txt1, 'red').join(c[:-1].split(txt1))
Nabin Hait19ee00b2012-03-16 16:22:30 +053054 a = ''
Anand Doshi5112b5b2012-04-02 19:12:29 +053055 while a.lower() not in ['y', 'n', 'skip']:
56 a = raw_input('Do you want to Change [y/n/skip]?')
Nabin Hait19ee00b2012-03-16 16:22:30 +053057 if a.lower() == 'y':
Nabin Hait26889bd2012-03-14 11:08:15 +053058 c = c.replace(txt1, txt2)
Anand Doshi5112b5b2012-04-02 19:12:29 +053059 elif a.lower() == 'skip':
60 return 'skip'
Nabin Hait17329052012-03-14 12:04:16 +053061 tmp.append(c)
Nabin Hait26889bd2012-03-14 11:08:15 +053062
63 with open(fpath, 'w') as f:
64 f.write(''.join(tmp))
Nabin Hait17329052012-03-14 12:04:16 +053065 print colored('Updated', 'green')
Nabin Hait26889bd2012-03-14 11:08:15 +053066
Rushabh Mehtae219db02012-02-06 15:33:08 +053067
68def setup_options():
69 from optparse import OptionParser
70 parser = OptionParser()
Rushabh Mehta1a355742012-02-23 11:46:28 +053071
72 parser.add_option("-d", "--db",
73 dest="db_name",
74 help="Apply the patches on given db")
75
76 # build
Rushabh Mehtae219db02012-02-06 15:33:08 +053077 parser.add_option("-b", "--build", default=False, action="store_true",
78 help="minify + concat js files")
79 parser.add_option("-c", "--clear", default=False, action="store_true",
80 help="increment version")
Rushabh Mehta1a355742012-02-23 11:46:28 +053081
82 # git
Rushabh Mehtae219db02012-02-06 15:33:08 +053083 parser.add_option("--status", default=False, action="store_true",
84 help="git status")
85 parser.add_option("--pull", nargs=2, default=False,
86 metavar = "remote branch",
87 help="git pull (both repos)")
88 parser.add_option("--push", nargs=3, default=False,
89 metavar = "remote branch comment",
90 help="git commit + push (both repos) [remote] [branch] [comment]")
91 parser.add_option("-l", "--latest",
92 action="store_true", dest="run_latest", default=False,
93 help="Apply the latest patches")
Rushabh Mehta1a355742012-02-23 11:46:28 +053094
95 # patch
Rushabh Mehtae219db02012-02-06 15:33:08 +053096 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
97 action="append",
98 help="Apply patch")
99 parser.add_option("-f", "--force",
100 action="store_true", dest="force", default=False,
101 help="Force Apply all patches specified using option -p or --patch")
Rushabh Mehtae6516482012-02-06 16:28:06 +0530102 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +0530103 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530104 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
105 help="export doc")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530106
107 # install
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530108 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
109 help="install fresh db")
Anand Doshi12b6da22012-03-20 14:55:16 +0530110
Rushabh Mehta1a355742012-02-23 11:46:28 +0530111 # diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530112 parser.add_option('--diff_ref_file', nargs=0, \
113 help="Get missing database records and mismatch properties, with file as reference")
114 parser.add_option('--diff_ref_db', nargs=0, \
115 help="Get missing .txt files and mismatch properties, with database as reference")
Anand Doshibae69082012-02-09 13:34:12 +0530116
Rushabh Mehta1a355742012-02-23 11:46:28 +0530117 # scheduler
118 parser.add_option('--run_scheduler', default=False, action="store_true",
119 help="Trigger scheduler")
120 parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
121 help="Run scheduler event")
122
123 # misc
124 parser.add_option("--replace", nargs=3, default=False,
125 metavar = "search replace_by extension",
126 help="file search-replace")
Anand Doshi7854f812012-03-14 12:01:13 +0530127
Anand Doshi66ea3052012-03-27 15:02:30 +0530128 parser.add_option("--cci", nargs=1, metavar="CacheItem Key or all",
Anand Doshi7854f812012-03-14 12:01:13 +0530129 help="Clear Cache Item")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530130
Anand Doshi66ea3052012-03-27 15:02:30 +0530131 parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
132 nargs=0)
133
134 parser.add_option("--sync", help="Synchronize given DocType using txt file",
135 nargs=2, metavar="module doctype (use their folder names)")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530136
Rushabh Mehtae219db02012-02-06 15:33:08 +0530137 return parser.parse_args()
138
139def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +0530140 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +0530141 sys.path.append('lib/py')
142 import webnotes
143 import webnotes.defs
144 sys.path.append(webnotes.defs.modules_path)
145
146 (options, args) = setup_options()
147
148
149 from webnotes.db import Database
150 import webnotes.modules.patch_handler
151
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100152 # connect
153 if options.db_name is not None:
154 webnotes.connect(options.db_name)
155
Rushabh Mehtae219db02012-02-06 15:33:08 +0530156 # build
157 if options.build:
158 import build.project
159 build.project.build()
160
161 elif options.clear:
162 from build.project import increment_version
163 print "Version:" + str(increment_version())
164
165 # code replace
166 elif options.replace:
167 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
168
169 # git
170 elif options.status:
171 os.system('git status')
172 os.chdir('lib')
173 os.system('git status')
174
175 elif options.pull:
176 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
177 os.chdir('lib')
178 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
179
180 elif options.push:
181 os.system('git commit -a -m "%s"' % options.push[2])
182 os.system('git push %s %s' % (options.push[0], options.push[1]))
183 os.chdir('lib')
184 os.system('git commit -a -m "%s"' % options.push[2])
185 os.system('git push %s %s' % (options.push[0], options.push[1]))
186
Rushabh Mehtae219db02012-02-06 15:33:08 +0530187 # patch
188 elif options.patch_list:
189 # clear log
190 webnotes.modules.patch_handler.log_list = []
191
Rushabh Mehtae219db02012-02-06 15:33:08 +0530192 # run individual patches
193 for patch in options.patch_list:
194 webnotes.modules.patch_handler.run_single(\
195 patchmodule = patch, force = options.force)
196
197 print '\n'.join(webnotes.modules.patch_handler.log_list)
198
199 # reload
200 elif options.reload_doc:
201 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530202 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530203 print '\n'.join(webnotes.modules.patch_handler.log_list)
204
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530205 elif options.export_doc:
206 from webnotes.modules import export_doc
207 export_doc(options.export_doc[0], options.export_doc[1])
208
Rushabh Mehtae219db02012-02-06 15:33:08 +0530209 # run all pending
210 elif options.run_latest:
211 webnotes.modules.patch_handler.run_all()
212 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100213
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530214 elif options.install:
215 from webnotes.install_lib.install import Installer
216 inst = Installer('root', options.install[0])
217 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530218 password='admin', verbose = 1)
219
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530220 elif options.diff_ref_file is not None:
Rushabh Mehta17773d02012-02-22 15:55:41 +0530221 import webnotes.modules.diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530222 webnotes.modules.diff.diff_ref_file()
223
224 elif options.diff_ref_db is not None:
225 import webnotes.modules.diff
226 webnotes.modules.diff.diff_ref_db()
Rushabh Mehta17773d02012-02-22 15:55:41 +0530227
Rushabh Mehta1a355742012-02-23 11:46:28 +0530228 elif options.run_scheduler:
229 import webnotes.utils.scheduler
230 print webnotes.utils.scheduler.execute()
231
232 elif options.run_scheduler_event is not None:
233 import webnotes.utils.scheduler
234 print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
235
Anand Doshi7854f812012-03-14 12:01:13 +0530236 elif options.cci is not None:
237 if options.cci=='all':
238 webnotes.conn.sql("DELETE FROM __CacheItem")
239 else:
240 from webnotes.utils.cache import CacheItem
241 CacheItem(options.cci).clear()
Anand Doshi66ea3052012-03-27 15:02:30 +0530242
243 elif options.sync_all is not None:
244 import webnotes.model.sync
Anand Doshi82042f12012-04-06 17:54:17 +0530245 webnotes.model.sync.sync_all(options.force or 0)
Anand Doshi66ea3052012-03-27 15:02:30 +0530246
247 elif options.sync is not None:
248 import webnotes.model.sync
Anand Doshi82042f12012-04-06 17:54:17 +0530249 webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
Anand Doshi7854f812012-03-14 12:01:13 +0530250
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100251 # print messages
252 if webnotes.message_log:
253 print '\n'.join(webnotes.message_log)
254
Rushabh Mehtae219db02012-02-06 15:33:08 +0530255if __name__=='__main__':
256 run()