Commits

Elson Wei  committed 858cd04

setup: add 'import_po' command to import the i18n

--package,-p specify the package, launchpad export package and bzr repo
are supported, default: launchpad-export.tar.gz

--lang,-l specify the languages to be imported, separated by ';'

Examples:
1. import all languages from launchpad-export.tar.gz
% setup.py import_po
2. import all languages from bzr repo
% setup.py import_po -p path/to/stable-po
3. import ja, zh_tw from file.tar.gz
% setup.py import_po -p path/to/file.tar.gz -l ja;zh_tw

  • Participants
  • Parent commits 8e2aba7
  • Branches stable

Comments (0)

Files changed (1)

 import cgi
 import tempfile
 import re
+import tarfile
 from fnmatch import fnmatch
 from distutils import log
 from distutils.core import setup, Command
             self.mkpath(modir)
             open(mofile, "wb").write(modata)
 
+class import_po(Command):
+
+    description = "import translations (.po file)"
+
+    user_options = [("package=", "p", "launchpad export package or bzr repo "
+                     "[defualt: launchpad-export.tar.gz]"),
+                    ("lang=", "l",
+                     "languages to be imported, separated by ';'")
+                    ]
+
+    def initialize_options(self):
+        self.package = None
+        self.lang = None
+
+    def finalize_options(self):
+        if not self.package:
+            self.package = 'launchpad-export.tar.gz'
+
+        if self.lang:
+            self.lang = self.lang.upper().split(';')
+
+    def _untar(self, name, path='.'):
+        tar = tarfile.open(name, 'r')
+        path = os.path.abspath(path)
+        for tarinfo in tar.getmembers():
+            # Extract the safe file only
+            p = os.path.abspath(os.path.join(path, tarinfo.name))
+            if p.startswith(path):
+                tar.extract(tarinfo, path)
+        tar.close()
+
+    def run(self):
+        if not find_executable('msgcat'):
+            self.warn("could not find msgcat executable")
+            return
+
+        dest_prefix = 'i18n/tortoisehg'
+        src_prefix = 'po/tortoisehg'
+
+        log.info('import from %s' % self.package)
+
+        if os.path.isdir(self.package):
+            self.bzrrepo = True
+            self.package_path = self.package
+        elif tarfile.is_tarfile(self.package):
+            self.bzrrepo = False
+            self.package_path = tempfile.mkdtemp()
+            self._untar(self.package, self.package_path)
+        else:
+            self.warn('%s is not a valid tranlation package' % self.package)
+            return
+
+        if self.bzrrepo:
+            filter = r'^([\S]+).po$'
+        else:
+            filter = r'^tortoisehg-([\S]+).po$'
+        r = re.compile(filter)
+
+        src_dir = os.path.join(self.package_path, src_prefix)
+        for src_file in os.listdir(src_dir):
+            m = r.match(src_file)
+            if not m:
+                continue
+
+            # filter the language
+            lang = m.group(1)
+            if self.lang and lang.upper() not in self.lang:
+                continue
+
+            dest_file = join(dest_prefix, lang) + '.po'
+            msg = 'updating %s...' % dest_file
+            cmd = ['msgcat',
+                   '--no-location',
+                   '-o', dest_file,
+                   os.path.join(src_dir, src_file)
+                   ]
+            self.execute(spawn, (cmd,), msg)
+
+        if not self.bzrrepo:
+            shutil.rmtree(self.package_path)
+
 class update_pot(Command):
 
     description = "extract translatable strings to tortoisehg.pot"
         'clean': clean,
         'clean_local': clean_local,
         'update_pot': update_pot ,
+        'import_po': import_po
     }
 
 def setup_windows(version):