diff --git a/bench/app.py b/bench/app.py index a742a284..f675f3a3 100644 --- a/bench/app.py +++ b/bench/app.py @@ -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) diff --git a/bench/cli.py b/bench/cli.py index 8da1924d..f43e33bb 100644 --- a/bench/cli.py +++ b/bench/cli.py @@ -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') diff --git a/bench/utils.py b/bench/utils.py index 1ae47737..0f48d6c0 100644 --- a/bench/utils.py +++ b/bench/utils.py @@ -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)