[damned-lies] Make Branch.co_path a cached property



commit a3c36ac4a23b8d2e0ace2eae682e49dd26d49121
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Mar 28 10:47:35 2018 +0200

    Make Branch.co_path a cached property

 stats/doap.py                               |    2 +-
 stats/management/commands/migrate-to-git.py |    4 +-
 stats/models.py                             |   47 ++++++++++++++-------------
 stats/tests/tests.py                        |   12 +++---
 stats/utils.py                              |    2 +-
 vertimus/tests/tests.py                     |    2 +-
 6 files changed, 35 insertions(+), 34 deletions(-)
---
diff --git a/stats/doap.py b/stats/doap.py
index c0deb12..563563f 100644
--- a/stats/doap.py
+++ b/stats/doap.py
@@ -51,7 +51,7 @@ class DoapParser(object):
 def update_doap_infos(module):
     """ Should only be called inside an "update-stats" context of a master branch,
         so there is no need for any extra checkout/locking strategy """
-    doap_path = module.get_head_branch().co_path() / ("%s.doap" % module.name)
+    doap_path = module.get_head_branch().co_path / ("%s.doap" % module.name)
     if not doap_path.exists():
         return
     try:
diff --git a/stats/management/commands/migrate-to-git.py b/stats/management/commands/migrate-to-git.py
index 797b95b..65bb40d 100644
--- a/stats/management/commands/migrate-to-git.py
+++ b/stats/management/commands/migrate-to-git.py
@@ -14,7 +14,7 @@ class Command(BaseCommand):
         for module in modules:
             old_branch_dirs = []
             for branch in module.branch_set.all():
-                old_branch_dirs.append(branch.co_path())
+                old_branch_dirs.append(branch.co_path)
 
             module.vcs_type = "git"
             module.vcs_root = "git://git.gnome.org/%s" % module.name
@@ -34,7 +34,7 @@ class Command(BaseCommand):
                 # Checkout branch (other than master)
                 cmd = ['git', 'checkout', '--track', '-b', branch.name, 'origin/%s' % branch.name]
                 try:
-                    utils.run_shell_command(cmd, raise_on_error=True, cwd=branch.co_path())
+                    utils.run_shell_command(cmd, raise_on_error=True, cwd=branch.co_path)
                 except Exception, e:
                     self.stderr.write("Unable to checkout branch '%s' of module '%s': %s" % (branch.name, 
module.name, e))
                     continue
diff --git a/stats/models.py b/stats/models.py
index 2bf6e62..b09e3fc 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -225,10 +225,10 @@ class Branch(models.Model):
     def delete_checkout(self):
         # Remove the repo checkout
         if self.module.vcs_type in ('cvs', 'svn'):
-            if os.access(self.co_path(), os.W_OK):
-                shutil.rmtree(self.co_path())
+            if os.access(self.co_path, os.W_OK):
+                shutil.rmtree(self.co_path)
         elif self.module.vcs_type == 'git':
-            wdir = self.co_path()
+            wdir = self.co_path
             if os.access(str(wdir), os.W_OK):
                 if self.is_head():
                     shutil.rmtree(str(wdir))
@@ -257,7 +257,7 @@ class Branch(models.Model):
 
     @property
     def uses_meson(self):
-        return Path(self.co_path(), 'meson.build').exists()
+        return Path(self.co_path, 'meson.build').exists()
 
     @property
     def img_url_prefix(self):
@@ -279,7 +279,7 @@ class Branch(models.Model):
         """ This method determine if some file has changed based on its hash
             Always returns true if this is the first time the path is checked
         """
-        full_path = self.co_path() / rel_path
+        full_path = self.co_path / rel_path
         if not full_path.exists():
             return False # Raise exception?
         new_hash = utils.compute_md5(full_path)
@@ -329,6 +329,7 @@ class Branch(models.Model):
             # Not implemented for other VCS
             return ""
 
+    @cached_property
     def co_path(self):
         """ Returns the path of the local checkout for the branch """
         if self.module.vcs_type in ('hg', 'git'):
@@ -355,7 +356,7 @@ class Branch(models.Model):
         return True
 
     def domain_path(self, domain):
-        return self.co_path() / domain.directory
+        return self.co_path / domain.directory
 
     def output_dir(self, dom_type):
         """ Directory where generated pot and po files are written on local system """
@@ -446,7 +447,7 @@ class Branch(models.Model):
                         dom.dtype, dom.name))
                     continue
                 errors.extend(errs)
-                linguas = dom.get_linguas(self.co_path())
+                linguas = dom.get_linguas(self.co_path)
                 if linguas['langs'] is None and linguas['error']:
                     errors.append(("warn", linguas['error']))
 
@@ -501,7 +502,7 @@ class Branch(models.Model):
                 # *****************************************
                 stats_with_ext_errors = Statistics.objects.filter(branch=self, domain=dom, 
information__type__endswith='-ext')
                 langs_with_ext_errors = [stat.language.locale for stat in stats_with_ext_errors]
-                dom_langs = dom.get_lang_files(self.co_path())
+                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)
@@ -552,22 +553,22 @@ class Branch(models.Model):
     def _exists(self):
         """ Determine if branch (self) already exists (i.e. already checked out) on local FS """
         if self.module.vcs_type == 'git':
-            if not self.co_path().exists():
+            if not self.co_path.exists():
                 return False
             command = "git branch | grep %s" % self.name
-            status, output, errs = utils.run_shell_command(command, cwd=self.co_path())
+            status, output, errs = utils.run_shell_command(command, cwd=self.co_path)
             return output != ""
         elif self.module.vcs_type == 'hg':
-            return self.id != None and os.access(str(self.co_path()), os.X_OK | os.W_OK)
+            return self.id != None and os.access(str(self.co_path), os.X_OK | os.W_OK)
         else:
-            return os.access(str(self.co_path()), os.X_OK | os.W_OK)
+            return os.access(str(self.co_path), os.X_OK | os.W_OK)
 
     def checkout(self):
         """ Do a checkout or an update of the VCS files """
         module_name = self.module.name
         vcs_type = self.module.vcs_type
         localroot = os.path.join(settings.SCRATCHDIR, vcs_type)
-        modulepath = self.co_path()
+        modulepath = self.co_path
         scmroot = self.module.vcs_root
 
         try: os.makedirs(localroot)
@@ -618,7 +619,7 @@ class Branch(models.Model):
         WARNING: if execute is True, the calling method should acquire a lock
         for the module to not mix checkouts in different threads/processes.
         """
-        modulepath = self.co_path()
+        modulepath = self.co_path
         logging.debug("Updating '%s.%s' (in '%s')..." % (self.module.name, self.name, modulepath))
         command_list = []
         if self.module.vcs_type == "cvs":
@@ -652,7 +653,7 @@ class Branch(models.Model):
             raise NotImplementedError("Commit is not implemented for '%s'" % vcs_type)
 
         locale = language.locale
-        commit_dir = self.co_path() / domain.directory
+        commit_dir = self.co_path / domain.directory
         prefix = '' if domain.dtype == 'ui' else locale
         dest_filename = os.path.join(prefix, "%s.po" % locale)
         dest_path = commit_dir / dest_filename
@@ -720,7 +721,7 @@ class Branch(models.Model):
             raise NotImplementedError("Commit cherry-pick is not implemented for '%s'" % 
self.module.vcs_type)
         with ModuleLock(self.module):
             self.update_repo()
-            commit_dir = self.co_path() / domain.directory
+            commit_dir = self.co_path / domain.directory
             result = utils.run_shell_command(
                 ['git', 'cherry-pick', '-x', commit_hash], cwd=commit_dir)
             if result[0] == utils.STATUS_OK:
@@ -791,7 +792,7 @@ class Domain(models.Model):
         if self.dtype == "ui":
             return "ui"
         else:
-            if os.access(os.path.join(branch.co_path(), self.directory, "C", "index.page"), os.R_OK):
+            if os.access(os.path.join(branch.co_path, self.directory, "C", "index.page"), os.R_OK):
                 return "mallard"
             else:
                 return "docbook"
@@ -818,7 +819,7 @@ class Domain(models.Model):
     def generate_pot_file(self, current_branch):
         """ Return the pot file generated (in the checkout tree), and the error if any """
 
-        vcs_path = current_branch.co_path() / self.directory
+        vcs_path = current_branch.co_path / self.directory
         env = None
         podir = vcs_path
 
@@ -863,7 +864,7 @@ class Domain(models.Model):
             # Try to get POT file from command output, with path relative to checkout root
             m = re.search('([\w/-]*\.pot)', output)
             if m:
-                potfile = current_branch.co_path() / m.group(0)
+                potfile = current_branch.co_path / m.group(0)
             else:
                 # Try to find .pot file in /po dir
                 for file_ in podir.iterdir():
@@ -883,7 +884,7 @@ class Domain(models.Model):
     def get_xgettext_command(self, branch):
         pot_command = ['xgettext',
                        '--files-from', 'POTFILES.in',
-                       '--directory', str(branch.co_path()),
+                       '--directory', str(branch.co_path),
                        '--from-code', 'utf-8',
                        '--add-comments',
                        '--output', '%s.pot' % self.potbase(),
@@ -893,11 +894,11 @@ class Domain(models.Model):
         env = {'GETTEXTDATADIRS': os.path.dirname(utils.ITS_DATA_DIR)}
         if self.extra_its_dirs:
             env['GETTEXTDATADIRS'] = ':'.join(
-                [env['GETTEXTDATADIRS']] + [str(branch.co_path() / path)
+                [env['GETTEXTDATADIRS']] + [str(branch.co_path / path)
                                             for path in self.extra_its_dirs.split(':')]
             )
         # Parse and use content from: "XGETTEXT_OPTIONS = --keyword=_ --keyword=N_"
-        vcs_path = branch.co_path() / self.directory
+        vcs_path = branch.co_path / self.directory
         makefile = utils.MakefileWrapper.find_file([vcs_path], file_name='Makevars')
         if makefile:
             kwds_vars = makefile.read_variable('XGETTEXT_OPTIONS')
@@ -1501,7 +1502,7 @@ class Statistics(models.Model):
                 fig2['translated_file'] = False
                 # Check if a translated figure really exists or if the English one is used
                 if (self.language and
-                        (self.branch.co_path() / self.domain.directory / self.language.locale
+                        (self.branch.co_path / self.domain.directory / self.language.locale
                          / fig['path']).exists()):
                     fig2['trans_remote_url'] = url_model % (self.language.locale, fig['path'])
                     fig2['translated_file'] = True
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index 64282b6..9776985 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -174,7 +174,7 @@ class ModuleTestCase(TestCase):
     @test_scratchdir
     def test_delete_branch(self):
         """ Deleting the master branch of a git module deletes the checkout dir """
-        checkout_path = self.branch.co_path()
+        checkout_path = self.branch.co_path
         branch = Branch.objects.create(name="gnome-hello-1-4", module = self.mod)
         branch.delete()
         self.assertTrue(checkout_path.exists())
@@ -244,12 +244,12 @@ class ModuleTestCase(TestCase):
         self.branch.update_stats(force=False)
 
         # Create a new file with translation
-        new_file_path = self.branch.co_path() / "dummy_file.py"
+        new_file_path = self.branch.co_path / "dummy_file.py"
         new_string = "Dummy string for D-L tests"
         with new_file_path.open('w') as fh:
             fh.write("a = _('%s')\n" % new_string)
         # Add the new file to POTFILES.in
-        with (self.branch.co_path() / "po" / "POTFILES.in").open('a') as fh:
+        with (self.branch.co_path / "po" / "POTFILES.in").open('a') as fh:
             fh.write("dummy_file.py\n")
         # Regenerate stats (mail should be sent)
         self.branch.update_stats(force=False, checkout=False)
@@ -345,7 +345,7 @@ class ModuleTestCase(TestCase):
         cherry-pick the branch commit on the master branch.
         """
         domain = self.mod.domain_set.get(name='po')
-        commit_dir = self.branch.co_path()
+        commit_dir = self.branch.co_path
         utils.run_shell_command(
             ['git', 'checkout', '-b', 'gnome-3-18', 'origin/master'], cwd=commit_dir, raise_on_error=True
         )
@@ -533,13 +533,13 @@ class FigureTests(TestCase):
         branch = Branch.objects.get(module__name='gnome-hello', name='master')
         pot_stat = Statistics.objects.get(branch=branch, domain__name='help', language__isnull=True)
         fig_path = str(
-            pot_stat.branch.co_path() / pot_stat.domain.directory / 'C' /
+            pot_stat.branch.co_path / pot_stat.domain.directory / 'C' /
             pot_stat.get_figures()[0]['path']
         )
         shutil.copyfile(fig_path, fig_path.replace('/C/', '/fr/'))
         doc_stat = Statistics.objects.get(branch=branch, domain__name='help', language__locale='fr')
         errs = utils.check_identical_figures(
-            doc_stat.get_figures(), branch.co_path() / 'help', 'fr',
+            doc_stat.get_figures(), branch.co_path / 'help', 'fr',
         )
         self.assertEqual(len(errs), 1)
         self.assertTrue(errs[0][1].startswith("Figures should not be copied"))
diff --git a/stats/utils.py b/stats/utils.py
index 29baaa0..50fdf59 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -636,7 +636,7 @@ def collect_its_data():
         with ModuleLock(branch.module):
             branch.checkout()
             for file_path in files:
-                src = branch.co_path() / file_path
+                src = branch.co_path / file_path
                 dest = os.path.join(ITS_DATA_DIR, os.path.basename(file_path))
                 shutil.copyfile(str(src), dest)
 
diff --git a/vertimus/tests/tests.py b/vertimus/tests/tests.py
index 5686551..2ec5e30 100644
--- a/vertimus/tests/tests.py
+++ b/vertimus/tests/tests.py
@@ -449,7 +449,7 @@ class VertimusTest(TeamsAndRolesTests):
         form = ActionForm(self.pcoo, state, state.get_available_actions(self.pcoo), True, post_data)
         self.assertTrue(form.is_valid())
         # path needed when copying file to commit
-        (self.b.co_path() / 'po').mkdir(parents=True, exist_ok=True)
+        (self.b.co_path / 'po').mkdir(parents=True, exist_ok=True)
         with patch_shell_command(only=['git ']) as cmds:
             action = Action.new_by_name('CI', person=self.pcoo)
             msg = action.apply_on(state, form.cleaned_data)


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