forked from frappe/erpnext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwnf.py
More file actions
executable file
·227 lines (184 loc) · 7.19 KB
/
wnf.py
File metadata and controls
executable file
·227 lines (184 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# ERPNext - web based ERP (http://erpnext.com)
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, sys
def replace_code(start, txt1, txt2, extn):
"""replace all txt1 by txt2 in files with extension (extn)"""
import os, re
for wt in os.walk(start, followlinks=1):
for fn in wt[2]:
if fn.split('.')[-1]==extn:
fpath = os.path.join(wt[0], fn)
with open(fpath, 'r') as f:
content = f.read()
if re.search(txt1, content):
a = raw_input('Change in %s [y/n]?' % fpath)
if a=='y':
with open(fpath, 'w') as f:
f.write(re.sub(txt1, txt2, content))
print 'updated in %s' % fpath
def setup_options():
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-d", "--db",
dest="db_name",
help="Apply the patches on given db")
# build
parser.add_option("-b", "--build", default=False, action="store_true",
help="minify + concat js files")
parser.add_option("-c", "--clear", default=False, action="store_true",
help="increment version")
# git
parser.add_option("--status", default=False, action="store_true",
help="git status")
parser.add_option("--pull", nargs=2, default=False,
metavar = "remote branch",
help="git pull (both repos)")
parser.add_option("--push", nargs=3, default=False,
metavar = "remote branch comment",
help="git commit + push (both repos) [remote] [branch] [comment]")
parser.add_option("-l", "--latest",
action="store_true", dest="run_latest", default=False,
help="Apply the latest patches")
# patch
parser.add_option("-p", "--patch", nargs=1, dest="patch_list", metavar='patch_module',
action="append",
help="Apply patch")
parser.add_option("-f", "--force",
action="store_true", dest="force", default=False,
help="Force Apply all patches specified using option -p or --patch")
parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
help="reload doc")
parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
help="export doc")
# install
parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
help="install fresh db")
parser.add_option('--sync_with_gateway', nargs=1, metavar = "1/0", \
help="Set or Unset Sync with Gateway")
# diff
parser.add_option('--diff_ref_file', nargs=0, \
help="Get missing database records and mismatch properties, with file as reference")
parser.add_option('--diff_ref_db', nargs=0, \
help="Get missing .txt files and mismatch properties, with database as reference")
# scheduler
parser.add_option('--run_scheduler', default=False, action="store_true",
help="Trigger scheduler")
parser.add_option('--run_scheduler_event', nargs=1, metavar="[all|daily|weekly|monthly]",
help="Run scheduler event")
# misc
parser.add_option("--replace", nargs=3, default=False,
metavar = "search replace_by extension",
help="file search-replace")
parser.add_option("--cci", nargs=1, metavar="CacheItem Key",
help="Clear Cache Item")
return parser.parse_args()
def run():
sys.path.append('lib')
sys.path.append('lib/py')
import webnotes
import webnotes.defs
sys.path.append(webnotes.defs.modules_path)
(options, args) = setup_options()
from webnotes.db import Database
import webnotes.modules.patch_handler
# connect
if options.db_name is not None:
webnotes.connect(options.db_name)
# build
if options.build:
import build.project
build.project.build()
elif options.clear:
from build.project import increment_version
print "Version:" + str(increment_version())
# code replace
elif options.replace:
replace_code('.', options.replace[0], options.replace[1], options.replace[2])
# git
elif options.status:
os.system('git status')
os.chdir('lib')
os.system('git status')
elif options.pull:
os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
os.chdir('lib')
os.system('git pull %s %s' % (options.pull[0], options.pull[1]))
elif options.push:
os.system('git commit -a -m "%s"' % options.push[2])
os.system('git push %s %s' % (options.push[0], options.push[1]))
os.chdir('lib')
os.system('git commit -a -m "%s"' % options.push[2])
os.system('git push %s %s' % (options.push[0], options.push[1]))
# patch
elif options.patch_list:
# clear log
webnotes.modules.patch_handler.log_list = []
# run individual patches
for patch in options.patch_list:
webnotes.modules.patch_handler.run_single(\
patchmodule = patch, force = options.force)
print '\n'.join(webnotes.modules.patch_handler.log_list)
# reload
elif options.reload_doc:
webnotes.modules.patch_handler.reload_doc(\
{"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
print '\n'.join(webnotes.modules.patch_handler.log_list)
elif options.export_doc:
from webnotes.modules import export_doc
export_doc(options.export_doc[0], options.export_doc[1])
# run all pending
elif options.run_latest:
webnotes.modules.patch_handler.run_all()
print '\n'.join(webnotes.modules.patch_handler.log_list)
elif options.install:
from webnotes.install_lib.install import Installer
inst = Installer('root', options.install[0])
inst.import_from_db(options.install[1], source_path=options.install[2], \
password='admin', verbose = 1)
elif options.sync_with_gateway:
if int(options.sync_with_gateway[0]) in [0, 1]:
webnotes.conn.begin()
webnotes.conn.sql("""\
UPDATE `tabSingles` SET value=%s
WHERE field='sync_with_gateway' AND doctype='Control Panel'""", int(options.sync_with_gateway[0]))
webnotes.conn.commit()
webnotes.message_log.append("sync_with_gateway set to %s" % options.sync_with_gateway[0])
else:
webnotes.message_log.append("ERROR: sync_with_gateway can be either 0 or 1")
elif options.diff_ref_file is not None:
import webnotes.modules.diff
webnotes.modules.diff.diff_ref_file()
elif options.diff_ref_db is not None:
import webnotes.modules.diff
webnotes.modules.diff.diff_ref_db()
elif options.run_scheduler:
import webnotes.utils.scheduler
print webnotes.utils.scheduler.execute()
elif options.run_scheduler_event is not None:
import webnotes.utils.scheduler
print webnotes.utils.scheduler.trigger('execute_' + options.run_scheduler_event)
elif options.cci is not None:
if options.cci=='all':
webnotes.conn.sql("DELETE FROM __CacheItem")
else:
from webnotes.utils.cache import CacheItem
CacheItem(options.cci).clear()
# print messages
if webnotes.message_log:
print '\n'.join(webnotes.message_log)
if __name__=='__main__':
run()