blob: 7450220516563bc39f9c4e46e7bc4292cfa1cb91 [file] [log] [blame]
Rushabh Mehta03e87872012-04-14 15:02:17 +05301#!/usr/bin/python
Rushabh Mehtae219db02012-02-06 15:33:08 +05302
Rushabh Mehta03e87872012-04-14 15:02:17 +05303# Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05304#
Rushabh Mehta03e87872012-04-14 15:02:17 +05305# MIT License (MIT)
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05306#
Rushabh Mehta03e87872012-04-14 15:02:17 +05307# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
Rushabh Mehta3966f1d2012-02-23 12:35:32 +053013#
Rushabh Mehta03e87872012-04-14 15:02:17 +053014# The above copyright notice and this permission notice shall be included in
15# all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
19# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23#
Rushabh Mehta3966f1d2012-02-23 12:35:32 +053024
Rushabh Mehtae219db02012-02-06 15:33:08 +053025import os, sys
26
Anand Doshifa0cd892012-04-05 13:03:35 +053027def replace_code(start, txt1, txt2, extn, search=None):
Rushabh Mehtae219db02012-02-06 15:33:08 +053028 """replace all txt1 by txt2 in files with extension (extn)"""
Anand Doshifa0cd892012-04-05 13:03:35 +053029 import webnotes.utils
Rushabh Mehtae219db02012-02-06 15:33:08 +053030 import os, re
Anand Doshifa0cd892012-04-05 13:03:35 +053031 esc = webnotes.utils.make_esc('[]')
32 if not search: search = esc(txt1)
Rushabh Mehtae219db02012-02-06 15:33:08 +053033 for wt in os.walk(start, followlinks=1):
34 for fn in wt[2]:
35 if fn.split('.')[-1]==extn:
36 fpath = os.path.join(wt[0], fn)
Nabin Hait19ee00b2012-03-16 16:22:30 +053037 if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary
38 with open(fpath, 'r') as f:
39 content = f.read()
Rushabh Mehtae219db02012-02-06 15:33:08 +053040
Anand Doshifa0cd892012-04-05 13:03:35 +053041 if re.search(search, content):
Anand Doshi5112b5b2012-04-02 19:12:29 +053042 res = search_replace_with_prompt(fpath, txt1, txt2)
43 if res == 'skip':
44 return 'skip'
Nabin Hait26889bd2012-03-14 11:08:15 +053045
46
47
48def search_replace_with_prompt(fpath, txt1, txt2):
49 """ Search and replace all txt1 by txt2 in the file with confirmation"""
50
51 from termcolor import colored
52 with open(fpath, 'r') as f:
53 content = f.readlines()
54
55 tmp = []
56 for c in content:
57 if c.find(txt1) != -1:
58 print '\n', fpath
59 print colored(txt1, 'red').join(c[:-1].split(txt1))
Nabin Hait19ee00b2012-03-16 16:22:30 +053060 a = ''
Anand Doshi5112b5b2012-04-02 19:12:29 +053061 while a.lower() not in ['y', 'n', 'skip']:
62 a = raw_input('Do you want to Change [y/n/skip]?')
Nabin Hait19ee00b2012-03-16 16:22:30 +053063 if a.lower() == 'y':
Nabin Hait26889bd2012-03-14 11:08:15 +053064 c = c.replace(txt1, txt2)
Anand Doshi5112b5b2012-04-02 19:12:29 +053065 elif a.lower() == 'skip':
66 return 'skip'
Nabin Hait17329052012-03-14 12:04:16 +053067 tmp.append(c)
Nabin Hait26889bd2012-03-14 11:08:15 +053068
69 with open(fpath, 'w') as f:
70 f.write(''.join(tmp))
Nabin Hait17329052012-03-14 12:04:16 +053071 print colored('Updated', 'green')
Nabin Hait26889bd2012-03-14 11:08:15 +053072
Rushabh Mehtae219db02012-02-06 15:33:08 +053073
74def setup_options():
75 from optparse import OptionParser
76 parser = OptionParser()
Rushabh Mehta1a355742012-02-23 11:46:28 +053077
78 parser.add_option("-d", "--db",
79 dest="db_name",
80 help="Apply the patches on given db")
81
82 # build
Rushabh Mehtae219db02012-02-06 15:33:08 +053083 parser.add_option("-b", "--build", default=False, action="store_true",
84 help="minify + concat js files")
85 parser.add_option("-c", "--clear", default=False, action="store_true",
Rushabh Mehta03e87872012-04-14 15:02:17 +053086 help="reset version")
Rushabh Mehta1a355742012-02-23 11:46:28 +053087
88 # git
Rushabh Mehtae219db02012-02-06 15:33:08 +053089 parser.add_option("--status", default=False, action="store_true",
90 help="git status")
91 parser.add_option("--pull", nargs=2, default=False,
92 metavar = "remote branch",
93 help="git pull (both repos)")
94 parser.add_option("--push", nargs=3, default=False,
95 metavar = "remote branch comment",
96 help="git commit + push (both repos) [remote] [branch] [comment]")
97 parser.add_option("-l", "--latest",
98 action="store_true", dest="run_latest", default=False,
99 help="Apply the latest patches")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530100
101 # patch
Rushabh Mehtae219db02012-02-06 15:33:08 +0530102 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
103 action="append",
104 help="Apply patch")
105 parser.add_option("-f", "--force",
106 action="store_true", dest="force", default=False,
107 help="Force Apply all patches specified using option -p or --patch")
Rushabh Mehtae6516482012-02-06 16:28:06 +0530108 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +0530109 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530110 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
111 help="export doc")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530112
113 # install
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530114 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
115 help="install fresh db")
Anand Doshi12b6da22012-03-20 14:55:16 +0530116
Rushabh Mehta1a355742012-02-23 11:46:28 +0530117 # diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530118 parser.add_option('--diff_ref_file', nargs=0, \
119 help="Get missing database records and mismatch properties, with file as reference")
120 parser.add_option('--diff_ref_db', nargs=0, \
121 help="Get missing .txt files and mismatch properties, with database as reference")
Anand Doshibae69082012-02-09 13:34:12 +0530122
Rushabh Mehta1a355742012-02-23 11:46:28 +0530123 # scheduler
124 parser.add_option('--run_scheduler', default=False, action="store_true",
125 help="Trigger scheduler")
126 parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
127 help="Run scheduler event")
128
129 # misc
130 parser.add_option("--replace", nargs=3, default=False,
131 metavar = "search replace_by extension",
132 help="file search-replace")
Anand Doshi7854f812012-03-14 12:01:13 +0530133
Anand Doshi66ea3052012-03-27 15:02:30 +0530134 parser.add_option("--cci", nargs=1, metavar="CacheItem Key or all",
Anand Doshi7854f812012-03-14 12:01:13 +0530135 help="Clear Cache Item")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530136
Anand Doshi66ea3052012-03-27 15:02:30 +0530137 parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
138 nargs=0)
139
140 parser.add_option("--sync", help="Synchronize given DocType using txt file",
141 nargs=2, metavar="module doctype (use their folder names)")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530142
Rushabh Mehtae219db02012-02-06 15:33:08 +0530143 return parser.parse_args()
144
145def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +0530146 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +0530147 sys.path.append('lib/py')
148 import webnotes
149 import webnotes.defs
150 sys.path.append(webnotes.defs.modules_path)
151
152 (options, args) = setup_options()
153
154
155 from webnotes.db import Database
156 import webnotes.modules.patch_handler
157
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100158 # connect
159 if options.db_name is not None:
160 webnotes.connect(options.db_name)
161
Rushabh Mehtae219db02012-02-06 15:33:08 +0530162 # build
163 if options.build:
164 import build.project
165 build.project.build()
166
167 elif options.clear:
Rushabh Mehta03e87872012-04-14 15:02:17 +0530168 from build.project import update_version
169 print "Version:" + str(update_version())
Rushabh Mehtae219db02012-02-06 15:33:08 +0530170
171 # code replace
172 elif options.replace:
173 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
174
175 # git
176 elif options.status:
177 os.system('git status')
178 os.chdir('lib')
179 os.system('git status')
180
181 elif options.pull:
182 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
183 os.chdir('lib')
184 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
185
186 elif options.push:
187 os.system('git commit -a -m "%s"' % options.push[2])
188 os.system('git push %s %s' % (options.push[0], options.push[1]))
189 os.chdir('lib')
190 os.system('git commit -a -m "%s"' % options.push[2])
191 os.system('git push %s %s' % (options.push[0], options.push[1]))
192
Rushabh Mehtae219db02012-02-06 15:33:08 +0530193 # patch
194 elif options.patch_list:
195 # clear log
196 webnotes.modules.patch_handler.log_list = []
197
Rushabh Mehtae219db02012-02-06 15:33:08 +0530198 # run individual patches
199 for patch in options.patch_list:
200 webnotes.modules.patch_handler.run_single(\
201 patchmodule = patch, force = options.force)
202
203 print '\n'.join(webnotes.modules.patch_handler.log_list)
204
205 # reload
206 elif options.reload_doc:
207 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530208 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530209 print '\n'.join(webnotes.modules.patch_handler.log_list)
210
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530211 elif options.export_doc:
212 from webnotes.modules import export_doc
213 export_doc(options.export_doc[0], options.export_doc[1])
214
Rushabh Mehtae219db02012-02-06 15:33:08 +0530215 # run all pending
216 elif options.run_latest:
217 webnotes.modules.patch_handler.run_all()
218 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100219
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530220 elif options.install:
221 from webnotes.install_lib.install import Installer
222 inst = Installer('root', options.install[0])
223 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530224 password='admin', verbose = 1)
225
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530226 elif options.diff_ref_file is not None:
Rushabh Mehta17773d02012-02-22 15:55:41 +0530227 import webnotes.modules.diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530228 webnotes.modules.diff.diff_ref_file()
229
230 elif options.diff_ref_db is not None:
231 import webnotes.modules.diff
232 webnotes.modules.diff.diff_ref_db()
Rushabh Mehta17773d02012-02-22 15:55:41 +0530233
Rushabh Mehta1a355742012-02-23 11:46:28 +0530234 elif options.run_scheduler:
235 import webnotes.utils.scheduler
236 print webnotes.utils.scheduler.execute()
237
238 elif options.run_scheduler_event is not None:
239 import webnotes.utils.scheduler
240 print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
241
Anand Doshi7854f812012-03-14 12:01:13 +0530242 elif options.cci is not None:
243 if options.cci=='all':
244 webnotes.conn.sql("DELETE FROM __CacheItem")
245 else:
246 from webnotes.utils.cache import CacheItem
247 CacheItem(options.cci).clear()
Anand Doshi66ea3052012-03-27 15:02:30 +0530248
249 elif options.sync_all is not None:
250 import webnotes.model.sync
Anand Doshi82042f12012-04-06 17:54:17 +0530251 webnotes.model.sync.sync_all(options.force or 0)
Anand Doshi66ea3052012-03-27 15:02:30 +0530252
253 elif options.sync is not None:
254 import webnotes.model.sync
Anand Doshi82042f12012-04-06 17:54:17 +0530255 webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0)
Anand Doshi7854f812012-03-14 12:01:13 +0530256
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100257 # print messages
258 if webnotes.message_log:
259 print '\n'.join(webnotes.message_log)
260
Rushabh Mehtae219db02012-02-06 15:33:08 +0530261if __name__=='__main__':
262 run()