diff --git a/packaging/univention-ucs-translation-template/test0.py b/packaging/univention-ucs-translation-template/test0.py index 4f63745..a80c40c 100755 --- a/packaging/univention-ucs-translation-template/test0.py +++ b/packaging/univention-ucs-translation-template/test0.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +from distutils.file_util import copy_file from hashlib import md5 import logging import os @@ -45,7 +46,7 @@ def _change_entry_in_source_file(module_path, po_entry): line = line.replace(po_entry.msgid, 'TEST! {}'.format(po_entry.msgid)) changed_js.write(line) - +SVN = 'SVN_TEST0' TRANSLATION_PKG_NAME = 'univention-ucs-translation-XX' # TODO: get latest public branch if __name__ == '__main__': @@ -57,23 +58,28 @@ if __name__ == '__main__': pass try: - tools.call('svn', 'checkout', 'http://forge.univention.org/svn/dev/branches/ucs-4.1/ucs-4.1-1/management/univention-management-console-module-passwordchange/', 'svn_repo/management/univention-management-console-module-passwordchange') - tools.call('univention-ucs-translation-build-package', '--source=svn_repo', '--languagecode=XX', '--locale=fr_FR.UTF-8:UTF-8', '--languagename=TEST0') + tools.call('svn', 'checkout', 'http://forge.univention.org/svn/dev/branches/ucs-4.1/ucs-4.1-1', SVN) + tools.call('univention-ucs-translation-build-package', '--source={}'.format(SVN), '--languagecode=XX', '--locale=fr_FR.UTF-8:UTF-8', '--languagename=TEST0') tools.call('univention-ucs-translation-fakemessage', TRANSLATION_PKG_NAME) except InvalidCommandError: print('Error: Tried to launch invalid command. Exiting.') sys.exit(1) # Choose js files to manipulate - js_po_files = tools.get_matching_file_paths(TRANSLATION_PKG_NAME, '*umc*js*.po') - choosen_po_path = random.choice(js_po_files) - choosen_po_pre_changes_path = '{}.pre-change'.format(choosen_po_path) - shutil.copy(choosen_po_path, choosen_po_pre_changes_path) - module_path = '/'.join(choosen_po_path.split('/')[2:4]) - choosen_po = polib.pofile(choosen_po_path) - random_entry = random.choice(choosen_po) - - _change_entry_in_source_file(module_path, random_entry) + changes = list() + for file_path_pattern in ('*umc*js*.po', '*umc*python*.po'): + po_paths = tools.get_matching_file_paths(TRANSLATION_PKG_NAME, '*umc*js*.po') + + # po_path, module_path, random_entry + for i in range(3): + random_po_path = po_paths.pop(random.choice(po_paths)) + # random_po_paths.append(random_po_path) + copy_file(random_po_path, '{}.pre_change'.format(random_po_path)) + module_path = '/'.join(random_po_path.split('/')[2:4]) + random_po = polib.pofile(random_po_path) + random_entry = random.choice(random_po) + _change_entry_in_source_file(module_path, random_entry) + tools.call('univention-ucs-translation-merge', 'XX', 'svn_repo', TRANSLATION_PKG_NAME) if not _entry_is_fuzzy(random_entry, choosen_po_path): diff --git a/packaging/univention-ucs-translation-template/test1.py b/packaging/univention-ucs-translation-template/test1.py new file mode 100644 index 0000000..e5063ee --- /dev/null +++ b/packaging/univention-ucs-translation-template/test1.py @@ -0,0 +1,81 @@ +import helper +import os +import sys +import shutil +from hashlib import md5 +from difflib import context_diff +from distutils.dir_util import copy_tree +from distutils.file_util import copy_file + +from pdb import set_trace as dbg + +SVN_PATH = 'SVN_REPO' +DUMMY_MOD_DIR = 'management/univention-management-console-module-dummy' +DUMMY_MOD_EXPECTED_PO_PATHS = [ + 'univention-ucs-translation-XX/XX/management/univention-management-console-module-dummy/umc/python/dummy/XX.po', + 'univention-ucs-translation-XX/XX/management/univention-management-console-module-dummy/umc/XX.po', + 'univention-ucs-translation-XX/XX/management/univention-management-console-module-dummy/umc/js/XX.po' +] + +MAKEFILE_EXPECTED_DIFF = [ + '+ \t$(DESTDIR)/usr/share/locale/XX/LC_MESSAGES/univention-management-console-module-dummy.mo \\\n', + '+ \t$(DESTDIR)/usr/share/univention-management-console-frontend/js/umc/modules/i18n/XX/dummy.json \\\n', + '+ \t$(DESTDIR)/usr/share/univention-management-console/i18n/XX/dummy.mo \\\n', + '+ $(DESTDIR)/usr/share/locale/XX/LC_MESSAGES/univention-management-console-module-dummy.mo: XX/management/univention-management-console-module-dummy/umc/python/dummy/XX.po\n', + '+ $(DESTDIR)/usr/share/univention-management-console-frontend/js/umc/modules/i18n/XX/dummy.json: XX/management/univention-management-console-module-dummy/umc/js/XX.po\n', + '+ $(DESTDIR)/usr/share/univention-management-console/i18n/XX/dummy.mo: XX/management/univention-management-console-module-dummy/umc/XX.po\n' +] + +TRANSLATION_PKG_NAME = 'univention-ucs-translation-XX' +if __name__ == '__main__': + # TODO: run whole test in tmp dir + try: + shutil.rmtree(SVN_PATH+'/management/univention-management-console-module-dummy') + shutil.rmtree(TRANSLATION_PKG_NAME) + except Exception: + pass + + try: + helper.call('svn', 'checkout', 'http://forge.univention.org/svn/dev/branches/ucs-4.1/ucs-4.1-1', SVN_PATH) + helper.call('univention-ucs-translation-build-package', '--source=' + SVN_PATH, '--languagecode=XX', '--locale=fr_FR.UTF-8:UTF-8', '--languagename=TEST0') + helper.call('univention-ucs-translation-fakemessage', TRANSLATION_PKG_NAME) + except helper.InvalidCommandError: + print('Error: Tried to launch invalid command. Exiting.') + sys.exit(1) + copy_file(os.path.join(TRANSLATION_PKG_NAME, 'all_targets.mk'), 'all_targets.mk.pre_merge') + + # Add dummy module with new translations + copy_tree('./dummy_module', SVN_PATH) + helper.call('univention-ucs-translation-merge', 'XX', SVN_PATH, TRANSLATION_PKG_NAME) + helper.call('univention-ucs-translation-fakemessage', TRANSLATION_PKG_NAME) + + translation_tree_path = os.path.join(TRANSLATION_PKG_NAME, 'XX', DUMMY_MOD_DIR) + new_po_paths = helper.get_matching_file_paths(translation_tree_path, '*.po') + if not set(new_po_paths) == set(DUMMY_MOD_EXPECTED_PO_PATHS): + print('Test: Failed') + sys.exit(1) + with open('all_targets.mk.pre_merge', 'r') as pre, open(os.path.join(TRANSLATION_PKG_NAME, 'all_targets.mk')) as after: + diff = [line for line in context_diff(pre.readlines(), after.readlines()) if line.startswith('+ ')] + + if set(diff) != set(MAKEFILE_EXPECTED_DIFF): + dbg() + print('Test: Failed. Diff didn\'t yield expected result.') + print(diff) + sys.exit(1) + + shutil.rmtree(SVN_PATH+'/management/univention-management-console-module-dummy') + helper.call('univention-ucs-translation-merge', 'XX', SVN_PATH, TRANSLATION_PKG_NAME) + + # Files obsoleted upstream detected? + new_po_paths = helper.get_matching_file_paths(translation_tree_path, '*.obsolete') + expected_obsoleted_po_paths = ['{}.obsolete'.format(path) for path in DUMMY_MOD_EXPECTED_PO_PATHS] + if set(new_po_paths) != set(expected_obsoleted_po_paths): + print('Test: Failed. Merge didn\'t detect obsoleted po files.') + #sys.exit(1) + + # Makefile should ne the same as before adding the dummy module + with open('all_targets.mk.pre_merge', 'rb') as pre_change, open(os.path.join(TRANSLATION_PKG_NAME, 'all_targets.mk'), 'rb') as after: + if md5(pre_change.read()).hexdigest() != md5(after.read()).hexdigest(): + dbg() + print('Test: Failed! Makefile was changed.') + sys.exit(1) diff --git a/packaging/univention-ucs-translation-template/tools.py b/packaging/univention-ucs-translation-template/tools.py new file mode 100644 index 0000000..e3f624b --- /dev/null +++ b/packaging/univention-ucs-translation-template/tools.py @@ -0,0 +1,66 @@ +import subprocess +import fnmatch +import os +import polib +import logging + + +class InvalidCommandError(Exception): + pass + + +def get_matching_file_paths(path, pattern): + matched_files_paths = list() + for dirname, dns, fnames in os.walk(path): + for fn in fnames: + matched_files_paths.append(os.path.join(dirname, fn)) + return fnmatch.filter(matched_files_paths, pattern) + + +def call(*command_parts): + if not command_parts: + raise InvalidCommandError() + try: + subprocess.check_call([part for part in command_parts]) + except subprocess.CalledProcessError as exc: + print('Error: Subprocess exited unsuccessfully. Attempted command:') + print(' '.join(exc.cmd)) + raise InvalidCommandError() + except AttributeError as exc: + print('Command must be a string like object.') + raise InvalidCommandError() + except OSError as exc: + print('Error: Command exited unsuccessfully. Operating System error during command execution.') + print('Error: {}'.format(exc.strerror)) + raise InvalidCommandError() + + +#def _entry_is_fuzzy(changed_entry, po_file_path): +# po_file = polib.pofile(po_file_path) +# found_change = False +# for fuzzy in po_file.fuzzy_entries(): +# if fuzzy.occurrences == changed_entry.occurrences: +# found_change = True +# return found_change +# +# +#def _remove_fuzzy_flags(po_file_path): +# po_file = polib.pofile(po_file_path) +# for fuzzy_entry in po_file.fuzzy_entries(): +# fuzzy_entry.flags.remove('fuzzy') +# po_file.save() +# +# +#SVN_PATH = 'SVN_REPO' +#def _change_entry_in_source_file(module_path, po_entry): +# for source_file, line_number in po_entry.occurrences: +# source_file = os.path.join(SVN_PATH, module_path, source_file) +# original_source_file = '{}.orig'.format(source_file) +# os.rename(source_file, original_source_file) +# with open(source_file, 'w') as changed_js: +# with open(original_source_file, 'r') as fd: +# for i, line in enumerate(fd): +# if i == int(line_number) - 1: +# logging.info('Changing {} in line {}'.format(source_file, line_number)) +# line = line.replace(po_entry.msgid, 'TEST! {}'.format(po_entry.msgid)) +# changed_js.write(line)