[meld] build_helpers: Move in i18n helper



commit 09a40f9463b3688cd9d5df18a031a1927a42ab2f
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Thu Nov 7 07:16:32 2013 +1000

    build_helpers: Move in i18n helper
    
    This is the last used piece of python-distutils-extra that we need.

 meld/build_helpers.py |  137 +++++++++++++++++++++++++++++++++++++++++++++++++
 setup.py              |   14 ++---
 2 files changed, 142 insertions(+), 9 deletions(-)
---
diff --git a/meld/build_helpers.py b/meld/build_helpers.py
index 3e4fdf7..f3f0191 100644
--- a/meld/build_helpers.py
+++ b/meld/build_helpers.py
@@ -104,3 +104,140 @@ class build_icons(distutils.cmd.Command):
                                         os.path.basename(size),
                                         os.path.basename(category)),
                                         icons))
+
+
+class build_i18n(distutils.cmd.Command):
+
+    description = "integrate the gettext framework"
+
+    user_options = [('desktop-files=', None, '.desktop.in files that '
+                                             'should be merged'),
+                    ('xml-files=', None, '.xml.in files that should be '
+                                         'merged'),
+                    ('schemas-files=', None, '.schemas.in files that '
+                                             'should be merged'),
+                    ('ba-files=', None, 'bonobo-activation files that '
+                                        'should be merged'),
+                    ('rfc822deb-files=', None, 'RFC822 files that should '
+                                               'be merged'),
+                    ('key-files=', None, '.key.in files that should be '
+                                         'merged'),
+                    ('domain=', 'd', 'gettext domain'),
+                    ('merge-po', 'm', 'merge po files against template'),
+                    ('po-dir=', 'p', 'directory that holds the i18n files'),
+                    ('bug-contact=', None, 'contact address for msgid bugs')]
+
+    boolean_options = ['merge-po']
+
+    def initialize_options(self):
+        self.desktop_files = []
+        self.xml_files = []
+        self.key_files = []
+        self.schemas_files = []
+        self.ba_files = []
+        self.rfc822deb_files = []
+        self.domain = None
+        self.merge_po = False
+        self.bug_contact = None
+        self.po_dir = None
+
+    def finalize_options(self):
+        if self.domain is None:
+            self.domain = self.distribution.metadata.name
+        if self.po_dir is None:
+            self.po_dir = "po"
+
+    def run(self):
+        """
+        Update the language files, generate mo files and add them
+        to the to be installed files
+        """
+        if not os.path.isdir(self.po_dir):
+            return
+
+        data_files = self.distribution.data_files
+        if data_files is None:
+            # in case not data_files are defined in setup.py
+            self.distribution.data_files = data_files = []
+
+        if self.bug_contact is not None:
+            os.environ["XGETTEXT_ARGS"] = "--msgid-bugs-address=%s " % \
+                                          self.bug_contact
+
+        # Print a warning if there is a Makefile that would overwrite our
+        # values
+        if os.path.exists("%s/Makefile" % self.po_dir):
+            self.announce("""
+WARNING: Intltool will use the values specified from the
+         existing po/Makefile in favor of the vaules
+         from setup.cfg.
+         Remove the Makefile to avoid problems.""")
+
+        # If there is a po/LINGUAS file, or the LINGUAS environment variable
+        # is set, only compile the languages listed there.
+        selected_languages = None
+        linguas_file = os.path.join(self.po_dir, "LINGUAS")
+        if os.path.isfile(linguas_file):
+            selected_languages = open(linguas_file).read().split()
+        if "LINGUAS" in os.environ:
+            selected_languages = os.environ["LINGUAS"].split()
+
+        # Update po(t) files and print a report
+        # We have to change the working dir to the po dir for intltool
+        cmd = ["intltool-update", (self.merge_po and "-r" or "-p"), "-g", self.domain]
+        wd = os.getcwd()
+        os.chdir(self.po_dir)
+        self.spawn(cmd)
+        os.chdir(wd)
+        max_po_mtime = 0
+        for po_file in glob.glob("%s/*.po" % self.po_dir):
+            lang = os.path.basename(po_file[:-3])
+            if selected_languages and not lang in selected_languages:
+                continue
+            mo_dir =  os.path.join("build", "mo", lang, "LC_MESSAGES")
+            mo_file = os.path.join(mo_dir, "%s.mo" % self.domain)
+            if not os.path.exists(mo_dir):
+                os.makedirs(mo_dir)
+            cmd = ["msgfmt", po_file, "-o", mo_file]
+            po_mtime = os.path.getmtime(po_file)
+            mo_mtime = os.path.exists(mo_file) and os.path.getmtime(mo_file) or 0
+            if po_mtime > max_po_mtime:
+                max_po_mtime = po_mtime
+            if po_mtime > mo_mtime:
+                self.spawn(cmd)
+
+            targetpath = os.path.join("share/locale", lang, "LC_MESSAGES")
+            data_files.append((targetpath, (mo_file,)))
+
+        # merge .in with translation
+        for (option, switch) in ((self.xml_files, "-x"),
+                                 (self.desktop_files, "-d"),
+                                 (self.schemas_files, "-s"),
+                                 (self.rfc822deb_files, "-r"),
+                                 (self.ba_files, "-b"),
+                                 (self.key_files, "-k"),):
+            try:
+                file_set = eval(option)
+            except:
+                continue
+            for (target, files) in file_set:
+                build_target = os.path.join("build", target)
+                if not os.path.exists(build_target): 
+                    os.makedirs(build_target)
+                files_merged = []
+                for file in files:
+                    if file.endswith(".in"):
+                        file_merged = os.path.basename(file[:-3])
+                    else:
+                        file_merged = os.path.basename(file)
+                    file_merged = os.path.join(build_target, file_merged)
+                    cmd = ["intltool-merge", switch, self.po_dir, file, 
+                           file_merged]
+                    mtime_merged = os.path.exists(file_merged) and \
+                                   os.path.getmtime(file_merged) or 0
+                    mtime_file = os.path.getmtime(file)
+                    if mtime_merged < max_po_mtime or mtime_merged < mtime_file:
+                        # Only build if output is older than input (.po,.in) 
+                        self.spawn(cmd)
+                    files_merged.append(file_merged)
+                data_files.append((target, files_merged))
diff --git a/setup.py b/setup.py
index b06761c..e21ac0c 100644
--- a/setup.py
+++ b/setup.py
@@ -3,13 +3,9 @@
 from distutils.core import setup
 import glob
 
-from DistUtilsExtra.command import (
-    build_i18n)
-
+import meld.build_helpers
 import meld.conf
 
-from meld.build_helpers import build_extra, build_help, build_icons
-
 
 setup(
     name=meld.conf.__package__,
@@ -45,9 +41,9 @@ setup(
          ),
     ],
     cmdclass={
-        "build": build_extra,
-        "build_i18n": build_i18n.build_i18n,
-        "build_help": build_help,
-        "build_icons": build_icons,
+        "build": meld.build_helpers.build_extra,
+        "build_i18n": meld.build_helpers.build_i18n,
+        "build_help": meld.build_helpers.build_help,
+        "build_icons": meld.build_helpers.build_icons,
     }
 )


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