[damned-lies] Default to gettext extraction for modules using meson



commit cb045008cc3753ba7986426c490a62d5fd86389f
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Mar 24 12:07:46 2018 +0100

    Default to gettext extraction for modules using meson

 stats/models.py      |  131 +++++++++++++++++++++++++++-----------------------
 stats/tests/tests.py |   10 ++++
 2 files changed, 80 insertions(+), 61 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index 4267ac3..d8ffe8f 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -7,6 +7,7 @@ from collections import Counter, OrderedDict
 from datetime import datetime
 from functools import total_ordering
 from operator import itemgetter
+from pathlib import Path
 from time import sleep
 from urllib import request
 from urllib.error import URLError
@@ -255,6 +256,10 @@ class Branch(models.Model):
         return ((-self.weight,) + split_name(self.name)) > ((-other.weight,) + split_name(other.name))
 
     @property
+    def uses_meson(self):
+        return Path(self.co_path(), 'meson.build').exists()
+
+    @property
     def img_url_prefix(self):
         return self.module.vcs_type == 'git' and "plain" or ""
 
@@ -801,71 +806,16 @@ class Domain(models.Model):
         env = None
         podir = vcs_path
 
-        if self.module.name == 'damned-lies':
+        if current_branch.uses_meson or self.pot_method == GETTEXT_METHOD:
+            pot_command = self.get_xgettext_command(current_branch)
+
+        elif self.module.name == 'damned-lies':
             # Special case for d-l, pot file should be generated from running instance dir
             pot_command = ''
             call_command('update-trans', 'en')
             status, output, errs = 0, '', ''
             vcs_path = './po'
 
-        elif not self.pot_method:  # default is intltool
-            pot_command = ['intltool-update', '-g', self.potbase(), '-p']
-
-        elif self.pot_method == GETTEXT_METHOD:
-            pot_command = ['xgettext',
-                           '--files-from', 'POTFILES.in',
-                           '--directory', current_branch.co_path(),
-                           '--from-code', 'utf-8',
-                           '--add-comments',
-                           '--output', '%s.pot' % self.potbase(),
-                          ]
-            if not os.path.exists(utils.ITS_DATA_DIR):
-                utils.collect_its_data()
-            env = {'GETTEXTDATADIRS': os.path.dirname(utils.ITS_DATA_DIR)}
-            if self.extra_its_dirs:
-                env['GETTEXTDATADIRS'] = ':'.join(
-                    [env['GETTEXTDATADIRS']] + [os.path.join(current_branch.co_path(), path)
-                                                for path in self.extra_its_dirs.split(':')]
-                )
-            # Parse and use content from: "XGETTEXT_OPTIONS = --keyword=_ --keyword=N_"
-            makefile = utils.MakefileWrapper.find_file([vcs_path], file_name='Makevars')
-            if makefile:
-                kwds_vars = makefile.read_variable('XGETTEXT_OPTIONS')
-                if kwds_vars:
-                    pot_command.extend(kwds_vars.split())
-            else:
-                makefile = utils.MakefileWrapper.find_file([vcs_path], file_name='meson.build')
-                if makefile:
-                    if makefile.read_variable('gettext.preset') == 'glib':
-                        # These args should be kept in sync with
-                        # https://github.com/mesonbuild/meson/blob/master/mesonbuild/modules/i18n.py#L25
-                        pot_command.extend([
-                            '--keyword=_',
-                            '--keyword=N_',
-                            '--keyword=C_:1c,2',
-                            '--keyword=NC_:1c,2',
-                            '--keyword=g_dcgettext:2',
-                            '--keyword=g_dngettext:2,3',
-                            '--keyword=g_dpgettext2:2c,3',
-
-                            '--flag=N_:1:pass-c-format',
-                            '--flag=C_:2:pass-c-format',
-                            '--flag=NC_:2:pass-c-format',
-                            '--flag=g_dngettext:2:pass-c-format',
-                            '--flag=g_strdup_printf:1:c-format',
-                            '--flag=g_string_printf:2:c-format',
-                            '--flag=g_string_append_printf:2:c-format',
-                            '--flag=g_error_new:3:c-format',
-                            '--flag=g_set_error:4:c-format',
-                        ])
-                    extra_args = makefile.read_variable('gettext.args')
-                    if extra_args:
-                        pot_command.extend(extra_args)
-            # Added last as some chars in it may disturb CLI parsing
-            bugs_url = self.module.get_bugs_enter_url()
-            if bugs_url:
-                pot_command.extend(['--msgid-bugs-address', bugs_url])
-
         elif self.pot_method.startswith(('http://', 'https://')):
             # Get POT from URL and save file locally
             req = request.Request(self.pot_method)
@@ -876,9 +826,11 @@ class Domain(models.Model):
             potfile = os.path.join(vcs_path, self.potbase() + ".pot")
             with open(potfile, 'wb') as f:
                 f.write(handle.read())
-            pot_command = ':'  # noop
+            pot_command = None
+            status = utils.STATUS_OK
 
-        if 'intltool-update' in pot_command:
+        elif not self.pot_method:  # fallback is intltool
+            pot_command = ['intltool-update', '-g', self.potbase(), '-p']
             bugs_url = self.module.get_bugs_enter_url()
             if bugs_url:
                 env = {"XGETTEXT_ARGS": "\"--msgid-bugs-address=%s\"" % bugs_url}
@@ -907,6 +859,63 @@ class Domain(models.Model):
         else:
             return potfile, ()
 
+    def get_xgettext_command(self, branch):
+        pot_command = ['xgettext',
+                       '--files-from', 'POTFILES.in',
+                       '--directory', branch.co_path(),
+                       '--from-code', 'utf-8',
+                       '--add-comments',
+                       '--output', '%s.pot' % self.potbase(),
+                      ]
+        if not os.path.exists(utils.ITS_DATA_DIR):
+            utils.collect_its_data()
+        env = {'GETTEXTDATADIRS': os.path.dirname(utils.ITS_DATA_DIR)}
+        if self.extra_its_dirs:
+            env['GETTEXTDATADIRS'] = ':'.join(
+                [env['GETTEXTDATADIRS']] + [os.path.join(branch.co_path(), path)
+                                            for path in self.extra_its_dirs.split(':')]
+            )
+        # Parse and use content from: "XGETTEXT_OPTIONS = --keyword=_ --keyword=N_"
+        vcs_path = os.path.join(branch.co_path(), self.directory)
+        makefile = utils.MakefileWrapper.find_file([vcs_path], file_name='Makevars')
+        if makefile:
+            kwds_vars = makefile.read_variable('XGETTEXT_OPTIONS')
+            if kwds_vars:
+                pot_command.extend(kwds_vars.split())
+        else:
+            makefile = utils.MakefileWrapper.find_file([vcs_path], file_name='meson.build')
+            if makefile:
+                if makefile.read_variable('gettext.preset') == 'glib':
+                    # These args should be kept in sync with
+                    # https://github.com/mesonbuild/meson/blob/master/mesonbuild/modules/i18n.py#L25
+                    pot_command.extend([
+                        '--keyword=_',
+                        '--keyword=N_',
+                        '--keyword=C_:1c,2',
+                        '--keyword=NC_:1c,2',
+                        '--keyword=g_dcgettext:2',
+                        '--keyword=g_dngettext:2,3',
+                        '--keyword=g_dpgettext2:2c,3',
+
+                        '--flag=N_:1:pass-c-format',
+                        '--flag=C_:2:pass-c-format',
+                        '--flag=NC_:2:pass-c-format',
+                        '--flag=g_dngettext:2:pass-c-format',
+                        '--flag=g_strdup_printf:1:c-format',
+                        '--flag=g_string_printf:2:c-format',
+                        '--flag=g_string_append_printf:2:c-format',
+                        '--flag=g_error_new:3:c-format',
+                        '--flag=g_set_error:4:c-format',
+                    ])
+                extra_args = makefile.read_variable('gettext.args')
+                if extra_args:
+                    pot_command.extend(extra_args)
+        # Added last as some chars in it may disturb CLI parsing
+        bugs_url = self.module.get_bugs_enter_url()
+        if bugs_url:
+            pot_command.extend(['--msgid-bugs-address', bugs_url])
+        return pot_command
+
     def get_linguas(self, base_path):
         """ Return a linguas dict like this: {'langs':['lang1', lang2], 'error':"Error"} """
         if self.linguas_location:
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index 7a2e923..dbb1209 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -117,6 +117,16 @@ class ModuleTestCase(TestCase):
         help_domain.save()
         self.assertEqual(list(self.branch.get_domains().keys()), ['po'])
 
+    def test_domain_xgettext_command(self):
+        domain = self.branch.get_domains()['po']
+        self.assertEqual(
+            domain.get_xgettext_command(self.branch),
+            ['xgettext', '--files-from', 'POTFILES.in', '--directory',
+             '/home/claude/www/damned-lies/cvs/git/gnome-hello', '--from-code', 'utf-8',
+             '--add-comments', '--output', 'gnome-hello.pot', '--msgid-bugs-address',
+             'https://bugzilla.gnome.org/enter_bug.cgi?product=test&keywords=I18N+L10N&component=test']
+        )
+
     @test_scratchdir
     def test_branch_stats(self):
         lang = Language.objects.create(name='xxx', locale='xxx')


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