forked from unitedstates/congress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·73 lines (57 loc) · 1.9 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
import sys
import os
import traceback
import pprint as pp
import logging
import importlib
# set global HTTP timeouts to 10 seconds
import socket
socket.setdefaulttimeout(10)
CONGRESS_ROOT = os.path.dirname(os.path.abspath(__file__))
# name of the task comes first
task_name = sys.argv[1]
# parse any command line flags off
options = {}
for arg in sys.argv[2:]:
if arg.startswith("--"):
if "=" in arg:
key, value = arg.split('=')
else:
key, value = arg, True
key = key.split("--")[1]
if value == 'True':
value = True
elif value == 'False':
value = False
options[key.lower()] = value
# configure logging
if options.get('debug', False):
log_level = "debug"
else:
log_level = options.get("log", "warn")
if log_level not in ["debug", "info", "warn", "error"]:
print "Invalid log level (specify: debug, info, warn, error)."
sys.exit(1)
if options.get('timestamps', False):
logging.basicConfig(format='%(asctime)s %(message)s', level=log_level.upper())
else:
logging.basicConfig(format='%(message)s', level=log_level.upper())
sys.path.append(os.path.join(CONGRESS_ROOT, "tasks"))
import utils
try:
task_mod = __import__(task_name)
if 'patch' in options:
patch_mod = importlib.import_module(options['patch'])
patch_func = getattr(patch_mod, 'patch', None)
if patch_func is None:
logging.error("You specified a --patch argument but the {} module does not contain a 'patch' function.".format(options['patch']))
sys.exit(1)
elif not callable(patch_func):
logging.error("You specified a --patch argument but {}.patch is not callable".format(options['patch']))
sys.exit(1)
else:
patch_mod.patch(task_name)
task_mod.run(options)
except Exception as exception:
utils.admin(exception)