-
Notifications
You must be signed in to change notification settings - Fork 45
/
tasks.py
335 lines (263 loc) · 9.92 KB
/
tasks.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import os
import base64
import logging
import platform
from datetime import date, timedelta
from invoke import run, task
# from elasticsearch import helpers
# from dateutil.parser import parse
# from six.moves.urllib import parse as urllib_parse
# import scrapi.harvesters # noqa
# from scrapi import linter
# from scrapi import registry
# from scrapi import settings
logger = logging.getLogger()
WHEELHOUSE_PATH = os.environ.get('WHEELHOUSE')
@task
def reindex(src, dest):
''' Reindexes documents from index {src} to index {dest}'''
from elasticsearch import helpers
from scrapi.processing.elasticsearch import DatabaseManager
dm = DatabaseManager()
dm.setup()
helpers.reindex(dm.es, src, dest)
dm.es.indices.delete(src)
@task
def alias(alias, index):
''' Creates an Elasticsearch index alias '''
from scrapi.processing.elasticsearch import DatabaseManager
dm = DatabaseManager()
dm.setup()
dm.es.indices.delete_alias(index=alias, name='_all', ignore=404)
dm.es.indices.put_alias(alias, index)
@task
def migrate(migration, sources=None, kwargs_string=None, dry=True, async=False, group_size=1000):
''' Task to run a migration.
:param migration: The migration function to run. This is passed in
as a string then interpreted as a function by the invoke task.
:type migration: str
:param kwargs_string: parsed into an optional set of keyword
arguments, so that the invoke migrate task can accept a variable
number of arguments for each migration.
The kwargs_string should be in the following format:
'key:value, key2:value2'
...with the keys and values seperated by colons, and each kwarg seperated
by commas.
:type kwarg_string: str
An example of usage renaming mit to mit 2 as a real run would be:
inv migrate rename -s mit -k 'target:mit2' --no-dry
An example of calling renormalize on two sources as an async dry run:
inv migrate renormalize -s 'mit,asu' -a
'''
kwargs_string = kwargs_string or ':'
sources = sources or ''
from scrapi import migrations
from scrapi.tasks import migrate
kwargs = {}
for key, val in map(lambda x: x.split(':'), kwargs_string.split(',')):
key, val = key.strip(), val.strip()
if key not in kwargs.keys():
kwargs[key] = val
elif isinstance(kwargs[key], list):
kwargs[key].append(val)
else:
kwargs[key] = [kwargs[key], val]
kwargs['dry'] = dry
kwargs['async'] = async
kwargs['group_size'] = group_size
kwargs['sources'] = list(map(lambda x: x.strip(), sources.split(',')))
if kwargs['sources'] == ['']:
kwargs.pop('sources')
migrate_func = migrations.__dict__[migration]
migrate(migrate_func, **kwargs)
@task
def restart_search():
''' Restarts Elasticsearch '''
run("curl -XPOST 'http://localhost:9200/_shutdown'")
if platform.linux_distribution()[0] == 'Ubuntu':
run("sudo service elasticsearch restart")
elif platform.system() == 'Darwin': # Mac OSX
run('elasticsearch')
@task
def elasticsearch():
'''Start a local elasticsearch server
NOTE: Requires that elasticsearch is installed. See README for instructions
'''
if platform.linux_distribution()[0] == 'Ubuntu':
run("sudo service elasticsearch restart")
elif platform.system() == 'Darwin': # Mac OSX
run('elasticsearch')
else:
print(
"Your system is not recognized, you will have to start elasticsearch manually")
@task
def test(cov=True, doctests=True, verbose=False, debug=False, pdb=False):
"""
Runs all tests in the 'tests/' directory, and all doctests in the scrAPI directory
"""
cmd = 'py.test scrapi tests'
if doctests:
cmd += ' --doctest-modules'
if verbose:
cmd += ' -v'
if debug:
cmd += ' -s'
if cov:
cmd += ' --cov-report term-missing --cov-config .coveragerc --cov scrapi --cov api'
if pdb:
cmd += ' --pdb'
run(cmd, pty=True)
@task
def wheelhouse(develop=False):
''' Sets up the wheelhouse for requirements '''
req_file = 'dev-requirements.txt' if develop else 'requirements.txt'
cmd = 'pip wheel --find-links={} -r {} --wheel-dir={}'.format(WHEELHOUSE_PATH, req_file, WHEELHOUSE_PATH)
run(cmd, pty=True)
@task
def requirements(develop=False, upgrade=False):
''' Installs all Python dependencies '''
req_file = 'dev-requirements.txt' if develop else 'requirements.txt'
cmd = 'pip install -r {}'.format(req_file)
if upgrade:
cmd += ' --upgrade'
if WHEELHOUSE_PATH:
cmd += ' --no-index --find-links={}'.format(WHEELHOUSE_PATH)
run(cmd, pty=True)
@task
def beat(setup=True):
''' Runs the Celery Beat service '''
from scrapi import registry
from scrapi.tasks import app
# Set up the provider map for elasticsearch
if setup:
provider_map(delete=True)
app.conf['CELERYBEAT_SCHEDULE'] = registry.beat_schedule
app.Beat().run()
@task
def worker(loglevel='INFO', hostname='%h', autoreload=False):
''' Runs the Celery worker service '''
from scrapi.tasks import app
command = ['worker']
if loglevel:
command.extend(['--loglevel', loglevel])
if hostname:
command.extend(['--hostname', hostname])
if autoreload:
command.extend(['--autoreload'])
app.worker_main(command)
@task
def harvester(harvester_name, async=False, start=None, end=None):
''' Runs a specific harvester '''
os.environ['DJANGO_SETTINGS_MODULE'] = 'api.api.settings'
from scrapi import settings
settings.CELERY_ALWAYS_EAGER = not async
from scrapi import registry
from scrapi.tasks import run_harvester
from dateutil.parser import parse
if not registry.get(harvester_name):
raise ValueError('No such harvesters {}'.format(harvester_name))
end = parse(end).date() if end else date.today()
start = parse(start).date() if start else end - timedelta(settings.DAYS_BACK)
run_harvester.delay(harvester_name, start_date=start, end_date=end)
@task
def harvesters(async=False, start=None, end=None):
''' Runs all harvesters '''
from scrapi import settings
settings.CELERY_ALWAYS_EAGER = not async
from scrapi import registry
from scrapi.tasks import run_harvester
from dateutil.parser import parse
start = parse(start).date() if start else date.today() - timedelta(settings.DAYS_BACK)
end = parse(end).date() if end else date.today()
exceptions = []
for harvester_name in registry.keys():
try:
run_harvester.delay(harvester_name, start_date=start, end_date=end)
except Exception as e:
logger.exception(e)
exceptions.append(e)
logger.info("\n\nNumber of exceptions: {}".format(len(exceptions)))
for exception in exceptions:
logger.exception(e)
@task
def provider_map(delete=False):
''' Adds favicons and metadata for harvesters to Elasticsearch '''
from six.moves.urllib import parse as urllib_parse
from scrapi import registry
from scrapi.base.helpers import null_on_error
from scrapi.processing.elasticsearch import DatabaseManager
dm = DatabaseManager()
dm.setup()
es = dm.es
if delete:
es.indices.delete(index='share_providers', ignore=[404])
from scrapi.harvesters.push_api import gen_harvesters
gen_harvesters()
for harvester_name, harvester in registry.items():
if not null_on_error(es.get, log=False)(index='share_providers', doc_type=harvester_name, id=harvester_name):
with open("img/favicons/{}_favicon.ico".format(harvester.short_name), "rb") as f:
favicon = urllib_parse.quote(base64.encodestring(f.read()))
es.index(
'share_providers',
harvester.short_name,
body={
'favicon': 'data:image/png;base64,' + favicon,
'short_name': harvester.short_name,
'long_name': harvester.long_name,
'url': harvester.url
},
id=harvester.short_name,
refresh=True
)
print(es.count('share_providers', body={'query': {'match_all': {}}})['count'])
@task
def apiserver():
''' Runs the Django apiserver '''
os.system('python manage.py runserver')
@task
def apidb():
''' Runs the Django migrations '''
os.system('python manage.py migrate')
@task
def reset_all():
''' WARNING: Nukes all databases and sets them back up. Only run in development '''
import sys
from scrapi import settings
if sys.version[0] == "3":
raw_input = input
if raw_input('Are you sure? y/N ') != 'y':
return
os.system('psql -c "DROP DATABASE scrapi;" template1')
os.system('psql -c "CREATE DATABASE scrapi;" template1')
os.system('python manage.py migrate')
os.system("curl -XDELETE '{}/share*'".format(settings.ELASTIC_URI))
os.system("invoke alias share share_v2")
os.system("invoke provider_map")
@task
def institutions(grid_file='institutions/grid_2015_11_05.json', ipeds_file='institutions/hd2014.csv'):
''' Loads the institution data into Elasticsearch '''
grid(grid_file)
ipeds(ipeds_file)
@task
def remove_institutions(force=False):
''' Removes the institutions index from Elasticsearch '''
import six
if not force:
resp = six.moves.input('You are about to delete the institutions index. Are you sure? (y, n)\n')
if resp not in ('y', 'Y', 'Yes', 'yes'):
print('Remove institutions stopped.')
return
from institutions.institutions import remove
remove()
def grid(filepath):
from institutions import institutions, grid
institutions.setup()
grid.populate(filepath)
def ipeds(filepath):
from institutions import institutions, ipeds
institutions.setup()
ipeds.populate(filepath)
@task(default=True)
def usage():
''' Lists the available commands '''
run('invoke --list')