blob: 4f18187fccb9c6e281bf0bae7f1447c2b53643d8 [file] [log] [blame]
Rushabh Mehtae219db02012-02-06 15:33:08 +05301#!/usr/bin/env python
2
3import os, sys
4
5def replace_code(start, txt1, txt2, extn):
6 """replace all txt1 by txt2 in files with extension (extn)"""
7 import os, re
8 for wt in os.walk(start, followlinks=1):
9 for fn in wt[2]:
10 if fn.split('.')[-1]==extn:
11 fpath = os.path.join(wt[0], fn)
12 with open(fpath, 'r') as f:
13 content = f.read()
14
15 if re.search(txt1, content):
16 a = raw_input('Change in %s [y/n]?' % fpath)
17 if a=='y':
18 with open(fpath, 'w') as f:
19 f.write(re.sub(txt1, txt2, content))
20
21 print 'updated in %s' % fpath
22
23def setup_options():
24 from optparse import OptionParser
25 parser = OptionParser()
26 parser.add_option("-b", "--build", default=False, action="store_true",
27 help="minify + concat js files")
28 parser.add_option("-c", "--clear", default=False, action="store_true",
29 help="increment version")
30 parser.add_option("--replace", nargs=3, default=False,
31 metavar = "search replace_by extension",
32 help="file search-replace")
33 parser.add_option("--status", default=False, action="store_true",
34 help="git status")
35 parser.add_option("--pull", nargs=2, default=False,
36 metavar = "remote branch",
37 help="git pull (both repos)")
38 parser.add_option("--push", nargs=3, default=False,
39 metavar = "remote branch comment",
40 help="git commit + push (both repos) [remote] [branch] [comment]")
41 parser.add_option("-l", "--latest",
42 action="store_true", dest="run_latest", default=False,
43 help="Apply the latest patches")
44 parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
45 action="append",
46 help="Apply patch")
47 parser.add_option("-f", "--force",
48 action="store_true", dest="force", default=False,
49 help="Force Apply all patches specified using option -p or --patch")
50 parser.add_option("-d", "--db",
51 dest="db_name",
52 help="Apply the patches on given db")
Rushabh Mehtae6516482012-02-06 16:28:06 +053053 parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
Rushabh Mehtae219db02012-02-06 15:33:08 +053054 help="reload doc")
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +053055 parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
56 help="export doc")
Rushabh Mehta0b2874a2012-02-09 12:45:50 +053057 parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
58 help="install fresh db")
Anand Doshibae69082012-02-09 13:34:12 +053059 parser.add_option('--sync_with_gateway', nargs=1, metavar = "1/0", help="Set or Unset Sync with Gateway")
60
Rushabh Mehtae219db02012-02-06 15:33:08 +053061 return parser.parse_args()
62
63def run():
Rushabh Mehtae6516482012-02-06 16:28:06 +053064 sys.path.append('lib')
Rushabh Mehtae219db02012-02-06 15:33:08 +053065 sys.path.append('lib/py')
66 import webnotes
67 import webnotes.defs
68 sys.path.append(webnotes.defs.modules_path)
69
70 (options, args) = setup_options()
71
72
73 from webnotes.db import Database
74 import webnotes.modules.patch_handler
75
Rushabh Mehtacc54fd42012-02-06 13:09:08 +010076 # connect
77 if options.db_name is not None:
78 webnotes.connect(options.db_name)
79
Rushabh Mehtae219db02012-02-06 15:33:08 +053080 # build
81 if options.build:
82 import build.project
83 build.project.build()
84
85 elif options.clear:
86 from build.project import increment_version
87 print "Version:" + str(increment_version())
88
89 # code replace
90 elif options.replace:
91 replace_code('.', options.replace[0], options.replace[1], options.replace[2])
92
93 # git
94 elif options.status:
95 os.system('git status')
96 os.chdir('lib')
97 os.system('git status')
98
99 elif options.pull:
100 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
101 os.chdir('lib')
102 os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
103
104 elif options.push:
105 os.system('git commit -a -m "%s"' % options.push[2])
106 os.system('git push %s %s' % (options.push[0], options.push[1]))
107 os.chdir('lib')
108 os.system('git commit -a -m "%s"' % options.push[2])
109 os.system('git push %s %s' % (options.push[0], options.push[1]))
110
Rushabh Mehtae219db02012-02-06 15:33:08 +0530111 # patch
112 elif options.patch_list:
113 # clear log
114 webnotes.modules.patch_handler.log_list = []
115
Rushabh Mehtae219db02012-02-06 15:33:08 +0530116 # run individual patches
117 for patch in options.patch_list:
118 webnotes.modules.patch_handler.run_single(\
119 patchmodule = patch, force = options.force)
120
121 print '\n'.join(webnotes.modules.patch_handler.log_list)
122
123 # reload
124 elif options.reload_doc:
125 webnotes.modules.patch_handler.reload_doc(\
Rushabh Mehtae6516482012-02-06 16:28:06 +0530126 {"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
Rushabh Mehtae219db02012-02-06 15:33:08 +0530127 print '\n'.join(webnotes.modules.patch_handler.log_list)
128
Rushabh Mehtaf9620ea2012-02-07 14:31:49 +0530129 elif options.export_doc:
130 from webnotes.modules import export_doc
131 export_doc(options.export_doc[0], options.export_doc[1])
132
Rushabh Mehtae219db02012-02-06 15:33:08 +0530133 # run all pending
134 elif options.run_latest:
135 webnotes.modules.patch_handler.run_all()
136 print '\n'.join(webnotes.modules.patch_handler.log_list)
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100137
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530138 elif options.install:
139 from webnotes.install_lib.install import Installer
140 inst = Installer('root', options.install[0])
141 inst.import_from_db(options.install[1], source_path=options.install[2], \
Anand Doshibae69082012-02-09 13:34:12 +0530142 password='admin', verbose = 1)
143
144 elif options.sync_with_gateway:
145 if int(options.sync_with_gateway[0]) in [0, 1]:
146 webnotes.conn.begin()
147 webnotes.conn.sql("""\
148 UPDATE `tabSingles` SET value=%s
149 WHERE field='sync_with_gateway' AND doctype='Control Panel'""", int(options.sync_with_gateway[0]))
150 webnotes.conn.commit()
151 webnotes.message_log.append("sync_with_gateway set to %s" % options.sync_with_gateway[0])
152 else:
153 webnotes.message_log.append("ERROR: sync_with_gateway can be either 0 or 1")
Rushabh Mehta0b2874a2012-02-09 12:45:50 +0530154
Rushabh Mehtacc54fd42012-02-06 13:09:08 +0100155 # print messages
156 if webnotes.message_log:
157 print '\n'.join(webnotes.message_log)
158
Rushabh Mehtae219db02012-02-06 15:33:08 +0530159if __name__=='__main__':
160 run()