blob: 4cd1c8b581dbd747cb58d8fec6bb4df57c1c472e [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)
Nabin Hait19ee00b2012-03-16 16:22:30 +053028 if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary
29 with open(fpath, 'r') as f:
30 content = f.read()
Rushabh Mehtae219db02012-02-06 15:33:08 +053031
Nabin Hait19ee00b2012-03-16 16:22:30 +053032 if re.search(txt1, content):
Anand Doshi5112b5b2012-04-02 19:12:29 +053033 res = search_replace_with_prompt(fpath, txt1, txt2)
34 if res == 'skip':
35 return 'skip'
Nabin Hait26889bd2012-03-14 11:08:15 +053036
37
38
39def search_replace_with_prompt(fpath, txt1, txt2):
40 """ Search and replace all txt1 by txt2 in the file with confirmation"""
41
42 from termcolor import colored
43 with open(fpath, 'r') as f:
44 content = f.readlines()
45
46 tmp = []
47 for c in content:
48 if c.find(txt1) != -1:
49 print '\n', fpath
50 print colored(txt1, 'red').join(c[:-1].split(txt1))
Nabin Hait19ee00b2012-03-16 16:22:30 +053051 a = ''
Anand Doshi5112b5b2012-04-02 19:12:29 +053052 while a.lower() not in ['y', 'n', 'skip']:
53 a = raw_input('Do you want to Change [y/n/skip]?')
Nabin Hait19ee00b2012-03-16 16:22:30 +053054 if a.lower() == 'y':
Nabin Hait26889bd2012-03-14 11:08:15 +053055 c = c.replace(txt1, txt2)
Anand Doshi5112b5b2012-04-02 19:12:29 +053056 elif a.lower() == 'skip':
57 return 'skip'
Nabin Hait17329052012-03-14 12:04:16 +053058 tmp.append(c)
Nabin Hait26889bd2012-03-14 11:08:15 +053059
60 with open(fpath, 'w') as f:
61 f.write(''.join(tmp))
Nabin Hait17329052012-03-14 12:04:16 +053062 print colored('Updated', 'green')
Nabin Hait26889bd2012-03-14 11:08:15 +053063
Rushabh Mehtae219db02012-02-06 15:33:08 +053064
65def setup_options():
66 from optparse import OptionParser
67 parser = OptionParser()
Rushabh Mehta1a355742012-02-23 11:46:28 +053068
69 parser.add_option("-d", "--db",
70 dest="db_name",
71 help="Apply the patches on given db")
72
73 # build
Rushabh Mehtae219db02012-02-06 15:33:08 +053074 parser.add_option("-b", "--build", default=False, action="store_true",
75 help="minify + concat js files")
76 parser.add_option("-c", "--clear", default=False, action="store_true",
77 help="increment version")
Rushabh Mehta1a355742012-02-23 11:46:28 +053078
79 # git
Rushabh Mehtae219db02012-02-06 15:33:08 +053080 parser.add_option("--status", default=False, action="store_true",
81 help="git status")
82 parser.add_option("--pull", nargs=2, default=False,
83 metavar = "remote branch",
84 help="git pull (both repos)")
85 parser.add_option("--push", nargs=3, default=False,
86 metavar = "remote branch comment",
87 help="git commit + push (both repos) [remote] [branch] [comment]")
88 parser.add_option("-l", "--latest",
89 action="store_true", dest="run_latest", default=False,
90 help="Apply the latest patches")
Rushabh Mehta1a355742012-02-23 11:46:28 +053091
92 # patch
Rushabh Mehtae219db02012-02-06 15:33:08 +053093 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
94 action="append",
95 help="Apply patch")
96 parser.add_option("-f", "--force",
97 action="store_true", dest="force", default=False,
98 help="Force Apply all patches specified using option -p or --patch")
Rushabh Mehtae6516482012-02-06 16:28:06 +053099 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +0530100 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530101 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
102 help="export doc")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530103
104 # install
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530105 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
106 help="install fresh db")
Anand Doshi12b6da22012-03-20 14:55:16 +0530107
Rushabh Mehta1a355742012-02-23 11:46:28 +0530108 # diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530109 parser.add_option('--diff_ref_file', nargs=0, \
110 help="Get missing database records and mismatch properties, with file as reference")
111 parser.add_option('--diff_ref_db', nargs=0, \
112 help="Get missing .txt files and mismatch properties, with database as reference")
Anand Doshibae69082012-02-09 13:34:12 +0530113
Rushabh Mehta1a355742012-02-23 11:46:28 +0530114 # scheduler
115 parser.add_option('--run_scheduler', default=False, action="store_true",
116 help="Trigger scheduler")
117 parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
118 help="Run scheduler event")
119
120 # misc
121 parser.add_option("--replace", nargs=3, default=False,
122 metavar = "search replace_by extension",
123 help="file search-replace")
Anand Doshi7854f812012-03-14 12:01:13 +0530124
Anand Doshi66ea3052012-03-27 15:02:30 +0530125 parser.add_option("--cci", nargs=1, metavar="CacheItem Key or all",
Anand Doshi7854f812012-03-14 12:01:13 +0530126 help="Clear Cache Item")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530127
Anand Doshi66ea3052012-03-27 15:02:30 +0530128 parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
129 nargs=0)
130
131 parser.add_option("--sync", help="Synchronize given DocType using txt file",
132 nargs=2, metavar="module doctype (use their folder names)")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530133
Rushabh Mehtae219db02012-02-06 15:33:08 +0530134 return parser.parse_args()
135
136def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +0530137 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +0530138 sys.path.append('lib/py')
139 import webnotes
140 import webnotes.defs
141 sys.path.append(webnotes.defs.modules_path)
142
143 (options, args) = setup_options()
144
145
146 from webnotes.db import Database
147 import webnotes.modules.patch_handler
148
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100149 # connect
150 if options.db_name is not None:
151 webnotes.connect(options.db_name)
152
Rushabh Mehtae219db02012-02-06 15:33:08 +0530153 # build
154 if options.build:
155 import build.project
156 build.project.build()
157
158 elif options.clear:
159 from build.project import increment_version
160 print "Version:" + str(increment_version())
161
162 # code replace
163 elif options.replace:
164 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
165
166 # git
167 elif options.status:
168 os.system('git status')
169 os.chdir('lib')
170 os.system('git status')
171
172 elif options.pull:
173 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
174 os.chdir('lib')
175 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
176
177 elif options.push:
178 os.system('git commit -a -m "%s"' % options.push[2])
179 os.system('git push %s %s' % (options.push[0], options.push[1]))
180 os.chdir('lib')
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
Rushabh Mehtae219db02012-02-06 15:33:08 +0530184 # patch
185 elif options.patch_list:
186 # clear log
187 webnotes.modules.patch_handler.log_list = []
188
Rushabh Mehtae219db02012-02-06 15:33:08 +0530189 # run individual patches
190 for patch in options.patch_list:
191 webnotes.modules.patch_handler.run_single(\
192 patchmodule = patch, force = options.force)
193
194 print '\n'.join(webnotes.modules.patch_handler.log_list)
195
196 # reload
197 elif options.reload_doc:
198 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530199 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530200 print '\n'.join(webnotes.modules.patch_handler.log_list)
201
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530202 elif options.export_doc:
203 from webnotes.modules import export_doc
204 export_doc(options.export_doc[0], options.export_doc[1])
205
Rushabh Mehtae219db02012-02-06 15:33:08 +0530206 # run all pending
207 elif options.run_latest:
208 webnotes.modules.patch_handler.run_all()
209 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100210
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530211 elif options.install:
212 from webnotes.install_lib.install import Installer
213 inst = Installer('root', options.install[0])
214 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530215 password='admin', verbose = 1)
216
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530217 elif options.diff_ref_file is not None:
Rushabh Mehta17773d02012-02-22 15:55:41 +0530218 import webnotes.modules.diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530219 webnotes.modules.diff.diff_ref_file()
220
221 elif options.diff_ref_db is not None:
222 import webnotes.modules.diff
223 webnotes.modules.diff.diff_ref_db()
Rushabh Mehta17773d02012-02-22 15:55:41 +0530224
Rushabh Mehta1a355742012-02-23 11:46:28 +0530225 elif options.run_scheduler:
226 import webnotes.utils.scheduler
227 print webnotes.utils.scheduler.execute()
228
229 elif options.run_scheduler_event is not None:
230 import webnotes.utils.scheduler
231 print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
232
Anand Doshi7854f812012-03-14 12:01:13 +0530233 elif options.cci is not None:
234 if options.cci=='all':
235 webnotes.conn.sql("DELETE FROM __CacheItem")
236 else:
237 from webnotes.utils.cache import CacheItem
238 CacheItem(options.cci).clear()
Anand Doshi66ea3052012-03-27 15:02:30 +0530239
240 elif options.sync_all is not None:
241 import webnotes.model.sync
242 webnotes.model.sync.sync_all()
243
244 elif options.sync is not None:
245 import webnotes.model.sync
246 webnotes.model.sync.sync(options.sync[0], options.sync[1])
Anand Doshi7854f812012-03-14 12:01:13 +0530247
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100248 # print messages
249 if webnotes.message_log:
250 print '\n'.join(webnotes.message_log)
251
Rushabh Mehtae219db02012-02-06 15:33:08 +0530252if __name__=='__main__':
253 run()