borgend.py

Mon, 22 Jan 2018 02:37:46 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 22 Jan 2018 02:37:46 +0000
changeset 47
305de2297fde
parent 46
ecb41072a1b7
child 49
db33dfa64ad6
permissions
-rwxr-xr-x

README configuration file location update

#!/usr/local/bin/python3

import os
import sys
import logging
import logging.handlers
import argparse
import platform
import utils
from fifolog import FIFOHandler

#
# Branding
#
appname="borgend"
appname_stylised="Borgend"

#
# Logging configuration
#

loglevel=logging.DEBUG
logfmt="%(asctime)s:%(levelname)s:%(name)s:%(message)s"
fifolog_capacity=1000
fifolog_fmt="%(asctime)s:%(levelname)s:%(message)s"

#
# Setup logger, needed by the config module to be loaded next
#

logger=logging.getLogger(appname)
logger.setLevel(loglevel)
logger.propagate=True

#
# Argument processing
#

import config

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

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

    return parser.parse_args()

#
# Main routine
#

# First, setup our own logging handlers
fifolog=FIFOHandler(fifolog_capacity)
logger.addHandler(fifolog)
fifolog.setFormatter(logging.Formatter(fifolog_fmt))

if __name__=="__main__":
    # Parse args. Let argparse handle errors/exit if there are any
    args= do_args()
    tray = None

    try:
        args=do_args()

        if not os.path.isdir(config.logs_dir):
            os.makedirs(config.logs_dir)

        handler=logging.handlers.TimedRotatingFileHandler(
            os.path.join(config.logs_dir, appname+'.log'),
            when='D', interval=1)
        handler.setFormatter(logging.Formatter(logfmt))
        logger.addHandler(handler)

        from threading import Thread
        from backup import Backup
        from queue import Queue
        import signal, os

        backupconfigs=config.settings['backups']
        backups=[None]*len(backupconfigs);

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

            if args.notray or platform.system()!='Darwin':
                pass
            else:
                from ui import BorgendTray
                tray=BorgendTray(backups);
                tray.run()
        finally:
            for i in range(len(backups)):
                if backups[i]:
                    backups[i].abort()
            backups=[]
    except Exception as err:
        utils.log_exception(logger, err, detail='Exiting')
        if tray:
            tray.quit()
        else:
            logging.shutdown()
            #sys.exit()

    #
    # This shit is fucked, disables ^C etc., and threading doesn't seem to help
    #

    # ui_thread=Thread(target=tray.run)
    # ui_thread.daemon=True
    # ui_thread.start()

    # def quit_signal_handler(signum, frame):
    #     print('Signal handler called with signal %s' % str(signum))
    #     ui_thread.terminate()
    #     os.exit()

    # signal.signal(signal.SIGTERM, quit_signal_handler)
    # signal.signal(signal.SIGINT, quit_signal_handler)

mercurial