[damned-lies] Improved support for slashes in branch names



commit af8abba41fafc00bcec9a16899e09d9c0a9252ad
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Nov 23 15:39:50 2019 +0100

    Improved support for slashes in branch names

 api/urls.py     |  4 ++--
 stats/models.py | 21 +++++++++++++--------
 stats/views.py  |  2 +-
 3 files changed, 16 insertions(+), 11 deletions(-)
---
diff --git a/api/urls.py b/api/urls.py
index 8bd27070..734be31f 100644
--- a/api/urls.py
+++ b/api/urls.py
@@ -15,12 +15,12 @@ urlpatterns = [
     path('releases/<name:release>/languages/<locale:lang>', views.ReleaseLanguageView.as_view(),
         name='api-release-language'),
     path(
-        'modules/<name:module_name>/branches/<name:branch_name>/domains/<name:domain_name>'
+        'modules/<name:module_name>/branches/<path:branch_name>/domains/<name:domain_name>'
         '/languages/<locale:lang>',
         views.ModuleLangStatsView.as_view(),
         name='api-module-lang-stats'
     ),
     # Used by a GitLab webhook to signal a commit for that module/branch
-    path('modules/<name:module_name>/branches/<name:branch_name>/ping', views.rebuild_branch,
+    path('modules/<name:module_name>/branches/<path:branch_name>/ping', views.rebuild_branch,
         name='api-module-rebuild'),
 ]
diff --git a/stats/models.py b/stats/models.py
index e24126d5..9496dd5a 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -224,6 +224,11 @@ class Branch(models.Model):
     def __str__(self):
         return "%s (%s)" % (self.name, self.module)
 
+    @property
+    def name_escaped(self):
+        """Branch name suitable for including in file system paths."""
+        return self.name.replace('/', '_')
+
     @cached_property
     def _repo(self):
         repo_class = repos.RepoBase.repo_class_by_type(self.module.vcs_type)
@@ -337,7 +342,7 @@ class Branch(models.Model):
         if self.module.vcs_type in ('hg', 'git'):
             branch_dir = self.module.name
         else:
-            branch_dir = self.module.name + "." + self.name
+            branch_dir = self.module.name + "." + self.name_escaped
         return Path(settings.SCRATCHDIR, self.module.vcs_type, branch_dir)
 
     def get_domains(self):
@@ -363,7 +368,7 @@ class Branch(models.Model):
     def output_dir(self, dom_type):
         """ Directory where generated pot and po files are written on local system """
         subdir = {'ui': '', 'doc': 'docs'}[dom_type]
-        dirname = Path(settings.POTDIR, self.module.name + "." + self.name, subdir)
+        dirname = Path(settings.POTDIR, self.module.name + "." + self.name_escaped, subdir)
         dirname.mkdir(parents=True, exist_ok=True)
         return dirname
 
@@ -451,7 +456,7 @@ class Branch(models.Model):
 
                 # 4. Compare with old pot files, various checks
                 # *****************************
-                previous_pot = self.output_dir(dom.dtype) / ('%s.%s.pot' % (dom.potbase(), self.name))
+                previous_pot = self.output_dir(dom.dtype) / ('%s.%s.pot' % (dom.potbase(), 
self.name_escaped))
                 if not potfile:
                     logging.error("Can't generate POT file for %s/%s." % (
                         self.module.name, dom.name))
@@ -494,7 +499,7 @@ class Branch(models.Model):
                 dom_langs = dom.get_lang_files(self.co_path)
                 for lang, pofile in dom_langs:
                     outpo = self.output_dir(dom.dtype) / (
-                        '%s.%s.%s.po' % (dom.potbase(), self.name, lang)
+                        '%s.%s.%s.po' % (dom.potbase(), self.name_escaped, lang)
                     )
                     if (not force and changed_status in (utils.NOT_CHANGED, utils.CHANGED_ONLY_FORMATTING)
                             and outpo.exists()
@@ -1406,9 +1411,9 @@ class Statistics(models.Model):
 
     def filename(self, potfile=False, reduced=False):
         if not self.is_pot_stats() and not potfile:
-            return "%s.%s.%s.%spo" % (self.domain.potbase(), self.branch.name, self.language.locale, reduced 
and "reduced." or "")
+            return "%s.%s.%s.%spo" % (self.domain.potbase(), self.branch.name_escaped, self.language.locale, 
reduced and "reduced." or "")
         else:
-            return "%s.%s.%spot" % (self.domain.potbase(), self.branch.name, reduced and "reduced." or "")
+            return "%s.%s.%spot" % (self.domain.potbase(), self.branch.name_escaped, reduced and "reduced." 
or "")
 
     def pot_text(self):
         if not self.full_po:
@@ -1480,7 +1485,7 @@ class Statistics(models.Model):
         subdir = ""
         if self.domain.dtype == "doc":
             subdir = "docs"
-        path = os.path.join(settings.POTDIR, "%s.%s" % (self.module_name, self.branch.name),
+        path = os.path.join(settings.POTDIR, "%s.%s" % (self.module_name, self.branch.name_escaped),
             subdir, self.filename(potfile, reduced))
         if reduced and not os.path.exists(path):
             path = self.po_path(potfile=potfile, reduced=False)
@@ -1491,7 +1496,7 @@ class Statistics(models.Model):
         subdir = ""
         if self.domain.dtype == "doc":
             subdir = "docs/"
-        return utils.url_join("/POT/", "%s.%s" % (self.module_name, self.branch.name),
+        return utils.url_join("/POT/", "%s.%s" % (self.module_name, self.branch.name_escaped),
             subdir, self.filename(potfile, reduced))
 
     def pot_url(self):
diff --git a/stats/views.py b/stats/views.py
index 702da43b..0084e512 100644
--- a/stats/views.py
+++ b/stats/views.py
@@ -252,7 +252,7 @@ def dynamic_po(request, module_name, branch_name, domain_name, filename):
             break
     response = HttpResponse(dyn_content, 'text/plain')
     response['Content-Disposition'] = 'inline; filename=%s' % (
-        ".".join([domain.potbase(), branch_name, filename]))
+        ".".join([domain.potbase(), branch.name_escaped, filename]))
     return response
 
 def releases(request, format='html'):


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