Skip to content

Commit

Permalink
qilinguist release: fail when non-translated messages are found
Browse files Browse the repository at this point in the history
Implements #27606

Change-Id: I968e097e86b191d7a5bdc986f4a94b2c8bf02ef8
Reviewed-on: http://gerrit.aldebaran.lan/51691
Tested-by: gerrit
Reviewed-by: dmerejkowsky <[email protected]>
  • Loading branch information
dmerejkowsky committed Feb 3, 2015
1 parent 47640bc commit a05d558
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
2 changes: 1 addition & 1 deletion python/qibuild/test/projects/translateme/qt/qiproject.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<qibuild name="translateme-qt" />
<translate
name="helloqt"
linguas="fr_FR en_US"
linguas="fr_FR"
tr="linguist"/>
</project>
12 changes: 11 additions & 1 deletion python/qilinguist/pml_translator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os

from qisys import ui
import qisys.command
import qisys.qixml

import qilinguist.qtlinguist

def new_pml_translator(pml_path):
return PMLTranslator(pml_path)

Expand All @@ -16,14 +19,21 @@ def __init__(self, pml_path):
def update(self):
raise NotImplementedError()

def release(self):
def release(self, raises=True):
all_ok = True
for ts_file in self.ts_files:
qm_file = ts_file.replace(".ts", ".qm")
input = os.path.join(self.path, ts_file)
output = os.path.join(self.path, qm_file)
ok, message = qilinguist.qtlinguist.generate_qm_file(input, output)
if not ok:
ui.error(message)
all_ok = False
cmd = ["lrelease", "-compress", input, "-qm", output]
qisys.command.call(cmd)
self.qm_files.append(output)
if not all_ok and raises:
raise Exception("`qilinguist release` failed")

def install(self, dest):
translations_dest = os.path.join(dest, "translations")
Expand Down
24 changes: 20 additions & 4 deletions python/qilinguist/qigettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,21 @@ def update(self):
# generate PO file if it doesn't exist
self.generate_po_file(locale)

def release(self):
""" Compile every catalog """
def release(self, raises=True):
""" Compile every catalog.
"""
mo_output_dir = os.path.join(self.path, "po",
"share", "locale", self.name)
qisys.sh.mkdir(mo_output_dir, recursive=True)
all_ok = True
for locale in self.linguas:
self.generate_mo_file(locale)
ok, message = self.generate_mo_file(locale)
if not ok:
ui.error(message)
all_ok = all_ok and ok
if not all_ok and raises:
raise Exception("`qilinguist release` failed")

def extract_pot_file(self):
"""Extract sentence from source file and generate POT file"""
Expand Down Expand Up @@ -167,7 +175,15 @@ def generate_mo_file(self, locale):
cmd.extend(["--directory", self.po_path])
cmd.append(input_file)

qisys.command.call(cmd)
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = process.communicate()
ui.info(err.strip())
if "untranslated" in err:
return False, "Some untranslated messages were found"
if process.returncode != 0:
return False, "msgfmt failed"
return True, ""

def install(self, destination):
full_dest = os.path.join(destination, "share", "locale")
Expand Down
31 changes: 26 additions & 5 deletions python/qilinguist/qtlinguist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"""

import os
import subprocess

from qisys import ui
import qisys.command
import qilinguist.project
Expand All @@ -26,8 +28,8 @@ def update(self):
cmd.extend(output_files)
qisys.command.call(cmd, cwd=self.path)


def release(self):
def release(self, raises=True):
all_ok = True
for locale in self.linguas:
input_file = os.path.join(self.po_path, locale + ".ts")
if not os.path.exists(input_file):
Expand All @@ -37,9 +39,12 @@ def release(self):
continue
output_file = os.path.join(self.po_path,
self.name + "_" + locale + ".qm")
cmd = ["lrelease", "-compress",
input_file, "-qm", output_file]
qisys.command.call(cmd, cwd=self.path)
ok, message = generate_qm_file(input_file, output_file)
if not ok:
all_ok = False
ui.error(message)
if not all_ok and raises:
raise Exception("`qilinguist release` failed")

def install(self, destination):
full_dest = os.path.join(destination, "share", "locale")
Expand All @@ -49,3 +54,19 @@ def filter(f):

def __repr__(self):
return "<QtLinguistProject %s in %s>" % (self.name, self.path)

def generate_qm_file(input, output):
""" Generate a .qm file from a .ts file.
Returns (True, "") if everything went well,
(False, "<error message>") otherwise
"""
cmd = ["lrelease", "-compress", input, "-qm", output]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = process.communicate()
ui.info(out.strip())
if process.returncode != 0:
return False, "lrelease failed"
if "untranslated" in out:
return False, "untranslated messages were found"
return True, ""
1 change: 1 addition & 0 deletions python/qilinguist/test/test_gettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_release(qilinguist_action):
assert not os.path.exists(fr_FR_mo_file)
assert not os.path.exists(en_US_mo_file)
qilinguist_action("update", "translate")
qilinguist_action.create_po(trad)
qilinguist_action("release", "translate")
assert os.path.exists(fr_FR_mo_file)
assert os.path.exists(en_US_mo_file)
Expand Down
36 changes: 36 additions & 0 deletions python/qilinguist/test/test_qilinguist_release.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import os

import qisys.script

from qibuild.test.conftest import TestBuildWorkTree

import pytest

def test_pml_outside_worktree(tmpdir, monkeypatch):
Expand Down Expand Up @@ -39,3 +43,35 @@ def test_raise_when_no_project_given_outside_a_worktree(tmpdir, monkeypatch):
with pytest.raises(Exception) as e:
qisys.script.run_action("qilinguist.actions.release")
assert "outside a worktree" in e.value.message

def test_non_translated_messages_gettext(qilinguist_action, record_messages):
trad_project = qilinguist_action.trad
qilinguist_action.create_po(trad_project)
main_cpp = os.path.join(trad_project.path, "main.cpp")
with open(main_cpp, "a") as fp:
fp.write("""
char* foo() {
return _("Hello, world");
}
""")
qilinguist_action("update", "translate")
qilinguist_action("release", "translate", raises=True)
assert record_messages.find("untranslated")

def test_non_translated_messages_qt(qilinguist_action):
build_worktree = TestBuildWorkTree()
project = build_worktree.add_test_project("translateme/qt")
qilinguist_action("update", "helloqt")
qilinguist_action("release", "helloqt", raises=True)

def test_invalid_po_file(qilinguist_action):
trad_project = qilinguist_action.trad
qilinguist_action.create_po(trad_project)
fr_FR_po = os.path.join(trad_project.path, "po", "fr_FR.po")
with open(fr_FR_po, "a") as fp:
fp.write("""
#: broken
syntax-error
""")
error = qilinguist_action("release", "translate", raises=True)
assert "failed" in error

0 comments on commit a05d558

Please sign in to comment.