From 01e7057e94c6f502b6d283453315e5a718786d04 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 18 Jan 2012 19:03:25 +0100 Subject: Some random fixes --- alias_server/scripts/alias-server | 97 +++++++++++++++++++++++++++++++++++++++ alias_server/scripts/server | 97 --------------------------------------- 2 files changed, 97 insertions(+), 97 deletions(-) create mode 100644 alias_server/scripts/alias-server delete mode 100644 alias_server/scripts/server (limited to 'alias_server') diff --git a/alias_server/scripts/alias-server b/alias_server/scripts/alias-server new file mode 100644 index 0000000..554388f --- /dev/null +++ b/alias_server/scripts/alias-server @@ -0,0 +1,97 @@ +#!/usr/bin/python2 +import logging +from argparse import ArgumentParser +from alias_server.config import config +import daemon +import daemon.pidfile +from alias_server.component import ObjectComponent +import os.path +import sys + +if sys.version_info < (3, 0): + reload(sys) + sys.setdefaultencoding('utf8') +else: + raw_input = input + +if __name__ == '__main__': + commandline = ArgumentParser(description = 'Connect the alias \ + component to a given server') + commandline.add_argument('-p', '--port', + help = 'Port to connect to', + type = int) + commandline.add_argument('-s', '--secret', + help = 'password') + commandline.add_argument('-n', '--name', + help = 'Name the component will have') + commandline.add_argument('-r', '--root', + help = 'Root directory of the user files') + commandline.add_argument('-c', '--config', + help = 'Name of the config file to use') + commandline.add_argument('-d', '--debug', + help = 'Set log level to DEBUG', + action = 'store_const', + const = logging.DEBUG, + default = logging.INFO) + commandline.add_argument('-o', '--host', + help = 'Host to connect to') + commandline.add_argument('-b', '--background', + help = 'run the server in the background', + action = 'store_true') + commandline.add_argument('--logfile', + help = 'location of the log file (default /var/log/${name}.log') + commandline.add_argument('--pidfile', + help = 'location of the pid file (default /var/run/${name}.pid') + args = commandline.parse_args() + + if args.config is None: + config.name = args.name + config.port = args.port + config.secret = args.secret + config.root = args.root + config.host = args.host + config.background = args.background + config.logfile = args.logfile + config.pidfile = args.pidfile + else: + filename = args.config + config.read(filename) + if config.logfile is None: + config.logfile = os.path.join('/var/log/', config.name + '.log') + if config.pidfile is None: + config.pidfile = os.path.join('/var/run/', config.name + '.pid') + + #set up the root logger + logging.getLogger('').setLevel(args.debug) + formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + if config.background: + #save logs in a file + fh = logging.FileHandler(config.logfile) + fh.setFormatter(formatter) + logging.getLogger('').addHandler(fh) + else: + #save logs to the console + ch = logging.StreamHandler() + ch.setFormatter(formatter) + logging.getLogger('').addHandler(ch) + + if config.background: + context = daemon.DaemonContext(detach_process = True, + pidfile = daemon.pidfile.TimeoutPIDLockFile(config.pidfile,10), + files_preserve=[fh.stream.fileno()], + working_directory=os.path.abspath(config.root), + stdout=sys.stdout, stderr=sys.stderr) + else: + context = daemon.DaemonContext(detach_process = False, + stdout=sys.stdout, stderr=sys.stderr, + working_directory=os.path.abspath(config.root)) + + with context: + component = ObjectComponent(config.name, config.secret, + config.host, config.port, + config.root) + if component.connect(): + logging.info('Component {} connected'.format(component.boundjid)) + component.process(block=False) + else: + logging.error("Component {} couldn't connect".format(component.boundjid)) diff --git a/alias_server/scripts/server b/alias_server/scripts/server deleted file mode 100644 index 554388f..0000000 --- a/alias_server/scripts/server +++ /dev/null @@ -1,97 +0,0 @@ -#!/usr/bin/python2 -import logging -from argparse import ArgumentParser -from alias_server.config import config -import daemon -import daemon.pidfile -from alias_server.component import ObjectComponent -import os.path -import sys - -if sys.version_info < (3, 0): - reload(sys) - sys.setdefaultencoding('utf8') -else: - raw_input = input - -if __name__ == '__main__': - commandline = ArgumentParser(description = 'Connect the alias \ - component to a given server') - commandline.add_argument('-p', '--port', - help = 'Port to connect to', - type = int) - commandline.add_argument('-s', '--secret', - help = 'password') - commandline.add_argument('-n', '--name', - help = 'Name the component will have') - commandline.add_argument('-r', '--root', - help = 'Root directory of the user files') - commandline.add_argument('-c', '--config', - help = 'Name of the config file to use') - commandline.add_argument('-d', '--debug', - help = 'Set log level to DEBUG', - action = 'store_const', - const = logging.DEBUG, - default = logging.INFO) - commandline.add_argument('-o', '--host', - help = 'Host to connect to') - commandline.add_argument('-b', '--background', - help = 'run the server in the background', - action = 'store_true') - commandline.add_argument('--logfile', - help = 'location of the log file (default /var/log/${name}.log') - commandline.add_argument('--pidfile', - help = 'location of the pid file (default /var/run/${name}.pid') - args = commandline.parse_args() - - if args.config is None: - config.name = args.name - config.port = args.port - config.secret = args.secret - config.root = args.root - config.host = args.host - config.background = args.background - config.logfile = args.logfile - config.pidfile = args.pidfile - else: - filename = args.config - config.read(filename) - if config.logfile is None: - config.logfile = os.path.join('/var/log/', config.name + '.log') - if config.pidfile is None: - config.pidfile = os.path.join('/var/run/', config.name + '.pid') - - #set up the root logger - logging.getLogger('').setLevel(args.debug) - formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") - if config.background: - #save logs in a file - fh = logging.FileHandler(config.logfile) - fh.setFormatter(formatter) - logging.getLogger('').addHandler(fh) - else: - #save logs to the console - ch = logging.StreamHandler() - ch.setFormatter(formatter) - logging.getLogger('').addHandler(ch) - - if config.background: - context = daemon.DaemonContext(detach_process = True, - pidfile = daemon.pidfile.TimeoutPIDLockFile(config.pidfile,10), - files_preserve=[fh.stream.fileno()], - working_directory=os.path.abspath(config.root), - stdout=sys.stdout, stderr=sys.stderr) - else: - context = daemon.DaemonContext(detach_process = False, - stdout=sys.stdout, stderr=sys.stderr, - working_directory=os.path.abspath(config.root)) - - with context: - component = ObjectComponent(config.name, config.secret, - config.host, config.port, - config.root) - if component.connect(): - logging.info('Component {} connected'.format(component.boundjid)) - component.process(block=False) - else: - logging.error("Component {} couldn't connect".format(component.boundjid)) -- cgit v1.2.3-70-g09d2