[geany/www.geany.org] c3d138: Rework translation statistic update to operate on a Geany source tarball

Enrico Tröger git-noreply at xxxxx
Tue May 7 21:48:37 UTC 2019


Branch:      refs/heads/master
Author:      Enrico Tröger <enrico.troeger at uvena.de>
Committer:   Enrico Tröger <enrico.troeger at uvena.de>
Date:        Tue, 07 May 2019 21:48:37 UTC
Commit:      c3d138d1eb5e27a267deb0a91cde2d604f636aab
             https://github.com/geany/www.geany.org/commit/c3d138d1eb5e27a267deb0a91cde2d604f636aab

Log Message:
-----------
Rework translation statistic update to operate on a Geany source tarball


Modified Paths:
--------------
    README.dev.md
    geany/settings.py
    static_docs/generate_i18n_statistics.py
    static_docs/management/commands/generate_i18n_statistics.py

Modified: README.dev.md
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -103,7 +103,7 @@ the settings to your needs:
 
     LOGGING['handlers']['file']['filename'] = '/tmp/geany_django.log'
 
-    STATIC_DOCS_GEANY_SOURCE_DIR = '/path/to/geany/source/or/just/empty'
+    STATIC_DOCS_GEANY_SOURCE_TARBALL = '/path/to/geany/source/tarball/or/just/empty'
     IRC_USER_LIST_FILE = '/path/to/irc/data/or/just/empty'
 
 


Modified: geany/settings.py
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -448,7 +448,7 @@
 
 NIGHTLYBUILDS_BASE_DIR = '/path/to/nightly/builds'
 
-STATIC_DOCS_GEANY_SOURCE_DIR = '/home/geany/geany/geany_git/po'
+STATIC_DOCS_GEANY_SOURCE_TARBALL = '/srv/www/download.geany.org/geany_git.tar.gz'
 STATIC_DOCS_GEANY_DESTINATION_DIR = os.path.join(MEDIA_ROOT, 'i18n')
 STATIC_DOCS_GEANY_DESTINATION_URL = os.path.join(MEDIA_URL, 'i18n')
 STATIC_DOCS_GEANY_I18N_STATISTICS_FILENAME = 'i18n_statistics.json'


Modified: static_docs/generate_i18n_statistics.py
48 lines changed, 40 insertions(+), 8 deletions(-)
===================================================================
@@ -13,9 +13,11 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from json import dump, JSONEncoder
-from os import listdir
+from os import listdir, makedirs
 from os.path import join, splitext
+from shutil import rmtree
 from subprocess import CalledProcessError, check_output, STDOUT
+from tempfile import TemporaryDirectory
 from time import time
 import re
 
@@ -67,18 +69,22 @@ def default(self, o):  # pylint: disable=method-hidden
 class TranslationStatisticsGenerator:
 
     # ----------------------------------------------------------------------
-    def __init__(self, domain, source_path, destination_path, target_filename):
+    def __init__(self, domain, source_tarball, destination_path, target_filename):
         self._domain = domain
-        self._source_path = source_path
+        self._source_tarball = source_tarball
         self._destination_path = destination_path
         self._target_filename = target_filename
+        self._temp_path = None
+        self._source_path = None
         self._pot_stats = None
         self._message_catalogs = None
         self._message_catalog = None
         self._overall_statistics = None
 
     # ----------------------------------------------------------------------
     def generate(self):
+        self._create_destionation_path_if_necessary()
+        self._extract_geany_source_tarball()
         self._update_pot_file()
         self._fetch_pot_stats()
         self._fetch_message_catalogs()
@@ -88,6 +94,25 @@ def generate(self):
 
         self._factor_overall_statistics()
         self._write_overall_statistics()
+        self._remove_extracted_geany_source()
+
+    # ----------------------------------------------------------------------
+    def _create_destionation_path_if_necessary(self):
+        makedirs(self._destination_path, mode=0o755, exist_ok=True)
+
+    # ----------------------------------------------------------------------
+    def _extract_geany_source_tarball(self):
+        self._temp_path = TemporaryDirectory()
+        self._source_path = join(self._temp_path.name, 'po')
+
+        extract_command = [
+            'tar',
+            '--extract',
+            '--strip-components', '1',
+            '--directory', self._temp_path.name,
+            '--file', self._source_tarball,
+        ]
+        self._execute_command(extract_command)
 
     # ----------------------------------------------------------------------
     def _update_pot_file(self):
@@ -111,28 +136,31 @@ def _execute_command(self, command):
             srcdir=self._source_path,
             LANG='C')
         try:
-            return check_output(
+            output = check_output(
                 command,
                 env=environment,
                 cwd=self._destination_path,
                 stderr=STDOUT)
+            output_utf8 = output.decode('utf-8')
+            return output_utf8
         except CalledProcessError as exc:
             raise ValueError(
                 'Command: "{}" exited with code {}: {}'.format(
-                    command,
+                    ' '.join(command),
                     exc.returncode,
-                    exc.output))
+                    exc.output.decode('utf-8')))
 
     # ----------------------------------------------------------------------
     def _fetch_pot_stats(self):
-        self._pot_stats = self._read_po_translation_statistics(self._factor_pot_filename())
+        pot_filename = self._factor_pot_filename()
+        self._pot_stats = self._read_po_translation_statistics(pot_filename)
 
     # ----------------------------------------------------------------------
     def _read_po_translation_statistics(self, filename):
         msgfmt_command = ['msgfmt', '--statistics', filename]
         output = self._execute_command(msgfmt_command)
         # parse
-        match = STATISTICS_REGEXP.match(output.decode('utf-8'))
+        match = STATISTICS_REGEXP.match(output)
         if match:
             translated = match.group('translated')
             fuzzy = match.group('fuzzy')
@@ -217,3 +245,7 @@ def _write_overall_statistics(self):
         output_filename = join(self._destination_path, self._target_filename)
         with open(output_filename, 'w') as output_file:
             dump(self._overall_statistics, output_file, cls=SimpleObjectToJSONEncoder)
+
+    # ----------------------------------------------------------------------
+    def _remove_extracted_geany_source(self):
+        self._temp_path.cleanup()


Modified: static_docs/management/commands/generate_i18n_statistics.py
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -25,7 +25,7 @@ class Command(BaseCommand):
     def handle(self, *args, **options):
         generator = TranslationStatisticsGenerator(
             'geany',
-            settings.STATIC_DOCS_GEANY_SOURCE_DIR,
+            settings.STATIC_DOCS_GEANY_SOURCE_TARBALL,
             settings.STATIC_DOCS_GEANY_DESTINATION_DIR,
             settings.STATIC_DOCS_GEANY_I18N_STATISTICS_FILENAME,
         )



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list