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) |
| 28 | with open(fpath, 'r') as f: |
| 29 | content = f.read() |
| 30 | |
| 31 | if re.search(txt1, content): |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 32 | search_replace_with_prompt(fpath, txt1, txt2) |
| 33 | |
| 34 | |
| 35 | |
| 36 | def 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) |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame^] | 52 | tmp.append(c) |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 53 | |
| 54 | with open(fpath, 'w') as f: |
| 55 | f.write(''.join(tmp)) |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame^] | 56 | print colored('Updated', 'green') |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 57 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 58 | |
| 59 | def setup_options(): |
| 60 | from optparse import OptionParser |
| 61 | parser = OptionParser() |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 62 | |
| 63 | parser.add_option("-d", "--db", |
| 64 | dest="db_name", |
| 65 | help="Apply the patches on given db") |
| 66 | |
| 67 | # build |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 68 | 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 Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 72 | |
| 73 | # git |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 74 | 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 Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 85 | |
| 86 | # patch |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 87 | 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 Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 93 | parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname", |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 94 | help="reload doc") |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 95 | parser.add_option('--export_doc', nargs=2, metavar = "doctype docname", |
| 96 | help="export doc") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 97 | |
| 98 | # install |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 99 | parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source", |
| 100 | help="install fresh db") |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 101 | parser.add_option('--sync_with_gateway', nargs=1, metavar = "1/0", \ |
| 102 | help="Set or Unset Sync with Gateway") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 103 | |
| 104 | # diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 105 | parser.add_option('--diff_ref_file', nargs=0, \ |
| 106 | help="Get missing database records and mismatch properties, with file as reference") |
| 107 | parser.add_option('--diff_ref_db', nargs=0, \ |
| 108 | help="Get missing .txt files and mismatch properties, with database as reference") |
Anand Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 109 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 110 | # scheduler |
| 111 | parser.add_option('--run_scheduler', default=False, action="store_true", |
| 112 | help="Trigger scheduler") |
| 113 | parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]", |
| 114 | help="Run scheduler event") |
| 115 | |
| 116 | # misc |
| 117 | parser.add_option("--replace", nargs=3, default=False, |
| 118 | metavar = "search replace_by extension", |
| 119 | help="file search-replace") |
| 120 | |
| 121 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 122 | return parser.parse_args() |
| 123 | |
| 124 | def run(): |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 125 | sys.path.append('lib') |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 126 | sys.path.append('lib/py') |
| 127 | import webnotes |
| 128 | import webnotes.defs |
| 129 | sys.path.append(webnotes.defs.modules_path) |
| 130 | |
| 131 | (options, args) = setup_options() |
| 132 | |
| 133 | |
| 134 | from webnotes.db import Database |
| 135 | import webnotes.modules.patch_handler |
| 136 | |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 137 | # connect |
| 138 | if options.db_name is not None: |
| 139 | webnotes.connect(options.db_name) |
| 140 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 141 | # build |
| 142 | if options.build: |
| 143 | import build.project |
| 144 | build.project.build() |
| 145 | |
| 146 | elif options.clear: |
| 147 | from build.project import increment_version |
| 148 | print "Version:" + str(increment_version()) |
| 149 | |
| 150 | # code replace |
| 151 | elif options.replace: |
| 152 | replace_code('.', options.replace[0], options.replace[1], options.replace[2]) |
| 153 | |
| 154 | # git |
| 155 | elif options.status: |
| 156 | os.system('git status') |
| 157 | os.chdir('lib') |
| 158 | os.system('git status') |
| 159 | |
| 160 | elif options.pull: |
| 161 | os.system('git pull %s %s' % (options.pull[0], options.pull[1])) |
| 162 | os.chdir('lib') |
| 163 | os.system('git pull %s %s' % (options.pull[0], options.pull[1])) |
| 164 | |
| 165 | elif options.push: |
| 166 | os.system('git commit -a -m "%s"' % options.push[2]) |
| 167 | os.system('git push %s %s' % (options.push[0], options.push[1])) |
| 168 | os.chdir('lib') |
| 169 | os.system('git commit -a -m "%s"' % options.push[2]) |
| 170 | os.system('git push %s %s' % (options.push[0], options.push[1])) |
| 171 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 172 | # patch |
| 173 | elif options.patch_list: |
| 174 | # clear log |
| 175 | webnotes.modules.patch_handler.log_list = [] |
| 176 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 177 | # run individual patches |
| 178 | for patch in options.patch_list: |
| 179 | webnotes.modules.patch_handler.run_single(\ |
| 180 | patchmodule = patch, force = options.force) |
| 181 | |
| 182 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
| 183 | |
| 184 | # reload |
| 185 | elif options.reload_doc: |
| 186 | webnotes.modules.patch_handler.reload_doc(\ |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 187 | {"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] | 188 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
| 189 | |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 190 | elif options.export_doc: |
| 191 | from webnotes.modules import export_doc |
| 192 | export_doc(options.export_doc[0], options.export_doc[1]) |
| 193 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 194 | # run all pending |
| 195 | elif options.run_latest: |
| 196 | webnotes.modules.patch_handler.run_all() |
| 197 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 198 | |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 199 | elif options.install: |
| 200 | from webnotes.install_lib.install import Installer |
| 201 | inst = Installer('root', options.install[0]) |
| 202 | inst.import_from_db(options.install[1], source_path=options.install[2], \ |
Anand Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 203 | password='admin', verbose = 1) |
| 204 | |
| 205 | elif options.sync_with_gateway: |
| 206 | if int(options.sync_with_gateway[0]) in [0, 1]: |
| 207 | webnotes.conn.begin() |
| 208 | webnotes.conn.sql("""\ |
| 209 | UPDATE `tabSingles` SET value=%s |
| 210 | WHERE field='sync_with_gateway' AND doctype='Control Panel'""", int(options.sync_with_gateway[0])) |
| 211 | webnotes.conn.commit() |
| 212 | webnotes.message_log.append("sync_with_gateway set to %s" % options.sync_with_gateway[0]) |
| 213 | else: |
| 214 | webnotes.message_log.append("ERROR: sync_with_gateway can be either 0 or 1") |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 215 | |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 216 | elif options.diff_ref_file is not None: |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 217 | import webnotes.modules.diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 218 | webnotes.modules.diff.diff_ref_file() |
| 219 | |
| 220 | elif options.diff_ref_db is not None: |
| 221 | import webnotes.modules.diff |
| 222 | webnotes.modules.diff.diff_ref_db() |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 223 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 224 | elif options.run_scheduler: |
| 225 | import webnotes.utils.scheduler |
| 226 | print webnotes.utils.scheduler.execute() |
| 227 | |
| 228 | elif options.run_scheduler_event is not None: |
| 229 | import webnotes.utils.scheduler |
| 230 | print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event) |
| 231 | |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 232 | # print messages |
| 233 | if webnotes.message_log: |
| 234 | print '\n'.join(webnotes.message_log) |
| 235 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 236 | if __name__=='__main__': |
| 237 | run() |