Commit 17288503 authored by Jonathan Michalon's avatar Jonathan Michalon

Put args parsing and config loading in methods and reuse them in the watchdog

parent df8f10bd
......@@ -5,6 +5,7 @@ import os
import importlib
from picomon import config
from picomon import mails
from picomon.__main__ import parse_args, import_config
# launch picomon
......@@ -14,19 +15,9 @@ if os.path.sep in sys.argv[0]:
retcode = subprocess.call([filename] + sys.argv[1:])
# load config file (unprotected, will trigger exceptions if problems but real
# picomon beforehand)
configfile = "config.py"
if "-c" in sys.argv:
configfile = sys.argv[sys.argv.index("-c") + 1]
if "--config" in sys.argv:
configfile = sys.argv[sys.argv.index("--config") + 1]
sys.path.append(os.path.dirname(configfile))
filename = os.path.basename(configfile)
base, ext = os.path.splitext(filename)
importlib.import_module(base)
# load config
args = parse_args()
import_config(args.config)
# send warning
mails.send_email(config.emails.watchdog_subject,
......
......@@ -57,8 +57,7 @@ def __alarm_handler(signum, frame):
mails.send_email_report(report)
def run():
# Parse command line
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-1", "--one",
help="single run with immediate output of " +
......@@ -70,12 +69,14 @@ def run():
parser.add_argument("-c", "--config",
help="Set config file (defauts to config.py)",
default='config.py')
args = parser.parse_args()
return parser.parse_args()
def import_config(configfile):
# import config file module
try:
sys.path.append(os.path.dirname(args.config))
filename = os.path.basename(args.config)
sys.path.append(os.path.dirname(configfile))
filename = os.path.basename(configfile)
base, ext = os.path.splitext(filename)
importlib.import_module(base)
except ImportError as e:
......@@ -83,6 +84,14 @@ def run():
args.config, str(e)))
sys.exit(1)
def run():
# Parse command line
args = parse_args()
# import config file module
import_config(args.config)
# Configure logging
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
level=config.verb_level)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment