[damned-lies] Added ability to read more meson.build variables (i18n.gettext)



commit d502f1b306cf7c01315ecfabee804068c24fde8b
Author: Claude Paroz <claude 2xlibre net>
Date:   Sun Aug 13 22:50:53 2017 +0200

    Added ability to read more meson.build variables (i18n.gettext)

 stats/tests/meson-ui.build |    1 +
 stats/tests/tests.py       |    4 ++++
 stats/utils.py             |   36 ++++++++++++++++++++++++++++++------
 3 files changed, 35 insertions(+), 6 deletions(-)
---
diff --git a/stats/tests/meson-ui.build b/stats/tests/meson-ui.build
new file mode 100644
index 0000000..9423439
--- /dev/null
+++ b/stats/tests/meson-ui.build
@@ -0,0 +1 @@
+i18n.gettext(gnomebt_gettext_package, preset: 'glib')
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index 024d9e0..a24abf0 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -552,6 +552,10 @@ class UtilsTests(TestCase):
             meson_file.read_variable('sources'),
             ['index.page', 'what-is.page', 'legal.xml']
         )
+        # UI meson file
+        path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'meson-ui.build')
+        meson_file = utils.MesonfileWrapper(path)
+        self.assertEqual(meson_file.read_variable('preset'), 'glib')
 
     def test_read_cmake_variables(self):
         path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "help_mallard")
diff --git a/stats/utils.py b/stats/utils.py
index d5ffd62..a3d6552 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -160,11 +160,18 @@ class MakefileWrapper:
 
 
 class MesonfileWrapper(MakefileWrapper):
+    i18n_gettext_kwargs = {'po_dir', 'data_dirs', 'type', 'languages', 'args', 'preset'}
+    gnome_yelp_kwargs= {'sources', 'media', 'symlink_media', 'languages'}
+
     @cached_property
     def content(self):
         content = super().content
         # Here be dragons: Try to make meson content look like Python
-        content = re.sub(r'(sources|media|languages):', r'\1=', content)
+        content = re.sub(
+            r'(' + '|'.join(list(self.i18n_gettext_kwargs | self.gnome_yelp_kwargs)) + '):',
+            r'\1=',
+            content
+        )
         content = content.replace('true', 'True').replace('false', 'False')
         return content
 
@@ -180,11 +187,28 @@ class MesonfileWrapper(MakefileWrapper):
                     if var_name in kwargs:
                         catched[var_name] = kwargs[var_name]
 
-        gnome = VarCatcher()
-        try:
-            exec(self.content, {}, {'gnome': gnome, 'meson': Mock()})
-        except Exception:
-            pass
+            def gettext(self, *args, **kwargs):
+                catched['project_id'] = args[0]
+                for var_name in {'languages', 'args', 'preset'}:
+                    if var_name in kwargs:
+                        catched[var_name] = kwargs[var_name]
+
+        catcher = VarCatcher()
+        meson_locals = {'gnome': catcher, 'i18n': catcher, 'meson': Mock()}
+        while True:
+            try:
+                exec(self.content, {}, meson_locals)
+                break
+            except NameError as exc:
+                # Put the unknown name in the locals dict and retry exec
+                m = re.search(r"name '([^']*)' is not defined", str(exc))
+                if m:
+                    name = m.groups()[0]
+                    meson_locals[name] = Mock()
+                else:
+                    break
+            except Exception:
+                break
         for var in variables:
             if var in catched and not isinstance(catched[var], Mock):
                 return catched[var]


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