2012-02-06 10:03:08 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2012-02-23 07:05:32 +00:00
|
|
|
# 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/>.
|
|
|
|
|
2012-02-06 10:03:08 +00:00
|
|
|
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()
|
2012-02-23 06:16:28 +00:00
|
|
|
|
|
|
|
parser.add_option("-d", "--db",
|
|
|
|
dest="db_name",
|
|
|
|
help="Apply the patches on given db")
|
|
|
|
|
|
|
|
# build
|
2012-02-06 10:03:08 +00:00
|
|
|
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")
|
2012-02-23 06:16:28 +00:00
|
|
|
|
|
|
|
# git
|
2012-02-06 10:03:08 +00:00
|
|
|
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")
|
2012-02-23 06:16:28 +00:00
|
|
|
|
|
|
|
# patch
|
2012-02-06 10:03:08 +00:00
|
|
|
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")
|
2012-02-06 10:58:06 +00:00
|
|
|
parser.add_option('--reload_doc', nargs=3, metavar = "module doctype docname",
|
2012-02-06 10:03:08 +00:00
|
|
|
help="reload doc")
|
2012-02-07 09:01:49 +00:00
|
|
|
parser.add_option('--export_doc', nargs=2, metavar = "doctype docname",
|
|
|
|
help="export doc")
|
2012-02-23 06:16:28 +00:00
|
|
|
|
|
|
|
# install
|
2012-02-09 07:15:50 +00:00
|
|
|
parser.add_option('--install', nargs=3, metavar = "rootpassword dbname source",
|
|
|
|
help="install fresh db")
|
2012-02-22 10:25:41 +00:00
|
|
|
parser.add_option('--sync_with_gateway', nargs=1, metavar = "1/0", \
|
|
|
|
help="Set or Unset Sync with Gateway")
|
2012-02-23 06:16:28 +00:00
|
|
|
|
|
|
|
# diff
|
2012-02-22 12:00:07 +00:00
|
|
|
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")
|
2012-02-09 08:04:12 +00:00
|
|
|
|
2012-02-23 06:16:28 +00:00
|
|
|
# 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")
|
2012-03-14 06:31:13 +00:00
|
|
|
|
|
|
|
parser.add_option("--cci", nargs=1, metavar="CacheItem Key",
|
|
|
|
help="Clear Cache Item")
|
2012-02-23 06:16:28 +00:00
|
|
|
|
|
|
|
|
2012-02-06 10:03:08 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
def run():
|
2012-02-06 10:58:06 +00:00
|
|
|
sys.path.append('lib')
|
2012-02-06 10:03:08 +00:00
|
|
|
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
|
|
|
|
|
2012-02-06 12:09:08 +00:00
|
|
|
# connect
|
|
|
|
if options.db_name is not None:
|
|
|
|
webnotes.connect(options.db_name)
|
|
|
|
|
2012-02-06 10:03:08 +00:00
|
|
|
# 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(\
|
2012-02-06 10:58:06 +00:00
|
|
|
{"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
|
2012-02-06 10:03:08 +00:00
|
|
|
print '\n'.join(webnotes.modules.patch_handler.log_list)
|
|
|
|
|
2012-02-07 09:01:49 +00:00
|
|
|
elif options.export_doc:
|
|
|
|
from webnotes.modules import export_doc
|
|
|
|
export_doc(options.export_doc[0], options.export_doc[1])
|
|
|
|
|
2012-02-06 10:03:08 +00:00
|
|
|
# run all pending
|
|
|
|
elif options.run_latest:
|
|
|
|
webnotes.modules.patch_handler.run_all()
|
|
|
|
print '\n'.join(webnotes.modules.patch_handler.log_list)
|
2012-02-06 12:09:08 +00:00
|
|
|
|
2012-02-09 07:15:50 +00:00
|
|
|
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], \
|
2012-02-09 08:04:12 +00:00
|
|
|
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")
|
2012-02-09 07:15:50 +00:00
|
|
|
|
2012-02-22 12:00:07 +00:00
|
|
|
elif options.diff_ref_file is not None:
|
2012-02-22 10:25:41 +00:00
|
|
|
import webnotes.modules.diff
|
2012-02-22 12:00:07 +00:00
|
|
|
webnotes.modules.diff.diff_ref_file()
|
|
|
|
|
|
|
|
elif options.diff_ref_db is not None:
|
|
|
|
import webnotes.modules.diff
|
|
|
|
webnotes.modules.diff.diff_ref_db()
|
2012-02-22 10:25:41 +00:00
|
|
|
|
2012-02-23 06:16:28 +00:00
|
|
|
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)
|
|
|
|
|
2012-03-14 06:31:13 +00:00
|
|
|
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()
|
|
|
|
|
2012-02-06 12:09:08 +00:00
|
|
|
# print messages
|
|
|
|
if webnotes.message_log:
|
|
|
|
print '\n'.join(webnotes.message_log)
|
|
|
|
|
2012-02-06 10:03:08 +00:00
|
|
|
if __name__=='__main__':
|
|
|
|
run()
|