2
0
mirror of https://github.com/frappe/bench.git synced 2024-09-27 22:39:03 +00:00

Add logging, #4

This commit is contained in:
Pratik Vyas 2014-07-11 15:03:07 +05:30
parent a89963bc09
commit 7a5ace5dcb
3 changed files with 34 additions and 3 deletions

View File

@ -1,6 +1,9 @@
import os
from .utils import exec_cmd, get_frappe
import logging
logger = logging.getLogger(__name__)
def get_apps(bench='.'):
try:
@ -17,14 +20,17 @@ def add_to_appstxt(app, bench='.'):
return f.write('\n'.join(apps))
def get_app(app, git_url, bench='.'):
logger.info('getting app {}'.format(app))
exec_cmd("git clone {} --origin upstream {}".format(git_url, app), cwd=os.path.join(bench, 'apps'))
install_app(app, bench=bench)
def new_app(app, bench='.'):
logger.info('creating new app {}'.format(app))
exec_cmd("{frappe} --make_app {apps}".format(frappe=get_frappe(bench=bench), apps=os.path.join(bench, 'apps')))
install_app(app, bench=bench)
def install_app(app, bench='.'):
logger.info('installing {}'.format(app))
exec_cmd("{pip} install -e {app}".format(pip=os.path.join(bench, 'env', 'bin', 'pip'), app=os.path.join(bench, 'apps', app)))
add_to_appstxt(app, bench=bench)
@ -34,4 +40,5 @@ def pull_all_apps(bench='.'):
for app in apps:
app_dir = os.path.join(apps_dir, app)
if os.path.exists(os.path.join(app_dir, '.git')):
logger.info('pulling {}'.format(app))
exec_cmd("git pull --rebase upstream HEAD", cwd=app_dir)

View File

@ -5,13 +5,16 @@ from .utils import new_site as _new_site
from .utils import setup_backups as _setup_backups
from .utils import setup_auto_update as _setup_auto_update
from .utils import setup_sudoers as _setup_sudoers
from .utils import build_assets, patch_sites, exec_cmd, update_bench, get_frappe
from .utils import build_assets, patch_sites, exec_cmd, update_bench, get_frappe, setup_logging
from .app import get_app as _get_app
from .app import new_app as _new_app
from .app import pull_all_apps
from .config import generate_config
import os
import sys
import logging
logger = logging.getLogger('bench')
def cli():
if sys.argv[1] == "frappe":
@ -24,8 +27,9 @@ def frappe(bench='.'):
os.execv(f, [f] + sys.argv[2:])
@click.group()
def bench():
pass
def bench(bench='.'):
# TODO add bench path context
setup_logging(bench=bench)
@click.command()
@click.argument('path')

View File

@ -2,6 +2,9 @@ import os
import sys
import subprocess
import getpass
import logging
logger = logging.getLogger(__name__)
def get_frappe(bench='.'):
frappe = os.path.abspath(os.path.join(bench, 'env', 'bin', 'frappe'))
@ -19,6 +22,9 @@ def init(path):
os.mkdir(path)
for dirname in ('apps', 'sites', 'config', 'logs'):
os.mkdir(os.path.join(path, dirname))
setup_logging()
setup_env(bench=path)
get_app('frappe', 'https://github.com/frappe/frappe.git', bench=path)
setup_backups(bench=path)
@ -35,6 +41,7 @@ def setup_env(bench='.'):
exec_cmd('virtualenv {} -p {}'.format('env', sys.executable), cwd=bench)
def new_site(site, bench='.'):
logger.info('creating new site {}'.format(site))
exec_cmd("{frappe} --install {site} {site}".format(frappe=get_frappe(bench=bench), site=site), cwd=os.path.join(bench, 'sites'))
if len(get_sites(bench=bench)) == 1:
exec_cmd("{frappe} --use {site}".format(frappe=get_frappe(bench=bench), site=site), cwd=os.path.join(bench, 'sites'))
@ -58,14 +65,17 @@ def get_bench_dir(bench='.'):
return os.path.abspath(bench)
def setup_auto_update(bench='.'):
logger.info('setting up auto update')
exec_cmd('echo \"`crontab -l`\" | uniq | sed -e \"a0 10 * * * cd {bench_dir} && {bench} update\" | grep -v "^$" | uniq | crontab'.format(bench_dir=get_bench_dir(bench=bench),
bench=os.path.join(get_bench_dir(bench=bench), 'env', 'bin', 'bench')))
def setup_backups(bench='.'):
logger.info('setting up backups')
exec_cmd('echo \"`crontab -l`\" | uniq | sed -e \"a0 */6 * * * cd {sites_dir} && {frappe} --backup all\" | grep -v "^$" | uniq | crontab'.format(sites_dir=get_sites_dir(bench=bench),
frappe=get_frappe(bench=bench)))
def update_bench():
logger.info('setting up sudoers')
cwd = os.path.dirname(os.path.abspath(__file__))
exec_cmd("git pull", cwd=cwd)
@ -74,3 +84,13 @@ def setup_sudoers():
f.write("{user} ALL=(ALL) NOPASSWD: {supervisorctl} restart frappe\:\n".format(
user=getpass.getuser()),
supervisorctl=subprocess.check_output('which supervisorctl', shell=True).strip())
def setup_logging(bench='.'):
if os.path.exists(os.path.join(bench, 'logs')):
logger = logging.getLogger('bench')
log_file = os.path.join(bench, 'logs', 'bench.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr = logging.FileHandler(log_file)
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)