Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 3 | # 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 Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 19 | import os, sys |
| 20 | |
| 21 | def 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 Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 28 | 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 Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 31 | |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 32 | if re.search(txt1, content): |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame^] | 33 | res = search_replace_with_prompt(fpath, txt1, txt2) |
| 34 | if res == 'skip': |
| 35 | return 'skip' |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 36 | |
| 37 | |
| 38 | |
| 39 | def 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 Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 51 | a = '' |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame^] | 52 | while a.lower() not in ['y', 'n', 'skip']: |
| 53 | a = raw_input('Do you want to Change [y/n/skip]?') |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 54 | if a.lower() == 'y': |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 55 | c = c.replace(txt1, txt2) |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame^] | 56 | elif a.lower() == 'skip': |
| 57 | return 'skip' |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame] | 58 | tmp.append(c) |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 59 | |
| 60 | with open(fpath, 'w') as f: |
| 61 | f.write(''.join(tmp)) |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame] | 62 | print colored('Updated', 'green') |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 63 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 64 | |
| 65 | def setup_options(): |
| 66 | from optparse import OptionParser |
| 67 | parser = OptionParser() |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 68 | |
| 69 | parser.add_option("-d", "--db", |
| 70 | dest="db_name", |
| 71 | help="Apply the patches on given db") |
| 72 | |
| 73 | # build |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 74 | 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 Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 78 | |
| 79 | # git |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 80 | 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 Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 91 | |
| 92 | # patch |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 93 | 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 Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 99 | parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname", |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 100 | help="reload doc") |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 101 | parser.add_option('--export_doc', nargs=2, metavar = "doctype docname", |
| 102 | help="export doc") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 103 | |
| 104 | # install |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 105 | parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source", |
| 106 | help="install fresh db") |
Anand Doshi | 12b6da2 | 2012-03-20 14:55:16 +0530 | [diff] [blame] | 107 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 108 | # diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 109 | 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 Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 113 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 114 | # 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 Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 124 | |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 125 | parser.add_option("--cci", nargs=1, metavar="CacheItem Key or all", |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 126 | help="Clear Cache Item") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 127 | |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 128 | 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 Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 133 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 134 | return parser.parse_args() |
| 135 | |
| 136 | def run(): |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 137 | sys.path.append('lib') |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 138 | 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 Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 149 | # connect |
| 150 | if options.db_name is not None: |
| 151 | webnotes.connect(options.db_name) |
| 152 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 153 | # 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 Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 184 | # patch |
| 185 | elif options.patch_list: |
| 186 | # clear log |
| 187 | webnotes.modules.patch_handler.log_list = [] |
| 188 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 189 | # 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 Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 199 | {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]}) |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 200 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
| 201 | |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 202 | elif options.export_doc: |
| 203 | from webnotes.modules import export_doc |
| 204 | export_doc(options.export_doc[0], options.export_doc[1]) |
| 205 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 206 | # 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 Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 210 | |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 211 | 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 Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 215 | password='admin', verbose = 1) |
| 216 | |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 217 | elif options.diff_ref_file is not None: |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 218 | import webnotes.modules.diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 219 | 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 Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 224 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 225 | 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 Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 233 | 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 Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 239 | |
| 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 Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 247 | |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 248 | # print messages |
| 249 | if webnotes.message_log: |
| 250 | print '\n'.join(webnotes.message_log) |
| 251 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 252 | if __name__=='__main__': |
| 253 | run() |