blob: 4c6c012edf173771cd255d927ad9a74247f2ec56 [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):
33 search_replace_with_prompt(fpath, txt1, txt2)
Nabin Hait26889bd2012-03-14 11:08:15 +053034
35
36
37def search_replace_with_prompt(fpath, txt1, txt2):
38 """ Search and replace all txt1 by txt2 in the file with confirmation"""
39
40 from termcolor import colored
41 with open(fpath, 'r') as f:
42 content = f.readlines()
43
44 tmp = []
45 for c in content:
46 if c.find(txt1) != -1:
47 print '\n', fpath
48 print colored(txt1, 'red').join(c[:-1].split(txt1))
Nabin Hait19ee00b2012-03-16 16:22:30 +053049 a = ''
50 while a not in ['y', 'n', 'Y', 'N']:
51 a = raw_input('Do you want to Change [y/n]?')
52 if a.lower() == 'y':
Nabin Hait26889bd2012-03-14 11:08:15 +053053 c = c.replace(txt1, txt2)
Nabin Hait17329052012-03-14 12:04:16 +053054 tmp.append(c)
Nabin Hait26889bd2012-03-14 11:08:15 +053055
56 with open(fpath, 'w') as f:
57 f.write(''.join(tmp))
Nabin Hait17329052012-03-14 12:04:16 +053058 print colored('Updated', 'green')
Nabin Hait26889bd2012-03-14 11:08:15 +053059
Rushabh Mehtae219db02012-02-06 15:33:08 +053060
61def setup_options():
62 from optparse import OptionParser
63 parser = OptionParser()
Rushabh Mehta1a355742012-02-23 11:46:28 +053064
65 parser.add_option("-d", "--db",
66 dest="db_name",
67 help="Apply the patches on given db")
68
69 # build
Rushabh Mehtae219db02012-02-06 15:33:08 +053070 parser.add_option("-b", "--build", default=False, action="store_true",
71 help="minify + concat js files")
72 parser.add_option("-c", "--clear", default=False, action="store_true",
73 help="increment version")
Rushabh Mehta1a355742012-02-23 11:46:28 +053074
75 # git
Rushabh Mehtae219db02012-02-06 15:33:08 +053076 parser.add_option("--status", default=False, action="store_true",
77 help="git status")
78 parser.add_option("--pull", nargs=2, default=False,
79 metavar = "remote branch",
80 help="git pull (both repos)")
81 parser.add_option("--push", nargs=3, default=False,
82 metavar = "remote branch comment",
83 help="git commit + push (both repos) [remote] [branch] [comment]")
84 parser.add_option("-l", "--latest",
85 action="store_true", dest="run_latest", default=False,
86 help="Apply the latest patches")
Rushabh Mehta1a355742012-02-23 11:46:28 +053087
88 # patch
Rushabh Mehtae219db02012-02-06 15:33:08 +053089 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
90 action="append",
91 help="Apply patch")
92 parser.add_option("-f", "--force",
93 action="store_true", dest="force", default=False,
94 help="Force Apply all patches specified using option -p or --patch")
Rushabh Mehtae6516482012-02-06 16:28:06 +053095 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +053096 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +053097 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
98 help="export doc")
Rushabh Mehta1a355742012-02-23 11:46:28 +053099
100 # install
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530101 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
102 help="install fresh db")
Rushabh Mehta17773d02012-02-22 15:55:41 +0530103 parser.add_option('--sync_with_gateway', nargs=1, metavar = "1/0", \
104 help="Set or Unset Sync with Gateway")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530105
106 # diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530107 parser.add_option('--diff_ref_file', nargs=0, \
108 help="Get missing database records and mismatch properties, with file as reference")
109 parser.add_option('--diff_ref_db', nargs=0, \
110 help="Get missing .txt files and mismatch properties, with database as reference")
Anand Doshibae69082012-02-09 13:34:12 +0530111
Rushabh Mehta1a355742012-02-23 11:46:28 +0530112 # scheduler
113 parser.add_option('--run_scheduler', default=False, action="store_true",
114 help="Trigger scheduler")
115 parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
116 help="Run scheduler event")
117
118 # misc
119 parser.add_option("--replace", nargs=3, default=False,
120 metavar = "search replace_by extension",
121 help="file search-replace")
Anand Doshi7854f812012-03-14 12:01:13 +0530122
123 parser.add_option("--cci", nargs=1, metavar="CacheItem Key",
124 help="Clear Cache Item")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530125
126
Rushabh Mehtae219db02012-02-06 15:33:08 +0530127 return parser.parse_args()
128
129def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +0530130 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +0530131 sys.path.append('lib/py')
132 import webnotes
133 import webnotes.defs
134 sys.path.append(webnotes.defs.modules_path)
135
136 (options, args) = setup_options()
137
138
139 from webnotes.db import Database
140 import webnotes.modules.patch_handler
141
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100142 # connect
143 if options.db_name is not None:
144 webnotes.connect(options.db_name)
145
Rushabh Mehtae219db02012-02-06 15:33:08 +0530146 # build
147 if options.build:
148 import build.project
149 build.project.build()
150
151 elif options.clear:
152 from build.project import increment_version
153 print "Version:" + str(increment_version())
154
155 # code replace
156 elif options.replace:
157 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
158
159 # git
160 elif options.status:
161 os.system('git status')
162 os.chdir('lib')
163 os.system('git status')
164
165 elif options.pull:
166 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
167 os.chdir('lib')
168 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
169
170 elif options.push:
171 os.system('git commit -a -m "%s"' % options.push[2])
172 os.system('git push %s %s' % (options.push[0], options.push[1]))
173 os.chdir('lib')
174 os.system('git commit -a -m "%s"' % options.push[2])
175 os.system('git push %s %s' % (options.push[0], options.push[1]))
176
Rushabh Mehtae219db02012-02-06 15:33:08 +0530177 # patch
178 elif options.patch_list:
179 # clear log
180 webnotes.modules.patch_handler.log_list = []
181
Rushabh Mehtae219db02012-02-06 15:33:08 +0530182 # run individual patches
183 for patch in options.patch_list:
184 webnotes.modules.patch_handler.run_single(\
185 patchmodule = patch, force = options.force)
186
187 print '\n'.join(webnotes.modules.patch_handler.log_list)
188
189 # reload
190 elif options.reload_doc:
191 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530192 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530193 print '\n'.join(webnotes.modules.patch_handler.log_list)
194
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530195 elif options.export_doc:
196 from webnotes.modules import export_doc
197 export_doc(options.export_doc[0], options.export_doc[1])
198
Rushabh Mehtae219db02012-02-06 15:33:08 +0530199 # run all pending
200 elif options.run_latest:
201 webnotes.modules.patch_handler.run_all()
202 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100203
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530204 elif options.install:
205 from webnotes.install_lib.install import Installer
206 inst = Installer('root', options.install[0])
207 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530208 password='admin', verbose = 1)
209
210 elif options.sync_with_gateway:
211 if int(options.sync_with_gateway[0]) in [0, 1]:
212 webnotes.conn.begin()
213 webnotes.conn.sql("""\
214 UPDATE `tabSingles` SET value=%s
215 WHERE field='sync_with_gateway' AND doctype='Control Panel'""", int(options.sync_with_gateway[0]))
216 webnotes.conn.commit()
217 webnotes.message_log.append("sync_with_gateway set to %s" % options.sync_with_gateway[0])
218 else:
219 webnotes.message_log.append("ERROR: sync_with_gateway can be either 0 or 1")
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530220
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530221 elif options.diff_ref_file is not None:
Rushabh Mehta17773d02012-02-22 15:55:41 +0530222 import webnotes.modules.diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530223 webnotes.modules.diff.diff_ref_file()
224
225 elif options.diff_ref_db is not None:
226 import webnotes.modules.diff
227 webnotes.modules.diff.diff_ref_db()
Rushabh Mehta17773d02012-02-22 15:55:41 +0530228
Rushabh Mehta1a355742012-02-23 11:46:28 +0530229 elif options.run_scheduler:
230 import webnotes.utils.scheduler
231 print webnotes.utils.scheduler.execute()
232
233 elif options.run_scheduler_event is not None:
234 import webnotes.utils.scheduler
235 print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
236
Anand Doshi7854f812012-03-14 12:01:13 +0530237 elif options.cci is not None:
238 if options.cci=='all':
239 webnotes.conn.sql("DELETE FROM __CacheItem")
240 else:
241 from webnotes.utils.cache import CacheItem
242 CacheItem(options.cci).clear()
243
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100244 # print messages
245 if webnotes.message_log:
246 print '\n'.join(webnotes.message_log)
247
Rushabh Mehtae219db02012-02-06 15:33:08 +0530248if __name__=='__main__':
249 run()