/
__init__.py
228 lines (181 loc) · 6.82 KB
/
__init__.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
# -*- coding: utf-8 -*-
"""Common utilty functions."""
from __future__ import absolute_import
import errno
import getpass
import logging
import os
import re
from django.conf import settings
from django.utils import six
from django.utils.functional import allow_lazy
from django.utils.safestring import SafeText, mark_safe
from django.utils.text import slugify as slugify_base
from future.backports.urllib.parse import urlparse
from celery import group, chord
from readthedocs.builds.constants import LATEST, BUILD_STATE_TRIGGERED
from readthedocs.doc_builder.constants import DOCKER_LIMITS
log = logging.getLogger(__name__)
SYNC_USER = getattr(settings, 'SYNC_USER', getpass.getuser())
def broadcast(type, task, args, kwargs=None, callback=None): # pylint: disable=redefined-builtin
"""
Run a broadcast across our servers.
Returns a task group that can be checked for results.
`callback` should be a task signature that will be run once,
after all of the broadcast tasks have finished running.
"""
assert type in ['web', 'app', 'build']
if kwargs is None:
kwargs = {}
default_queue = getattr(settings, 'CELERY_DEFAULT_QUEUE', 'celery')
if type in ['web', 'app']:
servers = getattr(settings, "MULTIPLE_APP_SERVERS", [default_queue])
elif type in ['build']:
servers = getattr(settings, "MULTIPLE_BUILD_SERVERS", [default_queue])
tasks = []
for server in servers:
task_sig = task.s(*args, **kwargs).set(queue=server)
tasks.append(task_sig)
if callback:
task_promise = chord(tasks, callback).apply_async()
else:
# Celery's Group class does some special handling when an iterable with
# len() == 1 is passed in. This will be hit if there is only one server
# defined in the above queue lists
if len(tasks) > 1:
task_promise = group(*tasks).apply_async()
else:
task_promise = group(tasks).apply_async()
return task_promise
def clean_url(url):
parsed = urlparse(url)
if parsed.scheme or parsed.netloc:
return parsed.netloc
return parsed.path
def cname_to_slug(host):
from dns import resolver
answer = [ans for ans in resolver.query(host, 'CNAME')][0]
domain = answer.target.to_unicode()
slug = domain.split('.')[0]
return slug
def prepare_build(
project, version=None, record=True, force=False, immutable=True):
"""
Prepare a build in a Celery task for project and version.
If project has a ``build_queue``, execute the task on this build queue. If
project has ``skip=True``, the build is not triggered.
:param project: project's documentation to be built
:param version: version of the project to be built. Default: ``latest``
:param record: whether or not record the build in a new Build object
:param force: build the HTML documentation even if the files haven't changed
:param immutable: whether or not create an immutable Celery signature
:returns: Celery signature of UpdateDocsTask to be executed
"""
# Avoid circular import
from readthedocs.projects.tasks import UpdateDocsTask
from readthedocs.builds.models import Build
if project.skip:
log.info(
'Build not triggered because Project.skip=True: project=%s',
project.slug,
)
return None
if not version:
version = project.versions.get(slug=LATEST)
kwargs = {
'version_pk': version.pk,
'record': record,
'force': force,
}
if record:
build = Build.objects.create(
project=project,
version=version,
type='html',
state=BUILD_STATE_TRIGGERED,
success=True,
)
kwargs['build_pk'] = build.pk
options = {}
if project.build_queue:
options['queue'] = project.build_queue
# Set per-task time limit
time_limit = DOCKER_LIMITS['time']
try:
if project.container_time_limit:
time_limit = int(project.container_time_limit)
except ValueError:
log.warning('Invalid time_limit for project: %s', project.slug)
# Add 20% overhead to task, to ensure the build can timeout and the task
# will cleanly finish.
options['soft_time_limit'] = time_limit
options['time_limit'] = int(time_limit * 1.2)
update_docs_task = UpdateDocsTask()
return update_docs_task.signature(
(project.pk,),
kwargs=kwargs,
options=options,
immutable=True,
)
def trigger_build(project, version=None, record=True, force=False):
"""
Trigger a Build.
Helper that calls ``prepare_build`` and just effectively trigger the Celery
task to be executed by a worker.
:param project: project's documentation to be built
:param version: version of the project to be built. Default: ``latest``
:param record: whether or not record the build in a new Build object
:param force: build the HTML documentation even if the files haven't changed
:returns: Celery AsyncResult promise
"""
update_docs_task = prepare_build(
project,
version,
record,
force,
immutable=True,
)
if update_docs_task is None:
# Current project is skipped
return None
return update_docs_task.apply_async()
def send_email(recipient, subject, template, template_html, context=None,
request=None, from_email=None, **kwargs): # pylint: disable=unused-argument
"""
Alter context passed in and call email send task.
.. seealso::
Task :py:func:`readthedocs.core.tasks.send_email_task`
Task that handles templating and sending email message
"""
from ..tasks import send_email_task
if context is None:
context = {}
context['uri'] = '{scheme}://{host}'.format(
scheme='https', host=settings.PRODUCTION_DOMAIN)
send_email_task.delay(recipient=recipient, subject=subject, template=template,
template_html=template_html, context=context, from_email=from_email,
**kwargs)
def slugify(value, *args, **kwargs):
"""
Add a DNS safe option to slugify.
:param dns_safe: Remove underscores from slug as well
"""
dns_safe = kwargs.pop('dns_safe', True)
value = slugify_base(value, *args, **kwargs)
if dns_safe:
value = mark_safe(re.sub('[-_]+', '-', value))
return value
slugify = allow_lazy(slugify, six.text_type, SafeText)
def safe_makedirs(directory_name):
"""
Safely create a directory.
Makedirs has an issue where it has a race condition around checking for a
directory and then creating it. This catches the exception in the case where
the dir already exists.
"""
try:
os.makedirs(directory_name)
except OSError as e:
if e.errno == errno.EEXIST:
pass
raise