generated from Hear-Ye/congress-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·82 lines (63 loc) · 2.03 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
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
import importlib
import logging
import os
# set global HTTP timeouts to 10 seconds
import socket
import sys
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, "congress_crawler"))
from congress_crawler 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)