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