Commit 78295f22 authored by Claude Paroz's avatar Claude Paroz

Make BuildTranslatedDocsView.build_docs less dependent on class state

parent 9ad9a022
......@@ -294,26 +294,26 @@ class QualityCheckView(PoFileActionBase):
class BuildTranslatedDocsView(PoFileActionBase):
def post(self, request, *args, **kwargs):
self.pofile = self.get_po_file()
if self.pofile is None:
pofile = self.get_po_file()
if pofile is None:
raise Http404('No target po file for this action')
self.html_dir = Path(settings.SCRATCHDIR, 'HTML', str(self.kwargs['action_pk']))
if self.html_dir.exists():
html_dir = Path(settings.SCRATCHDIR, 'HTML', str(self.kwargs['action_pk']))
if html_dir.exists():
# If the build already ran, redirect to the static results
return HttpResponseRedirect(self.action.build_url)
state = self.action.state_db
with ModuleLock(state.branch.module):
state.branch.checkout()
error_message = self.build_docs(state)
error_message = self.build_docs(state, pofile, html_dir)
if error_message:
messages.error(request, error_message)
return HttpResponseRedirect(state.get_absolute_url())
return HttpResponseRedirect(self.action.build_url)
def build_docs(self, state):
def build_docs(self, state, po_file, html_dir):
"""
Try building translated docs, return an error message or an empty string
on success.
......@@ -327,7 +327,7 @@ class BuildTranslatedDocsView(PoFileActionBase):
with tempfile.NamedTemporaryFile(suffix='.gmo') as gmo, \
tempfile.TemporaryDirectory() as build_dir:
result = subprocess.run([
'msgfmt', self.pofile, '-o', os.path.join(gmo.name)
'msgfmt', po_file, '-o', os.path.join(gmo.name)
], stderr=subprocess.PIPE)
if result.returncode != 0:
return build_error % {
......@@ -346,26 +346,26 @@ class BuildTranslatedDocsView(PoFileActionBase):
}
# Now build the html version
if not self.html_dir.exists():
self.html_dir.mkdir(parents=True)
if not html_dir.exists():
html_dir.mkdir(parents=True)
if doc_format.format == 'mallard':
# With mallard, specifying the directory is enough.
build_ref = [str(build_dir)]
else:
build_ref = [os.path.join(build_dir, s.name) for s in sources]
cmd = [
'yelp-build', 'html', '-o', str(self.html_dir),
'yelp-build', 'html', '-o', str(html_dir),
'-p', str(doc_format.vcs_path / 'C'),
*build_ref
]
result = subprocess.run(cmd, cwd=str(build_dir), stderr=subprocess.PIPE)
if result.returncode != 0:
shutil.rmtree(str(self.html_dir))
shutil.rmtree(str(html_dir))
return build_error % {
'program': 'yelp-build', 'err': result.stderr.decode()
}
if not (self.html_dir / 'index.html').exists():
if not (html_dir / 'index.html').exists():
# Create an index.html symlink to the base html doc if needed
try:
doc = parse(build_ref[0])
......@@ -374,5 +374,5 @@ class BuildTranslatedDocsView(PoFileActionBase):
pass
else:
html_name = '%s.html' % base_name
(self.html_dir / 'index.html').symlink_to(self.html_dir / html_name)
(html_dir / 'index.html').symlink_to(html_dir / html_name)
return ''
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment