-
Notifications
You must be signed in to change notification settings - Fork 61
/
config_hci.py
307 lines (286 loc) · 10.5 KB
/
config_hci.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, re, types
import json
from copy import deepcopy
from twisted.internet import defer
TEST_CORPUS = "--test-corpus--"
DEFAULT_CORPUS = "--hyphe--"
CONFIG_FILE = os.path.join('config', 'config.json')
from hyphe_backend.lib import creationrules
from hyphe_backend.lib import webarchives
try:
from pymongo import MongoClient
except:
from pymongo import Connection as MongoClient
def load_config():
# Open config file
try:
with open(CONFIG_FILE, 'r') as config_file:
conf = json.load(config_file)
except IOError as e:
print 'ERROR: Could not open %s file : ' % CONFIG_FILE, e
exit(1)
except ValueError as e:
print 'ERROR: Config file is not valid JSON', e
exit(1)
# Set default noproxy setting if missing
if "mongo-scrapy" in conf:
if 'proxy_host' not in conf['mongo-scrapy']:
conf['mongo-scrapy']['proxy_host'] = ''
if 'proxy_port' not in conf['mongo-scrapy']:
conf['mongo-scrapy']['proxy_port'] = 3128
# Ensure retrocompat
conf['mongo-scrapy']['proxy'] = {
'host': conf['mongo-scrapy']['proxy_host'],
'port': conf['mongo-scrapy']['proxy_port']
}
for missing_key in ['store_crawled_html_content', 'ignore_internal_links', 'obey_robots']:
if missing_key not in conf['mongo-scrapy']:
conf['mongo-scrapy'][missing_key] = False
# Set default creation rules if missing
if "defaultCreationRule" not in conf:
conf["defaultCreationRule"] = "domain"
if "creationRules" not in conf:
conf["creationRules"] = {}
# Auto unset phantomJs autoretry if missing
if "phantom" in conf and "autoretry" not in conf["phantom"]:
conf["phantom"]["autoretry"] = False
# Check sanity
try:
check_conf_sanity(conf, GLOBAL_CONF_SCHEMA)
except Exception as e:
print e
exit(1)
# Test MongoDB server
mongoconf = conf['mongo-scrapy']
db = MongoClient(os.environ.get('HYPHE_MONGODB_HOST', mongoconf['host']), int(os.environ.get('HYPHE_MONGODB_PORT', mongoconf['mongo_port'])))[mongoconf.get('db_name', mongoconf.get('project'))]
try:
test = list(db['%s.logs' % DEFAULT_CORPUS].find())
except Exception as x:
print "ERROR: Cannot connect to mongoDB, please check your server and the configuration in %s" % CONFIG_FILE
if conf['DEBUG']:
print x
exit(1)
# Turn on Twisted debugging
if conf['DEBUG']:
defer.setDebugging(True)
return conf
VALID_STARTPAGES_MODES = re.compile(r'(%s)$' % "|".join(['startpages', 'prefixes', 'pages-\d+', 'homepage']))
def validateStartpagesMode(modes):
"""be a string or an array of strings among "prefixes", "startpages" and "pages-<N>" where "<N>" is an int."""
if type(modes) in [str, unicode, bytes]:
modes = [modes]
for m in modes:
if type(m) not in [str, unicode, bytes]:
return False
m = m.lower()
if not VALID_STARTPAGES_MODES.match(m):
return False
if not modes:
return False
return True
GLOBAL_CONF_SCHEMA = {
"mongo-scrapy": {
"type": dict,
"int_fields": ["mongo_port", "proxy_port", "scrapy_port", "max_depth", "max_simul_requests", "max_simul_requests_per_host"],
"str_fields": ["host", "proxy_host", "db_name"],
"extra_fields": {
"download_delay": float,
"store_crawled_html_content": bool,
"ignore_internal_links": bool,
"obey_robots": bool
}
}, "traph": {
"type": dict,
"int_fields": ["keepalive", "max_simul_pages_indexing"],
"extra_fields": {
"data_path": "path"
}
}, "core_api_port": {
"type": int
}, "defaultStartpagesMode": {
"type": validateStartpagesMode
}, "defaultCreationRule": {
"type": creationrules.testPreset
}, "creationRules": {
"type": dict,
"keys": str,
"values": creationrules.testPreset
}, "discoverPrefixes": {
"type": list
}, "phantom": {
"type": dict,
"int_fields": ["timeout", "idle_timeout", "ajax_timeout"],
"extra_fields": {
"whitelist_domains": list,
"autoretry": bool
}
}, "webarchives": {
"type": dict,
"int_fields": ["days_range"],
"str_fields": ["password"],
"extra_fields": {
"options": webarchives.validateOptions,
"date": webarchives.validateArchiveDate
}
}, "DEBUG": {
"type": int
}
}
CORPUS_CONF_SCHEMA = {
"keepalive": {
"type": int,
"default": "global/traph/keepalive"
},
"max_depth": {
"type": int,
"default": "global/mongo-scrapy/max_depth",
"max": "global/mongo-scrapy/max_depth"
},
"obey_robots": {
"type": bool,
"default": "global/mongo-scrapy/obey_robots"
},
"ignore_internal_links": {
"type": bool,
"default": "global/mongo-scrapy/ignore_internal_links"
},
"defaultStartpagesMode": {
"type": validateStartpagesMode,
"default": "global/defaultStartpagesMode"
},
"defaultCreationRule": {
"type": creationrules.testPreset,
"default": "global/defaultCreationRule"
},
"follow_redirects": {
"type": list,
"default": "global/discoverPrefixes"
},
"proxy": {
"type": dict,
"str_fields": ["host"],
"int_fields": ["port"],
"default": "global/mongo-scrapy/proxy"
},
"phantom": {
"type": dict,
"int_fields": ["timeout", "idle_timeout", "ajax_timeout"],
"extra_fields": {
"whitelist_domains": list,
"autoretry": bool
},
"default": "global/phantom"
},
"webarchives_option": {
"type": webarchives.validateOption,
"default": ""
},
"webarchives_date": {
"type": webarchives.validateArchiveDate,
"default": "global/webarchives/date"
},
"webarchives_days_range": {
"type": int,
"default": "global/webarchives/days_range"
}
}
def dict_accessor(access, dico):
path = access.split('/')
tmp = deepcopy(dico)
while path:
curp = path.pop(0)
if curp not in tmp:
raise(TypeError("Default path value missing from global config: %s" % access))
tmp = deepcopy(tmp[curp])
return tmp
def clean_missing_corpus_options(conf, globalconf):
for opt, schema in CORPUS_CONF_SCHEMA.items():
if opt not in conf:
if type(schema["default"]) == str and schema["default"].startswith("global/"):
conf[opt] = dict_accessor(schema["default"][7:], globalconf)
else:
conf[opt] = schema["default"]
elif schema["type"] == dict:
for f in schema.get("int_fields", []) + schema.get("str_fields", []) + schema.get("extra_fields", {}).keys():
if f not in conf[opt]:
conf[opt][f] = dict_accessor(schema["default"][7:], globalconf)[f]
return conf
error_config = lambda x, ns, nm: Exception("ERROR in %s while reading %s:\n%s" % (nm, "field %s" % ns if ns else "", x))
def check_conf_sanity(conf, schema, name=CONFIG_FILE, soft=False, globalconf=None):
for ns, rules in schema.iteritems():
if ns not in conf:
if soft:
continue
raise(error_config("field %s missing" % ns, "", name))
test_type(conf[ns], rules["type"], ns, name=name, extra_rules=rules, globalconf=globalconf)
if rules["type"] == dict:
for f in rules.get("int_fields", []):
if f not in conf[ns]:
if soft:
continue
raise(error_config("int field %s missing" % f, ns, name))
test_type(conf[ns][f], int, f, ns, name=name)
for f in rules.get("str_fields", []):
if f not in conf[ns]:
if soft:
continue
raise(error_config("string field %s missing" % f, ns, name))
test_type(conf[ns][f], str, f, ns, name=name)
for f, otyp in rules.get("extra_fields", {}).iteritems():
if f not in conf[ns]:
if soft:
continue
raise(error_config("%s field %s missing" % (otyp, f), ns, name))
test_type(conf[ns][f], otyp, f, ns, name=name)
if 'keys' in rules:
for k in conf[ns]:
test_type(k, rules["keys"], k, ns, name=name)
if 'values' in rules:
for k in conf[ns]:
test_type(conf[ns][k], rules["values"], k, ns, name=name)
def test_type(obj, otype, ns, ns2="", name=CONFIG_FILE, extra_rules=None, globalconf=None):
if type(otype) == types.FunctionType:
if not otype(obj):
raise(error_config("field %s should %s" % (ns, otype.__doc__), ns2, name))
elif type(otype) == list:
if obj not in otype:
raise(error_config("field %s should be one of %s" % (ns, ", ".join(otype)), ns2, name))
elif otype == str or otype == "path":
if type(obj) not in [str, bytes, unicode]:
raise(error_config("field %s should be a string" % ns, ns2, name))
if otype == "path":
try:
test_and_make_dir(obj)
except:
raise(error_config("field %s should be a writable directory path" % ns, ns2, name))
elif otype == "range":
if not (type(obj) in (list, tuple) and len(obj) == 2 and type(obj[0]) == int and type(obj[1]) == int):
raise(error_config("field %s should be a list of two int values" % ns, ns2, name))
elif otype == float:
if type(obj) not in [float, int]:
raise(error_config("field %s should a number" % ns, ns2, name))
elif otype == int:
if type(obj) != int:
raise(error_config("field %s should a number" % ns, ns2, name))
if obj < 0:
raise(error_config("field %s should be positive" % ns, ns2, name))
if extra_rules and "max" in extra_rules:
maxval = extra_rules["max"]
if type(maxval) == str and maxval.startswith("global/"):
maxval = dict_accessor(maxval[7:], globalconf)
if obj > maxval:
raise(error_config("field %s must be lower than %s" % (ns, maxval), ns2, name))
elif otype != bool and type(obj) != otype:
raise(error_config("field %s should be of type %s" % (ns, str(otype)), ns2, name))
if otype == list:
for e in obj:
if type(e) not in [str, bytes, unicode]:
raise(error_config("%s should be a list of strings" % ns, ns2, name))
def test_and_make_dir(path):
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise