brotherton-erpnext/wnf.py

254 lines
7.7 KiB
Python
Raw Normal View History

#!/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/>.
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)
2012-03-16 10:52:30 +00:00
if fpath != '/var/www/erpnext/erpnext/patches/jan_mar_2012/rename_dt.py': # temporary
with open(fpath, 'r') as f:
content = f.read()
2012-03-16 10:52:30 +00:00
if re.search(txt1, content):
2012-04-02 13:42:29 +00:00
res = search_replace_with_prompt(fpath, txt1, txt2)
if res == 'skip':
return 'skip'
def search_replace_with_prompt(fpath, txt1, txt2):
""" Search and replace all txt1 by txt2 in the file with confirmation"""
from termcolor import colored
with open(fpath, 'r') as f:
content = f.readlines()
tmp = []
for c in content:
if c.find(txt1) != -1:
print '\n', fpath
print colored(txt1, 'red').join(c[:-1].split(txt1))
2012-03-16 10:52:30 +00:00
a = ''
2012-04-02 13:42:29 +00:00
while a.lower() not in ['y', 'n', 'skip']:
a = raw_input('Do you want to Change [y/n/skip]?')
2012-03-16 10:52:30 +00:00
if a.lower() == 'y':
c = c.replace(txt1, txt2)
2012-04-02 13:42:29 +00:00
elif a.lower() == 'skip':
return 'skip'
2012-03-14 06:34:16 +00:00
tmp.append(c)
with open(fpath, 'w') as f:
f.write(''.join(tmp))
2012-03-14 06:34:16 +00:00
print colored('Updated', 'green')
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
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
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
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",
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-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-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-27 09:32:30 +00:00
parser.add_option("--cci", nargs=1, metavar="CacheItem Key or all",
help="Clear Cache Item")
2012-02-23 06:16:28 +00:00
2012-03-27 09:32:30 +00:00
parser.add_option("--sync_all", help="Synchronize all DocTypes using txt files",
nargs=0)
parser.add_option("--sync", help="Synchronize given DocType using txt file",
nargs=2, metavar="module doctype (use their folder names)")
2012-02-23 06:16:28 +00:00
return parser.parse_args()
def run():
2012-02-06 10:58:06 +00:00
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
2012-02-06 12:09:08 +00:00
# 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(\
2012-02-06 10:58:06 +00:00
{"module":options.reload_doc[0], "dt":options.reload_doc[1], "dn":options.reload_doc[2]})
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])
# 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], \
password='admin', verbose = 1)
2012-02-22 12:00:07 +00:00
elif options.diff_ref_file is not None:
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-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)
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-03-27 09:32:30 +00:00
elif options.sync_all is not None:
import webnotes.model.sync
webnotes.model.sync.sync_all()
elif options.sync is not None:
import webnotes.model.sync
webnotes.model.sync.sync(options.sync[0], options.sync[1])
2012-02-06 12:09:08 +00:00
# print messages
if webnotes.message_log:
print '\n'.join(webnotes.message_log)
if __name__=='__main__':
run()