# -*- coding: utf-8 -*- import logging import os import subprocess # Saving logs logging.basicConfig(level=logging.INFO) DECORATE_STR = "************************************" logging.info("TEST RESULTS FOR INTLTOOL\n{0}".format(DECORATE_STR)) # CONSTANTS PACKAGE_TO_TEST = "intltool" PYTHON_INTERPRETER = "python3" TEST_I18N_FILE = "test_intltool.py" LOG_INFO_PASS = "[ PASS ]" LOG_INFO_FAIL = "[ FAIL ]" LOG_INFO_SKIP = "[ SKIP ]" LOG_INFO_OS_ERROR = "[ OS Error ]" INTLTOOL_CMDS = [ 'intltool-extract', 'intltool-merge', 'intltool-prepare', 'intltool-update' ] TEST_FILES = [ 'cases/extract1.desktop', 'cases/extract7.xml', 'cases/space-preserve.xml.in', 'cases/spacepreserve.po', ] GENERATED_FILES = [ 'cases/extract1.desktop.h', 'cases/extract7.xml.h' ] TEMP_PO_FILE = 'cases/space-preserve.po' def _quick_clean(): [os.unlink(g_file) for g_file in GENERATED_FILES if os.path.exists(g_file)] if os.path.exists(TEMP_PO_FILE): subprocess.Popen( "mv {0} {1}".format(TEMP_PO_FILE, TEST_FILES[3]), shell=True ).communicate() def test_intltool_installed(): """ Test if intltool is installed """ subject = "GNU Intltool Installation Test" try: cmd_check_intltool = ['rpm', '-q', PACKAGE_TO_TEST] p1 = subprocess.Popen(cmd_check_intltool, stdout=subprocess.PIPE) std_data, stderr = p1.communicate() std_data = std_data.decode() logging.info("Found {0} NVR: {1}".format(PACKAGE_TO_TEST, std_data)) if PACKAGE_TO_TEST in std_data: logging.info("{0}: {1}\n".format(subject, LOG_INFO_PASS)) else: logging.error("{0}: {1}\n".format(subject, LOG_INFO_FAIL)) except OSError as e: logging.error("{0}: {1}\n".format(subject, LOG_INFO_OS_ERROR)) def test_intltool_extract(): """ Test intltool-extract command """ subject = "GNU Intltool Extract Command Test" try: cmd_intltool_extract_ini = \ "{0} --type=gettext/ini --quiet --update {1}".format(INTLTOOL_CMDS[0], TEST_FILES[0]) p1 = subprocess.Popen(cmd_intltool_extract_ini, shell=True) p1.communicate() cmd_intltool_extract_xml = \ "{0} --type=gettext/xml --quiet --update {1}".format(INTLTOOL_CMDS[0], TEST_FILES[1]) p2 = subprocess.Popen(cmd_intltool_extract_xml, shell=True) p2.communicate() if os.path.isfile(GENERATED_FILES[0]) and os.path.isfile(GENERATED_FILES[1]): logging.info("{0}: {1}\n".format(subject, LOG_INFO_PASS)) else: logging.error("{0}: {1}\n".format(subject, LOG_INFO_FAIL)) except OSError as e: logging.error("{0}: {1} - {2}\n".format(subject, LOG_INFO_OS_ERROR, e)) def test_intltool_merge(): """ Test intltool-extract command """ subject = "GNU Intltool Merge Command Test" if not os.path.exists(TEST_FILES[2]) and not os.path.exists(TEST_FILES[3]): logging.error("{0}: {1}\n".format(subject, LOG_INFO_SKIP)) return try: cmd_file_type = "file {0}".format(TEST_FILES[3]) p1 = subprocess.Popen(cmd_file_type, stdout=subprocess.PIPE, shell=True) std_data_p1, stderr_p1 = p1.communicate() file_type_before_merge = std_data_p1.decode() subprocess.Popen("cp {0} {1}".format(TEST_FILES[3], TEMP_PO_FILE), shell=True).communicate() cmd_intltool_merge = "{0} -x --quiet cases {1} {2}".format(INTLTOOL_CMDS[1], TEST_FILES[2], TEST_FILES[3]) p2 = subprocess.Popen(cmd_intltool_merge, shell=True) p2.communicate() p3 = subprocess.Popen(cmd_file_type, stdout=subprocess.PIPE, shell=True) std_data_p3, stderr_p3 = p3.communicate() file_type_after_merge = std_data_p3.decode() if 'GNU gettext message catalogue' in file_type_before_merge and \ 'XML 1.0 document' in file_type_after_merge: logging.info("{0}: {1}\n".format(subject, LOG_INFO_PASS)) else: logging.error("{0}: {1}\n".format(subject, LOG_INFO_FAIL)) except OSError as e: logging.error("{0}: {1} - {2}\n".format(subject, LOG_INFO_OS_ERROR, e)) if __name__ == "__main__": """ Entry point for intltool tests """ env_tests = [test_intltool_installed, test_intltool_extract, test_intltool_merge] # Execute tests [env_test() for env_test in env_tests] _quick_clean()