blob: 428d6b057354e6732a750915494e2f616e8d935e [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")
122
123
Rushabh Mehtae219db02012-02-06 15:33:08 +0530124 return parser.parse_args()
125
126def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +0530127 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +0530128 sys.path.append('lib/py')
129 import webnotes
130 import webnotes.defs
131 sys.path.append(webnotes.defs.modules_path)
132
133 (options, args) = setup_options()
134
135
136 from webnotes.db import Database
137 import webnotes.modules.patch_handler
138
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100139 # connect
140 if options.db_name is not None:
141 webnotes.connect(options.db_name)
142
Rushabh Mehtae219db02012-02-06 15:33:08 +0530143 # build
144 if options.build:
145 import build.project
146 build.project.build()
147
148 elif options.clear:
149 from build.project import increment_version
150 print "Version:" + str(increment_version())
151
152 # code replace
153 elif options.replace:
154 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
155
156 # git
157 elif options.status:
158 os.system('git status')
159 os.chdir('lib')
160 os.system('git status')
161
162 elif options.pull:
163 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
164 os.chdir('lib')
165 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
166
167 elif options.push:
168 os.system('git commit -a -m "%s"' % options.push[2])
169 os.system('git push %s %s' % (options.push[0], options.push[1]))
170 os.chdir('lib')
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
Rushabh Mehtae219db02012-02-06 15:33:08 +0530174 # patch
175 elif options.patch_list:
176 # clear log
177 webnotes.modules.patch_handler.log_list = []
178
Rushabh Mehtae219db02012-02-06 15:33:08 +0530179 # run individual patches
180 for patch in options.patch_list:
181 webnotes.modules.patch_handler.run_single(\
182 patchmodule = patch, force = options.force)
183
184 print '\n'.join(webnotes.modules.patch_handler.log_list)
185
186 # reload
187 elif options.reload_doc:
188 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530189 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530190 print '\n'.join(webnotes.modules.patch_handler.log_list)
191
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530192 elif options.export_doc:
193 from webnotes.modules import export_doc
194 export_doc(options.export_doc[0], options.export_doc[1])
195
Rushabh Mehtae219db02012-02-06 15:33:08 +0530196 # run all pending
197 elif options.run_latest:
198 webnotes.modules.patch_handler.run_all()
199 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100200
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530201 elif options.install:
202 from webnotes.install_lib.install import Installer
203 inst = Installer('root', options.install[0])
204 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530205 password='admin', verbose = 1)
206
207 elif options.sync_with_gateway:
208 if int(options.sync_with_gateway[0]) in [0, 1]:
209 webnotes.conn.begin()
210 webnotes.conn.sql("""\
211 UPDATE `tabSingles` SET value=%s
212 WHERE field='sync_with_gateway' AND doctype='Control Panel'""", int(options.sync_with_gateway[0]))
213 webnotes.conn.commit()
214 webnotes.message_log.append("sync_with_gateway set to %s" % options.sync_with_gateway[0])
215 else:
216 webnotes.message_log.append("ERROR: sync_with_gateway can be either 0 or 1")
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530217
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530218 elif options.diff_ref_file is not None:
Rushabh Mehta17773d02012-02-22 15:55:41 +0530219 import webnotes.modules.diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530220 webnotes.modules.diff.diff_ref_file()
221
222 elif options.diff_ref_db is not None:
223 import webnotes.modules.diff
224 webnotes.modules.diff.diff_ref_db()
Rushabh Mehta17773d02012-02-22 15:55:41 +0530225
Rushabh Mehta1a355742012-02-23 11:46:28 +0530226 elif options.run_scheduler:
227 import webnotes.utils.scheduler
228 print webnotes.utils.scheduler.execute()
229
230 elif options.run_scheduler_event is not None:
231 import webnotes.utils.scheduler
232 print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
233
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100234 # print messages
235 if webnotes.message_log:
236 print '\n'.join(webnotes.message_log)
237
Rushabh Mehtae219db02012-02-06 15:33:08 +0530238if __name__=='__main__':
239 run()