Rushabh Mehta | 03e8787 | 2012-04-14 15:02:17 +0530 | [diff] [blame] | 1 | #!/usr/bin/python |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 2 | |
Rushabh Mehta | 03e8787 | 2012-04-14 15:02:17 +0530 | [diff] [blame] | 3 | # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com) |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 4 | # |
Rushabh Mehta | 03e8787 | 2012-04-14 15:02:17 +0530 | [diff] [blame] | 5 | # MIT License (MIT) |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 6 | # |
Rushabh Mehta | 03e8787 | 2012-04-14 15:02:17 +0530 | [diff] [blame] | 7 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | # copy of this software and associated documentation files (the "Software"), |
| 9 | # to deal in the Software without restriction, including without limitation |
| 10 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | # and/or sell copies of the Software, and to permit persons to whom the |
| 12 | # Software is furnished to do so, subject to the following conditions: |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 13 | # |
Rushabh Mehta | 03e8787 | 2012-04-14 15:02:17 +0530 | [diff] [blame] | 14 | # The above copyright notice and this permission notice shall be included in |
| 15 | # all copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 18 | # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 19 | # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 21 | # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 22 | # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | # |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 24 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 25 | import os, sys |
| 26 | |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 27 | def replace_code(start, txt1, txt2, extn, search=None): |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 28 | """replace all txt1 by txt2 in files with extension (extn)""" |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 29 | import webnotes.utils |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 30 | import os, re |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 31 | esc = webnotes.utils.make_esc('[]') |
| 32 | if not search: search = esc(txt1) |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 33 | for wt in os.walk(start, followlinks=1): |
| 34 | for fn in wt[2]: |
| 35 | if fn.split('.')[-1]==extn: |
| 36 | fpath = os.path.join(wt[0], fn) |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 37 | if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary |
| 38 | with open(fpath, 'r') as f: |
| 39 | content = f.read() |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 40 | |
Anand Doshi | fa0cd89 | 2012-04-05 13:03:35 +0530 | [diff] [blame] | 41 | if re.search(search, content): |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame] | 42 | res = search_replace_with_prompt(fpath, txt1, txt2) |
| 43 | if res == 'skip': |
| 44 | return 'skip' |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 45 | |
| 46 | |
| 47 | |
| 48 | def search_replace_with_prompt(fpath, txt1, txt2): |
| 49 | """ Search and replace all txt1 by txt2 in the file with confirmation""" |
| 50 | |
| 51 | from termcolor import colored |
| 52 | with open(fpath, 'r') as f: |
| 53 | content = f.readlines() |
| 54 | |
| 55 | tmp = [] |
| 56 | for c in content: |
| 57 | if c.find(txt1) != -1: |
| 58 | print '\n', fpath |
| 59 | print colored(txt1, 'red').join(c[:-1].split(txt1)) |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 60 | a = '' |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame] | 61 | while a.lower() not in ['y', 'n', 'skip']: |
| 62 | a = raw_input('Do you want to Change [y/n/skip]?') |
Nabin Hait | 19ee00b | 2012-03-16 16:22:30 +0530 | [diff] [blame] | 63 | if a.lower() == 'y': |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 64 | c = c.replace(txt1, txt2) |
Anand Doshi | 5112b5b | 2012-04-02 19:12:29 +0530 | [diff] [blame] | 65 | elif a.lower() == 'skip': |
| 66 | return 'skip' |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame] | 67 | tmp.append(c) |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 68 | |
| 69 | with open(fpath, 'w') as f: |
| 70 | f.write(''.join(tmp)) |
Nabin Hait | 1732905 | 2012-03-14 12:04:16 +0530 | [diff] [blame] | 71 | print colored('Updated', 'green') |
Nabin Hait | 26889bd | 2012-03-14 11:08:15 +0530 | [diff] [blame] | 72 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 73 | |
| 74 | def setup_options(): |
| 75 | from optparse import OptionParser |
| 76 | parser = OptionParser() |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 77 | |
| 78 | parser.add_option("-d", "--db", |
| 79 | dest="db_name", |
| 80 | help="Apply the patches on given db") |
| 81 | |
| 82 | # build |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 83 | parser.add_option("-b", "--build", default=False, action="store_true", |
| 84 | help="minify + concat js files") |
| 85 | parser.add_option("-c", "--clear", default=False, action="store_true", |
Rushabh Mehta | 03e8787 | 2012-04-14 15:02:17 +0530 | [diff] [blame] | 86 | help="reset version") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 87 | |
| 88 | # git |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 89 | parser.add_option("--status", default=False, action="store_true", |
| 90 | help="git status") |
| 91 | parser.add_option("--pull", nargs=2, default=False, |
| 92 | metavar = "remote branch", |
| 93 | help="git pull (both repos)") |
| 94 | parser.add_option("--push", nargs=3, default=False, |
| 95 | metavar = "remote branch comment", |
| 96 | help="git commit + push (both repos) [remote] [branch] [comment]") |
| 97 | parser.add_option("-l", "--latest", |
| 98 | action="store_true", dest="run_latest", default=False, |
| 99 | help="Apply the latest patches") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 100 | |
| 101 | # patch |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 102 | parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module', |
| 103 | action="append", |
| 104 | help="Apply patch") |
| 105 | parser.add_option("-f", "--force", |
| 106 | action="store_true", dest="force", default=False, |
| 107 | help="Force Apply all patches specified using option -p or --patch") |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 108 | parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname", |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 109 | help="reload doc") |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 110 | parser.add_option('--export_doc', nargs=2, metavar = "doctype docname", |
| 111 | help="export doc") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 112 | |
| 113 | # install |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 114 | parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source", |
| 115 | help="install fresh db") |
Anand Doshi | 12b6da2 | 2012-03-20 14:55:16 +0530 | [diff] [blame] | 116 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 117 | # diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 118 | parser.add_option('--diff_ref_file', nargs=0, \ |
| 119 | help="Get missing database records and mismatch properties, with file as reference") |
| 120 | parser.add_option('--diff_ref_db', nargs=0, \ |
| 121 | help="Get missing .txt files and mismatch properties, with database as reference") |
Anand Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 122 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 123 | # scheduler |
| 124 | parser.add_option('--run_scheduler', default=False, action="store_true", |
| 125 | help="Trigger scheduler") |
| 126 | parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]", |
| 127 | help="Run scheduler event") |
| 128 | |
| 129 | # misc |
| 130 | parser.add_option("--replace", nargs=3, default=False, |
| 131 | metavar = "search replace_by extension", |
| 132 | help="file search-replace") |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 133 | |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 134 | parser.add_option("--cci", nargs=1, metavar="CacheItem Key or all", |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 135 | help="Clear Cache Item") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 136 | |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 137 | parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files", |
| 138 | nargs=0) |
| 139 | |
| 140 | parser.add_option("--sync", help="Synchronize given DocType using txt file", |
| 141 | nargs=2, metavar="module doctype (use their folder names)") |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 142 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 143 | return parser.parse_args() |
| 144 | |
| 145 | def run(): |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 146 | sys.path.append('lib') |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 147 | sys.path.append('lib/py') |
| 148 | import webnotes |
Rushabh Mehta | cabfc65 | 2012-04-14 16:08:45 +0530 | [diff] [blame^] | 149 | import conf |
| 150 | sys.path.append(conf.modules_path) |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 151 | |
| 152 | (options, args) = setup_options() |
| 153 | |
| 154 | |
| 155 | from webnotes.db import Database |
| 156 | import webnotes.modules.patch_handler |
| 157 | |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 158 | # connect |
| 159 | if options.db_name is not None: |
| 160 | webnotes.connect(options.db_name) |
| 161 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 162 | # build |
| 163 | if options.build: |
| 164 | import build.project |
| 165 | build.project.build() |
| 166 | |
| 167 | elif options.clear: |
Rushabh Mehta | 03e8787 | 2012-04-14 15:02:17 +0530 | [diff] [blame] | 168 | from build.project import update_version |
| 169 | print "Version:" + str(update_version()) |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 170 | |
| 171 | # code replace |
| 172 | elif options.replace: |
| 173 | replace_code('.', options.replace[0], options.replace[1], options.replace[2]) |
| 174 | |
| 175 | # git |
| 176 | elif options.status: |
| 177 | os.system('git status') |
| 178 | os.chdir('lib') |
| 179 | os.system('git status') |
| 180 | |
| 181 | elif options.pull: |
Rushabh Mehta | b47e56f | 2012-04-14 15:03:49 +0530 | [diff] [blame] | 182 | from build.project import update_version |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 183 | os.system('git pull %s %s' % (options.pull[0], options.pull[1])) |
| 184 | os.chdir('lib') |
| 185 | os.system('git pull %s %s' % (options.pull[0], options.pull[1])) |
| 186 | |
Rushabh Mehta | b47e56f | 2012-04-14 15:03:49 +0530 | [diff] [blame] | 187 | # update js code version (clear to localStorage) |
| 188 | update_version() |
| 189 | |
| 190 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 191 | elif options.push: |
| 192 | os.system('git commit -a -m "%s"' % options.push[2]) |
| 193 | os.system('git push %s %s' % (options.push[0], options.push[1])) |
| 194 | os.chdir('lib') |
| 195 | os.system('git commit -a -m "%s"' % options.push[2]) |
| 196 | os.system('git push %s %s' % (options.push[0], options.push[1])) |
| 197 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 198 | # patch |
| 199 | elif options.patch_list: |
| 200 | # clear log |
| 201 | webnotes.modules.patch_handler.log_list = [] |
| 202 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 203 | # run individual patches |
| 204 | for patch in options.patch_list: |
| 205 | webnotes.modules.patch_handler.run_single(\ |
| 206 | patchmodule = patch, force = options.force) |
| 207 | |
| 208 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
| 209 | |
| 210 | # reload |
| 211 | elif options.reload_doc: |
| 212 | webnotes.modules.patch_handler.reload_doc(\ |
Rushabh Mehta | e651648 | 2012-02-06 16:28:06 +0530 | [diff] [blame] | 213 | {"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] | 214 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
| 215 | |
Rushabh Mehta | f9620ea | 2012-02-07 14:31:49 +0530 | [diff] [blame] | 216 | elif options.export_doc: |
| 217 | from webnotes.modules import export_doc |
| 218 | export_doc(options.export_doc[0], options.export_doc[1]) |
| 219 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 220 | # run all pending |
| 221 | elif options.run_latest: |
| 222 | webnotes.modules.patch_handler.run_all() |
| 223 | print '\n'.join(webnotes.modules.patch_handler.log_list) |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 224 | |
Rushabh Mehta | 0b2874a | 2012-02-09 12:45:50 +0530 | [diff] [blame] | 225 | elif options.install: |
| 226 | from webnotes.install_lib.install import Installer |
| 227 | inst = Installer('root', options.install[0]) |
| 228 | inst.import_from_db(options.install[1], source_path=options.install[2], \ |
Anand Doshi | bae6908 | 2012-02-09 13:34:12 +0530 | [diff] [blame] | 229 | password='admin', verbose = 1) |
| 230 | |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 231 | elif options.diff_ref_file is not None: |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 232 | import webnotes.modules.diff |
Rushabh Mehta | c952bc9 | 2012-02-22 17:30:07 +0530 | [diff] [blame] | 233 | webnotes.modules.diff.diff_ref_file() |
| 234 | |
| 235 | elif options.diff_ref_db is not None: |
| 236 | import webnotes.modules.diff |
| 237 | webnotes.modules.diff.diff_ref_db() |
Rushabh Mehta | 17773d0 | 2012-02-22 15:55:41 +0530 | [diff] [blame] | 238 | |
Rushabh Mehta | 1a35574 | 2012-02-23 11:46:28 +0530 | [diff] [blame] | 239 | elif options.run_scheduler: |
| 240 | import webnotes.utils.scheduler |
| 241 | print webnotes.utils.scheduler.execute() |
| 242 | |
| 243 | elif options.run_scheduler_event is not None: |
| 244 | import webnotes.utils.scheduler |
| 245 | print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event) |
| 246 | |
Anand Doshi | 7854f81 | 2012-03-14 12:01:13 +0530 | [diff] [blame] | 247 | elif options.cci is not None: |
| 248 | if options.cci=='all': |
| 249 | webnotes.conn.sql("DELETE FROM __CacheItem") |
| 250 | else: |
| 251 | from webnotes.utils.cache import CacheItem |
| 252 | CacheItem(options.cci).clear() |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 253 | |
| 254 | elif options.sync_all is not None: |
| 255 | import webnotes.model.sync |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 256 | webnotes.model.sync.sync_all(options.force or 0) |
Anand Doshi | 66ea305 | 2012-03-27 15:02:30 +0530 | [diff] [blame] | 257 | |
| 258 | elif options.sync is not None: |
| 259 | import webnotes.model.sync |
Anand Doshi | 82042f1 | 2012-04-06 17:54:17 +0530 | [diff] [blame] | 260 | 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] | 261 | |
Rushabh Mehta | cc54fd4 | 2012-02-06 13:09:08 +0100 | [diff] [blame] | 262 | # print messages |
| 263 | if webnotes.message_log: |
| 264 | print '\n'.join(webnotes.message_log) |
| 265 | |
Rushabh Mehta | e219db0 | 2012-02-06 15:33:08 +0530 | [diff] [blame] | 266 | if __name__=='__main__': |
| 267 | run() |