blob: 24677e84ba6cb77664f0d000d723fe5f2917b864 [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)
28 with open(fpath, 'r') as f:
29 content = f.read()
30
31 if re.search(txt1, content):
Nabin Hait26889bd2012-03-14 11:08:15 +053032 search_replace_with_prompt(fpath, txt1, txt2)
33
34
35
36def search_replace_with_prompt(fpath, txt1, txt2):
37 """ Search and replace all txt1 by txt2 in the file with confirmation"""
38
39 from termcolor import colored
40 with open(fpath, 'r') as f:
41 content = f.readlines()
42
43 tmp = []
44 for c in content:
45 if c.find(txt1) != -1:
46 print '\n', fpath
47 print colored(txt1, 'red').join(c[:-1].split(txt1))
48
49 a = raw_input('Do you want to Change [y/n]?')
50 if a=='y':
51 c = c.replace(txt1, txt2)
52 tmp.append(c)
53
54 with open(fpath, 'w') as f:
55 f.write(''.join(tmp))
56 print colored('Updated in %s' % fpath, 'green')
57
Rushabh Mehtae219db02012-02-06 15:33:08 +053058
59def setup_options():
60 from optparse import OptionParser
61 parser = OptionParser()
Rushabh Mehta1a355742012-02-23 11:46:28 +053062
63 parser.add_option("-d", "--db",
64 dest="db_name",
65 help="Apply the patches on given db")
66
67 # build
Rushabh Mehtae219db02012-02-06 15:33:08 +053068 parser.add_option("-b", "--build", default=False, action="store_true",
69 help="minify + concat js files")
70 parser.add_option("-c", "--clear", default=False, action="store_true",
71 help="increment version")
Rushabh Mehta1a355742012-02-23 11:46:28 +053072
73 # git
Rushabh Mehtae219db02012-02-06 15:33:08 +053074 parser.add_option("--status", default=False, action="store_true",
75 help="git status")
76 parser.add_option("--pull", nargs=2, default=False,
77 metavar = "remote branch",
78 help="git pull (both repos)")
79 parser.add_option("--push", nargs=3, default=False,
80 metavar = "remote branch comment",
81 help="git commit + push (both repos) [remote] [branch] [comment]")
82 parser.add_option("-l", "--latest",
83 action="store_true", dest="run_latest", default=False,
84 help="Apply the latest patches")
Rushabh Mehta1a355742012-02-23 11:46:28 +053085
86 # patch
Rushabh Mehtae219db02012-02-06 15:33:08 +053087 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
88 action="append",
89 help="Apply patch")
90 parser.add_option("-f", "--force",
91 action="store_true", dest="force", default=False,
92 help="Force Apply all patches specified using option -p or --patch")
Rushabh Mehtae6516482012-02-06 16:28:06 +053093 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +053094 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +053095 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
96 help="export doc")
Rushabh Mehta1a355742012-02-23 11:46:28 +053097
98 # install
Rushabh Mehta0b2874a2012-02-09 12:45:50 +053099 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
100 help="install fresh db")
Anand Doshi12b6da22012-03-20 14:55:16 +0530101
Rushabh Mehta1a355742012-02-23 11:46:28 +0530102 # diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530103 parser.add_option('--diff_ref_file', nargs=0, \
104 help="Get missing database records and mismatch properties, with file as reference")
105 parser.add_option('--diff_ref_db', nargs=0, \
106 help="Get missing .txt files and mismatch properties, with database as reference")
Anand Doshibae69082012-02-09 13:34:12 +0530107
Rushabh Mehta1a355742012-02-23 11:46:28 +0530108 # scheduler
109 parser.add_option('--run_scheduler', default=False, action="store_true",
110 help="Trigger scheduler")
111 parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
112 help="Run scheduler event")
113
114 # misc
115 parser.add_option("--replace", nargs=3, default=False,
116 metavar = "search replace_by extension",
117 help="file search-replace")
Anand Doshi7854f812012-03-14 12:01:13 +0530118
119 parser.add_option("--cci", nargs=1, metavar="CacheItem Key",
120 help="Clear Cache Item")
Rushabh Mehta1a355742012-02-23 11:46:28 +0530121
122
Rushabh Mehtae219db02012-02-06 15:33:08 +0530123 return parser.parse_args()
124
125def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +0530126 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +0530127 sys.path.append('lib/py')
128 import webnotes
129 import webnotes.defs
130 sys.path.append(webnotes.defs.modules_path)
131
132 (options, args) = setup_options()
133
134
135 from webnotes.db import Database
136 import webnotes.modules.patch_handler
137
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100138 # connect
139 if options.db_name is not None:
140 webnotes.connect(options.db_name)
141
Rushabh Mehtae219db02012-02-06 15:33:08 +0530142 # build
143 if options.build:
144 import build.project
145 build.project.build()
146
147 elif options.clear:
148 from build.project import increment_version
149 print "Version:" + str(increment_version())
150
151 # code replace
152 elif options.replace:
153 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
154
155 # git
156 elif options.status:
157 os.system('git status')
158 os.chdir('lib')
159 os.system('git status')
160
161 elif options.pull:
162 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
163 os.chdir('lib')
164 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
165
166 elif options.push:
167 os.system('git commit -a -m "%s"' % options.push[2])
168 os.system('git push %s %s' % (options.push[0], options.push[1]))
169 os.chdir('lib')
170 os.system('git commit -a -m "%s"' % options.push[2])
171 os.system('git push %s %s' % (options.push[0], options.push[1]))
172
Rushabh Mehtae219db02012-02-06 15:33:08 +0530173 # patch
174 elif options.patch_list:
175 # clear log
176 webnotes.modules.patch_handler.log_list = []
177
Rushabh Mehtae219db02012-02-06 15:33:08 +0530178 # run individual patches
179 for patch in options.patch_list:
180 webnotes.modules.patch_handler.run_single(\
181 patchmodule = patch, force = options.force)
182
183 print '\n'.join(webnotes.modules.patch_handler.log_list)
184
185 # reload
186 elif options.reload_doc:
187 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530188 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530189 print '\n'.join(webnotes.modules.patch_handler.log_list)
190
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530191 elif options.export_doc:
192 from webnotes.modules import export_doc
193 export_doc(options.export_doc[0], options.export_doc[1])
194
Rushabh Mehtae219db02012-02-06 15:33:08 +0530195 # run all pending
196 elif options.run_latest:
197 webnotes.modules.patch_handler.run_all()
198 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100199
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530200 elif options.install:
201 from webnotes.install_lib.install import Installer
202 inst = Installer('root', options.install[0])
203 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530204 password='admin', verbose = 1)
205
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530206 elif options.diff_ref_file is not None:
Rushabh Mehta17773d02012-02-22 15:55:41 +0530207 import webnotes.modules.diff
Rushabh Mehtac952bc92012-02-22 17:30:07 +0530208 webnotes.modules.diff.diff_ref_file()
209
210 elif options.diff_ref_db is not None:
211 import webnotes.modules.diff
212 webnotes.modules.diff.diff_ref_db()
Rushabh Mehta17773d02012-02-22 15:55:41 +0530213
Rushabh Mehta1a355742012-02-23 11:46:28 +0530214 elif options.run_scheduler:
215 import webnotes.utils.scheduler
216 print webnotes.utils.scheduler.execute()
217
218 elif options.run_scheduler_event is not None:
219 import webnotes.utils.scheduler
220 print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
221
Anand Doshi7854f812012-03-14 12:01:13 +0530222 elif options.cci is not None:
223 if options.cci=='all':
224 webnotes.conn.sql("DELETE FROM __CacheItem")
225 else:
226 from webnotes.utils.cache import CacheItem
227 CacheItem(options.cci).clear()
228
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100229 # print messages
230 if webnotes.message_log:
231 print '\n'.join(webnotes.message_log)
232
Rushabh Mehtae219db02012-02-06 15:33:08 +0530233if __name__=='__main__':
234 run()