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 | |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 21 | def replace_code(start, txt1, txt2, extn, search=None): |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 22 | """replace all txt1 by txt2 in files with extension (extn)""" |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 23 | import webnotes.utils |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 24 | import os, re |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 25 | esc = webnotes.utils.make_esc('[]') |
| 26 | if not search: search = esc(txt1) |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 27 | for wt in os.walk(start, followlinks=1): |
| 28 | for fn in wt[2]: |
| 29 | if fn.split('.')[-1]==extn: |
| 30 | fpath = os.path.join(wt[0], fn) |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 31 | if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary |
| 32 | with open(fpath, 'r') as f: |
| 33 | content = f.read() |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 34 | |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 35 | if re.search(search, content): |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame] | 36 | res = search_replace_with_prompt(fpath, txt1, txt2) |
| 37 | if res == 'skip': |
| 38 | return 'skip' |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 39 | |
| 40 | |
| 41 | |
| 42 | def search_replace_with_prompt(fpath, txt1, txt2): |
| 43 | """ Search and replace all txt1 by txt2 in the file with confirmation""" |
| 44 | |
| 45 | from termcolor import colored |
| 46 | with open(fpath, 'r') as f: |
| 47 | content = f.readlines() |
| 48 | |
| 49 | tmp = [] |
| 50 | for c in content: |
| 51 | if c.find(txt1) != -1: |
| 52 | print '\n', fpath |
| 53 | print colored(txt1, 'red').join(c[:-1].split(txt1)) |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 54 | a = '' |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame] | 55 | while a.lower() not in ['y', 'n', 'skip']: |
| 56 | a = raw_input('Do you want to Change [y/n/skip]?') |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 57 | if a.lower() == 'y': |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 58 | c = c.replace(txt1, txt2) |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame] | 59 | elif a.lower() == 'skip': |
| 60 | return 'skip' |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame] | 61 | tmp.append(c) |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 62 | |
| 63 | with open(fpath, 'w') as f: |
| 64 | f.write(''.join(tmp)) |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame] | 65 | print colored('Updated', 'green') |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 66 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 67 | |
| 68 | def setup_options(): |
| 69 | from optparse import OptionParser |
| 70 | parser = OptionParser() |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 71 | |
| 72 | parser.add_option("-d", "--db", |
| 73 | dest="db_name", |
| 74 | help="Apply the patches on given db") |
| 75 | |
| 76 | # build |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 77 | parser.add_option("-b", "--build", default=False, action="store_true", |
| 78 | help="minify + concat js files") |
| 79 | parser.add_option("-c", "--clear", default=False, action="store_true", |
| 80 | help="increment version") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 81 | |
| 82 | # git |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 83 | parser.add_option("--status", default=False, action="store_true", |
| 84 | help="git status") |
| 85 | parser.add_option("--pull", nargs=2, default=False, |
| 86 | metavar = "remote branch", |
| 87 | help="git pull (both repos)") |
| 88 | parser.add_option("--push", nargs=3, default=False, |
| 89 | metavar = "remote branch comment", |
| 90 | help="git commit + push (both repos) [remote] [branch] [comment]") |
| 91 | parser.add_option("-l", "--latest", |
| 92 | action="store_true", dest="run_latest", default=False, |
| 93 | help="Apply the latest patches") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 94 | |
| 95 | # patch |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 96 | parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module', |
| 97 | action="append", |
| 98 | help="Apply patch") |
| 99 | parser.add_option("-f", "--force", |
| 100 | action="store_true", dest="force", default=False, |
| 101 | help="Force Apply all patches specified using option -p or --patch") |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 102 | parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname", |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 103 | help="reload doc") |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 104 | parser.add_option('--export_doc', nargs=2, metavar = "doctype docname", |
| 105 | help="export doc") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 106 | |
| 107 | # install |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 108 | parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source", |
| 109 | help="install fresh db") |
Anand Doshi | 12b6da2 | 2012-03-20 14:55:16 +0530 | [diff] [blame] | 110 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 111 | # diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 112 | parser.add_option('--diff_ref_file', nargs=0, \ |
| 113 | help="Get missing database records and mismatch properties, with file as reference") |
| 114 | parser.add_option('--diff_ref_db', nargs=0, \ |
| 115 | help="Get missing .txt files and mismatch properties, with database as reference") |
Anand Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 116 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 117 | # scheduler |
| 118 | parser.add_option('--run_scheduler', default=False, action="store_true", |
| 119 | help="Trigger scheduler") |
| 120 | parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]", |
| 121 | help="Run scheduler event") |
| 122 | |
| 123 | # misc |
| 124 | parser.add_option("--replace", nargs=3, default=False, |
| 125 | metavar = "search replace_by extension", |
| 126 | help="file search-replace") |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 127 | |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 128 | parser.add_option("--cci", nargs=1, metavar="CacheItem Key or all", |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 129 | help="Clear Cache Item") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 130 | |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 131 | parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files", |
| 132 | nargs=0) |
| 133 | |
| 134 | parser.add_option("--sync", help="Synchronize given DocType using txt file", |
| 135 | nargs=2, metavar="module doctype (use their folder names)") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 136 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 137 | return parser.parse_args() |
| 138 | |
| 139 | def run(): |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 140 | sys.path.append('lib') |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 141 | sys.path.append('lib/py') |
| 142 | import webnotes |
| 143 | import webnotes.defs |
| 144 | sys.path.append(webnotes.defs.modules_path) |
| 145 | |
| 146 | (options, args) = setup_options() |
| 147 | |
| 148 | |
| 149 | from webnotes.db import Database |
| 150 | import webnotes.modules.patch_handler |
| 151 | |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 152 | # connect |
| 153 | if options.db_name is not None: |
| 154 | webnotes.connect(options.db_name) |
| 155 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 156 | # build |
| 157 | if options.build: |
| 158 | import build.project |
| 159 | build.project.build() |
| 160 | |
| 161 | elif options.clear: |
| 162 | from build.project import increment_version |
| 163 | print "Version:" + str(increment_version()) |
| 164 | |
| 165 | # code replace |
| 166 | elif options.replace: |
| 167 | replace_code('.', options.replace[0], options.replace[1], options.replace[2]) |
| 168 | |
| 169 | # git |
| 170 | elif options.status: |
| 171 | os.system('git status') |
| 172 | os.chdir('lib') |
| 173 | os.system('git status') |
| 174 | |
| 175 | elif options.pull: |
| 176 | os.system('git pull %s %s' % (options.pull[0], options.pull[1])) |
| 177 | os.chdir('lib') |
| 178 | os.system('git pull %s %s' % (options.pull[0], options.pull[1])) |
| 179 | |
| 180 | elif options.push: |
| 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 | os.chdir('lib') |
| 184 | os.system('git commit -a -m "%s"' % options.push[2]) |
| 185 | os.system('git push %s %s' % (options.push[0], options.push[1])) |
| 186 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 187 | # patch |
| 188 | elif options.patch_list: |
| 189 | # clear log |
| 190 | webnotes.modules.patch_handler.log_list = [] |
| 191 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 192 | # run individual patches |
| 193 | for patch in options.patch_list: |
| 194 | webnotes.modules.patch_handler.run_single(\ |
| 195 | patchmodule = patch, force = options.force) |
| 196 | |
| 197 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
| 198 | |
| 199 | # reload |
| 200 | elif options.reload_doc: |
| 201 | webnotes.modules.patch_handler.reload_doc(\ |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 202 | {"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] | 203 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
| 204 | |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 205 | elif options.export_doc: |
| 206 | from webnotes.modules import export_doc |
| 207 | export_doc(options.export_doc[0], options.export_doc[1]) |
| 208 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 209 | # run all pending |
| 210 | elif options.run_latest: |
| 211 | webnotes.modules.patch_handler.run_all() |
| 212 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 213 | |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 214 | elif options.install: |
| 215 | from webnotes.install_lib.install import Installer |
| 216 | inst = Installer('root', options.install[0]) |
| 217 | inst.import_from_db(options.install[1], source_path=options.install[2], \ |
Anand Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 218 | password='admin', verbose = 1) |
| 219 | |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 220 | elif options.diff_ref_file is not None: |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 221 | import webnotes.modules.diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 222 | webnotes.modules.diff.diff_ref_file() |
| 223 | |
| 224 | elif options.diff_ref_db is not None: |
| 225 | import webnotes.modules.diff |
| 226 | webnotes.modules.diff.diff_ref_db() |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 227 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 228 | elif options.run_scheduler: |
| 229 | import webnotes.utils.scheduler |
| 230 | print webnotes.utils.scheduler.execute() |
| 231 | |
| 232 | elif options.run_scheduler_event is not None: |
| 233 | import webnotes.utils.scheduler |
| 234 | print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event) |
| 235 | |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 236 | elif options.cci is not None: |
| 237 | if options.cci=='all': |
| 238 | webnotes.conn.sql("DELETE FROM __CacheItem") |
| 239 | else: |
| 240 | from webnotes.utils.cache import CacheItem |
| 241 | CacheItem(options.cci).clear() |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 242 | |
| 243 | elif options.sync_all is not None: |
| 244 | import webnotes.model.sync |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 245 | webnotes.model.sync.sync_all(options.force or 0) |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 246 | |
| 247 | elif options.sync is not None: |
| 248 | import webnotes.model.sync |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 249 | webnotes.model.sync.sync(options.sync[0], options.sync[1], options.force or 0) |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 250 | |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 251 | # print messages |
| 252 | if webnotes.message_log: |
| 253 | print '\n'.join(webnotes.message_log) |
| 254 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 255 | if __name__=='__main__': |
| 256 | run() |