[damned-lies] Fixes #103 -- Build translated docs on the appropriate branch



commit 31d41691e36a3a3d57de8240b53bbc02eb9b32e4
Author: Claude Paroz <claude 2xlibre net>
Date:   Tue Sep 25 15:04:32 2018 +0200

    Fixes #103 -- Build translated docs on the appropriate branch

 vertimus/views.py | 55 ++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 34 insertions(+), 21 deletions(-)
---
diff --git a/vertimus/views.py b/vertimus/views.py
index 60c2c417..2dbdc6a4 100644
--- a/vertimus/views.py
+++ b/vertimus/views.py
@@ -16,7 +16,9 @@ from django.utils.safestring import mark_safe
 from django.utils.translation import gettext as _
 from django.views.generic import View
 
-from stats.models import Statistics, FakeLangStatistics, Module, Branch, Domain, Language
+from stats.models import (
+    Statistics, FakeLangStatistics, Module, ModuleLock, Branch, Domain, Language,
+)
 from stats.utils import DocFormat, UndetectableDocFormat, check_po_quality, is_po_reduced
 from vertimus.models import State, Action, ActionArchived, SendMailFailed
 from vertimus.forms import ActionForm
@@ -296,17 +298,31 @@ class BuildTranslatedDocsView(PoFileActionBase):
         if self.pofile is None:
             raise Http404('No target po file for this action')
 
-        html_dir = Path(settings.SCRATCHDIR, 'HTML', str(self.kwargs['action_pk']))
-        if html_dir.exists():
+        self.html_dir = Path(settings.SCRATCHDIR, 'HTML', str(self.kwargs['action_pk']))
+        if self.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)
+
+        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):
+        """
+        Try building translated docs, return an error message or an empty string
+        on success.
+        """
         try:
             doc_format = DocFormat(state.domain, state.branch)
         except UndetectableDocFormat as err:
-            messages.error(request, err)
-            return HttpResponseRedirect(state.get_absolute_url())
+            return str(err)
+
         build_error = _('Build failed (%(program)s): %(err)s')
         with tempfile.NamedTemporaryFile(suffix='.gmo') as gmo, \
                 tempfile.TemporaryDirectory() as build_dir:
@@ -314,10 +330,9 @@ class BuildTranslatedDocsView(PoFileActionBase):
                 'msgfmt', self.pofile, '-o', os.path.join(gmo.name)
             ], stderr=subprocess.PIPE)
             if result.returncode != 0:
-                messages.error(request, build_error % {
+                return build_error % {
                     'program': 'msgfmt', 'err': result.stderr.decode()
-                })
-                return HttpResponseRedirect(state.get_absolute_url())
+                }
 
             sources = doc_format.source_files()
             result = subprocess.run([
@@ -326,33 +341,31 @@ class BuildTranslatedDocsView(PoFileActionBase):
                 *[str(s) for s in sources],
             ], cwd=str(doc_format.vcs_path), stderr=subprocess.PIPE)
             if result.returncode != 0:
-                messages.error(request, build_error % {
+                return build_error % {
                     'program': 'itstool', 'err': result.stderr.decode()
-                })
-                return HttpResponseRedirect(state.get_absolute_url())
+                }
 
             # Now build the html version
-            if not html_dir.exists():
-                html_dir.mkdir(parents=True)
+            if not self.html_dir.exists():
+                self.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(html_dir),
+                'yelp-build', 'html', '-o', str(self.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:
-                messages.error(request, build_error % {
+                shutil.rmtree(str(self.html_dir))
+                return build_error % {
                     'program': 'yelp-build', 'err': result.stderr.decode()
-                })
-                shutil.rmtree(str(html_dir))
-                return HttpResponseRedirect(state.get_absolute_url())
+                }
 
-            if not (html_dir / 'index.html').exists():
+            if not (self.html_dir / 'index.html').exists():
                 # Create an index.html symlink to the base html doc if needed
                 try:
                     doc = parse(build_ref[0])
@@ -361,5 +374,5 @@ class BuildTranslatedDocsView(PoFileActionBase):
                     pass
                 else:
                     html_name = '%s.html' % base_name
-                    (html_dir / 'index.html').symlink_to(html_dir / html_name)
-        return HttpResponseRedirect(self.action.build_url)
+                    (self.html_dir / 'index.html').symlink_to(self.html_dir / html_name)
+        return ''


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]