borgend.py

Sun, 28 Jan 2018 11:54:46 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Sun, 28 Jan 2018 11:54:46 +0000
changeset 80
a409242121d5
parent 79
b075b3db3044
child 82
4e7678dd7b42
permissions
-rwxr-xr-x

Better package-like organisation

#!/usr/local/bin/python3

# Common modules
import os
import sys
import argparse
import platform
import logging
# Own modules needed at this stage
import borgend.branding as branding
import borgend.locations as locations

#
# Argument processing
#

parser=argparse.ArgumentParser(
    description=branding.appname_stylised + ": BorgBackup scheduler, queue, and tray icon.",
    epilog='Configuration file location:\n\n    %s\n ' % locations.cfgfile,
    formatter_class=argparse.RawDescriptionHelpFormatter)

parser.add_argument(
    '--no-tray',
    dest='notray',
    action='store_true',
    help='Do not show the tray icon')

parser.add_argument(
    '--debug',
    dest='debug',
    action='store_true',
    help='Set logging level to debug')

args=parser.parse_args()

#
# Done parsing args, import our own modules, and launch everything
#

import borgend.config as config
import borgend.dreamtime as dreamtime
import borgend.loggers as loggers
from borgend.scheduler import Scheduler
from borgend.repository import Repository
from borgend.backup import Backup

logger=loggers.mainlogger

if args.debug:
    logger.setLevel(logging.DEBUG)

tray = None
repos=[]
backups=[]

try:
    dreamtime.start_monitoring()

    scheduler = Scheduler()
    scheduler.start()

    repoconfigs=config.settings['repositories']

    for i in range(len(repoconfigs)):
        logger.info('Setting up repository %d' % i)
        r=Repository(i, repoconfigs[i])
        repos.append(r)

    backupconfigs=config.settings['backups']

    for i in range(len(backupconfigs)):
        logger.info('Setting up backup set %d' % i)
        b=Backup(i, backupconfigs[i], scheduler)
        backups.append(b)

    for r in repos:
        r.start()

    for b in backups:
        b.start()

    if args.notray or platform.system()!='Darwin':
        # Wait for scheduler to finish
        scheduler.join()
    else:
        # Start UI, and let it handle exit control
        from borgend.ui import BorgendTray
        tray=BorgendTray(backups);
        tray.run()

except Exception as err:
    # TODO: Should write errors here to stderr;
    # perhaps add an extra stderr logger for error level messages
    logger.exception("Exception fell through: exiting")

finally:
    for b in backups:
        b.terminate()

    for r in repos:
        r.terminate()

    if tray:
        tray.quit()
    else:
        logging.shutdown()

mercurial