From 12a4e7d7e74e9d8b39a0509984910caa79a45e12 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Mon, 28 Mar 2016 20:15:16 +0300 Subject: [PATCH 1/2] Moved gettext into i18n module. --- mesonbuild/backend/ninjabackend.py | 22 +++++++------- mesonbuild/build.py | 6 ++++ mesonbuild/interpreter.py | 14 ++++----- mesonbuild/modules/i18n.py | 29 +++++++++++++++++++ test cases/frameworks/6 gettext/meson.build | 2 ++ .../frameworks/6 gettext/po/intltest.pot | 4 +-- .../frameworks/6 gettext/po/meson.build | 3 +- 7 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 mesonbuild/modules/i18n.py diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 8e8fa4285..2f2c70b6e 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -406,17 +406,16 @@ int dummy; def generate_po(self, outfile): for p in self.build.pot: - (packagename, languages, subdir) = p - input_file = os.path.join(subdir, 'POTFILES') + input_file = os.path.join(p.subdir, 'POTFILES') elem = NinjaBuildElement(self.all_outputs, 'pot', 'GEN_POT', []) - elem.add_item('PACKAGENAME', packagename) - elem.add_item('OUTFILE', packagename + '.pot') + elem.add_item('PACKAGENAME', p.packagename) + elem.add_item('OUTFILE', p.packagename + '.pot') elem.add_item('FILELIST', os.path.join(self.environment.get_source_dir(), input_file)) - elem.add_item('OUTDIR', os.path.join(self.environment.get_source_dir(), subdir)) + elem.add_item('OUTDIR', os.path.join(self.environment.get_source_dir(), p.subdir)) elem.write(outfile) - for l in languages: - infile = os.path.join(self.environment.get_source_dir(), subdir, l + '.po') - outfilename = os.path.join(subdir, l + '.gmo') + for l in p.languages: + infile = os.path.join(self.environment.get_source_dir(), p.subdir, l + '.po') + outfilename = os.path.join(p.subdir, l + '.gmo') lelem = NinjaBuildElement(self.all_outputs, outfilename, 'GEN_GMO', infile) lelem.add_item('INFILE', infile) lelem.add_item('OUTFILE', outfilename) @@ -482,11 +481,10 @@ int dummy; def generate_po_install(self, d, elem): for p in self.build.pot: - (package_name, languages, subdir) = p # FIXME: assumes only one po package per source - d.po_package_name = package_name - for lang in languages: - rel_src = os.path.join(subdir, lang + '.gmo') + d.po_package_name = p.packagename + for lang in p.languages: + rel_src = os.path.join(p.subdir, lang + '.gmo') src_file = os.path.join(self.environment.get_build_dir(), rel_src) d.po.append((src_file, self.environment.coredata.get_builtin_option('localedir'), lang)) elem.add_dep(rel_src) diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 3f480e870..d064dd725 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -1016,3 +1016,9 @@ class InstallScript: def __init__(self, cmd_arr): assert(isinstance(cmd_arr, list)) self.cmd_arr = cmd_arr + +class PoInfo(): + def __init__(self, packagename, languages, subdir): + self.packagename = packagename + self.languages = languages + self.subdir = subdir diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index ef6f3c101..88d4efb04 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -1039,6 +1039,10 @@ class Interpreter(): self.build.install_scripts.append(v) elif isinstance(v, build.Data): self.build.data.append(v) + elif isinstance(v, build.PoInfo): + if len(self.build.pot) > 0: + raise coredata.MesonException('More than one gettext definition currently not supported.') + self.build.pot.append(v) else: print(v) raise InterpreterException('Module returned a value of unknown type.') @@ -1269,15 +1273,7 @@ class Interpreter(): @stringArgs def func_gettext(self, nodes, args, kwargs): - if len(args) != 1: - raise InterpreterException('Gettext requires one positional argument (package name).') - packagename = args[0] - languages = kwargs.get('languages', None) - check_stringlist(languages, 'Argument languages must be a list of strings.') - # TODO: check that elements are strings - if len(self.build.pot) > 0: - raise InterpreterException('More than one gettext definition currently not supported.') - self.build.pot.append((packagename, languages, self.subdir)) + raise InterpreterException('Gettext() function has been moved to module i18n. Import it and use i18n.gettext() instead') def func_option(self, nodes, args, kwargs): raise InterpreterException('Tried to call option() in build description file. All options must be in the option file.') diff --git a/mesonbuild/modules/i18n.py b/mesonbuild/modules/i18n.py new file mode 100644 index 000000000..1467e7d5a --- /dev/null +++ b/mesonbuild/modules/i18n.py @@ -0,0 +1,29 @@ +# Copyright 2016 The Meson development team + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .. import coredata, mesonlib, build + +class I18nModule: + + def gettext(self, state, args, kwargs): + if len(args) != 1: + raise coredata.MesonException('Gettext requires one positional argument (package name).') + packagename = args[0] + languages = mesonlib.stringlistify(kwargs.get('languages', [])) + if len(languages) == 0: + raise coredata.MesonException('List of languages empty.') + return build.PoInfo(packagename, languages, state.subdir) + +def initialize(): + return I18nModule() diff --git a/test cases/frameworks/6 gettext/meson.build b/test cases/frameworks/6 gettext/meson.build index 4384978c7..6bba7e006 100644 --- a/test cases/frameworks/6 gettext/meson.build +++ b/test cases/frameworks/6 gettext/meson.build @@ -1,4 +1,6 @@ project('gettext example', 'c') +i18n = import('i18n') + subdir('po') subdir('src') diff --git a/test cases/frameworks/6 gettext/po/intltest.pot b/test cases/frameworks/6 gettext/po/intltest.pot index 05da67d57..d65e2c1d6 100644 --- a/test cases/frameworks/6 gettext/po/intltest.pot +++ b/test cases/frameworks/6 gettext/po/intltest.pot @@ -1,6 +1,6 @@ # SOME DESCRIPTIVE TITLE. # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER -# This file is distributed under the same license as the PACKAGE package. +# This file is distributed under the same license as the intltest package. # FIRST AUTHOR , YEAR. # #, fuzzy @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: intltest\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2013-09-12 19:04+0300\n" +"POT-Creation-Date: 2016-03-28 19:59+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" diff --git a/test cases/frameworks/6 gettext/po/meson.build b/test cases/frameworks/6 gettext/po/meson.build index 3376c84f6..8ea2c1113 100644 --- a/test cases/frameworks/6 gettext/po/meson.build +++ b/test cases/frameworks/6 gettext/po/meson.build @@ -1,4 +1,3 @@ langs = ['fi', 'de'] -gettext('intltest', -languages : langs) +i18n.gettext('intltest', languages : langs) From dc148e0702dc47197f36a843b499d3d513faf272 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Sat, 21 May 2016 18:21:23 +0300 Subject: [PATCH 2/2] Remove all special casing for gettext and use elementary operations instead. --- mesonbuild/backend/ninjabackend.py | 50 ---------------------- mesonbuild/build.py | 7 ---- mesonbuild/interpreter.py | 5 +-- mesonbuild/mesonmain.py | 3 ++ mesonbuild/modules/i18n.py | 17 +++++++- mesonbuild/scripts/gettext.py | 65 +++++++++++++++++++++++++++++ mesonbuild/scripts/meson_install.py | 14 ------- 7 files changed, 85 insertions(+), 76 deletions(-) create mode 100644 mesonbuild/scripts/gettext.py diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 2f2c70b6e..b6ce421d7 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -175,9 +175,6 @@ int dummy; self.generate_phony(outfile) outfile.write('# Build rules for targets\n\n') [self.generate_target(t, outfile) for t in self.build.get_targets().values()] - if len(self.build.pot) > 0: - outfile.write('# Build rules for localisation.\n\n') - self.generate_po(outfile) outfile.write('# Test rules\n\n') self.generate_tests(outfile) outfile.write('# Install rules\n\n') @@ -404,23 +401,6 @@ int dummy; elem.write(outfile) self.processed_targets[target.name + target.type_suffix()] = True - def generate_po(self, outfile): - for p in self.build.pot: - input_file = os.path.join(p.subdir, 'POTFILES') - elem = NinjaBuildElement(self.all_outputs, 'pot', 'GEN_POT', []) - elem.add_item('PACKAGENAME', p.packagename) - elem.add_item('OUTFILE', p.packagename + '.pot') - elem.add_item('FILELIST', os.path.join(self.environment.get_source_dir(), input_file)) - elem.add_item('OUTDIR', os.path.join(self.environment.get_source_dir(), p.subdir)) - elem.write(outfile) - for l in p.languages: - infile = os.path.join(self.environment.get_source_dir(), p.subdir, l + '.po') - outfilename = os.path.join(p.subdir, l + '.gmo') - lelem = NinjaBuildElement(self.all_outputs, outfilename, 'GEN_GMO', infile) - lelem.add_item('INFILE', infile) - lelem.add_item('OUTFILE', outfilename) - lelem.write(outfile) - def generate_coverage_rules(self, outfile): (gcovr_exe, lcov_exe, genhtml_exe) = environment.find_coverage_tools() added_rule = False @@ -471,7 +451,6 @@ int dummy; self.generate_header_install(d) self.generate_man_install(d) self.generate_data_install(d) - self.generate_po_install(d, elem) self.generate_custom_install_script(d) self.generate_subdir_install(d) elem.write(outfile) @@ -479,16 +458,6 @@ int dummy; ofile = open(install_data_file, 'wb') pickle.dump(d, ofile) - def generate_po_install(self, d, elem): - for p in self.build.pot: - # FIXME: assumes only one po package per source - d.po_package_name = p.packagename - for lang in p.languages: - rel_src = os.path.join(p.subdir, lang + '.gmo') - src_file = os.path.join(self.environment.get_build_dir(), rel_src) - d.po.append((src_file, self.environment.coredata.get_builtin_option('localedir'), lang)) - elem.add_dep(rel_src) - def generate_target_install(self, d): libdir = self.environment.get_libdir() bindir = self.environment.get_bindir() @@ -628,25 +597,6 @@ int dummy; outfile.write(" command = %s %s %s %s %s %s --backend ninja\n" % c) outfile.write(' description = Regenerating build files\n') outfile.write(' generator = 1\n\n') - if len(self.build.pot) > 0: - self.generate_gettext_rules(outfile) - outfile.write('\n') - - def generate_gettext_rules(self, outfile): - rule = 'rule GEN_POT\n' - command = " command = xgettext --package-name=$PACKAGENAME -p $OUTDIR -f $FILELIST -D '%s' -k_ -o $OUTFILE\n" % \ - self.environment.get_source_dir() - desc = " description = Creating pot file for package $PACKAGENAME.\n" - outfile.write(rule) - outfile.write(command) - outfile.write(desc) - outfile.write('\n') - rule = 'rule GEN_GMO\n' - command = ' command = msgfmt $INFILE -o $OUTFILE\n' - desc = ' description = Generating gmo file $OUTFILE\n' - outfile.write(rule) - outfile.write(command) - outfile.write(desc) outfile.write('\n') def generate_phony(self, outfile): diff --git a/mesonbuild/build.py b/mesonbuild/build.py index d064dd725..93e20c87e 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -95,7 +95,6 @@ class Build: self.data = [] self.static_linker = None self.static_cross_linker = None - self.pot = [] self.subprojects = {} self.install_scripts = [] self.postconf_scripts = [] @@ -1016,9 +1015,3 @@ class InstallScript: def __init__(self, cmd_arr): assert(isinstance(cmd_arr, list)) self.cmd_arr = cmd_arr - -class PoInfo(): - def __init__(self, packagename, languages, subdir): - self.packagename = packagename - self.languages = languages - self.subdir = subdir diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 88d4efb04..b860ec3f9 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -1036,13 +1036,10 @@ class Interpreter(): raise InterpreterException('Tried to create target %s which already exists.' % v.name) self.build.targets[v.name] = v elif isinstance(v, build.InstallScript): + print('x') self.build.install_scripts.append(v) elif isinstance(v, build.Data): self.build.data.append(v) - elif isinstance(v, build.PoInfo): - if len(self.build.pot) > 0: - raise coredata.MesonException('More than one gettext definition currently not supported.') - self.build.pot.append(v) else: print(v) raise InterpreterException('Module returned a value of unknown type.') diff --git a/mesonbuild/mesonmain.py b/mesonbuild/mesonmain.py index 22b52e388..c53c9d185 100644 --- a/mesonbuild/mesonmain.py +++ b/mesonbuild/mesonmain.py @@ -206,6 +206,9 @@ def run_script_command(args): elif cmdname == 'vcstagger': import mesonbuild.scripts.vcstagger as abc cmdfunc = abc.run + elif cmdname == 'gettext': + import mesonbuild.scripts.gettext as abc + cmdfunc = abc.run else: raise MesonException('Unknown internal command {}.'.format(cmdname)) return cmdfunc(cmdargs) diff --git a/mesonbuild/modules/i18n.py b/mesonbuild/modules/i18n.py index 1467e7d5a..51668cbaa 100644 --- a/mesonbuild/modules/i18n.py +++ b/mesonbuild/modules/i18n.py @@ -13,6 +13,7 @@ # limitations under the License. from .. import coredata, mesonlib, build +import sys class I18nModule: @@ -23,7 +24,21 @@ class I18nModule: languages = mesonlib.stringlistify(kwargs.get('languages', [])) if len(languages) == 0: raise coredata.MesonException('List of languages empty.') - return build.PoInfo(packagename, languages, state.subdir) + potargs = [state.environment.get_build_command(), '--internal', 'gettext', 'pot', packagename] + pottarget = build.RunTarget(packagename + '-pot', sys.executable, potargs, state.subdir) + gmoargs = [state.environment.get_build_command(), '--internal', 'gettext', 'gen_gmo'] + languages + gmotarget = build.RunTarget(packagename + '-gmo', sys.executable, gmoargs, state.subdir) + installcmd = [sys.executable, + state.environment.get_build_command(), + '--internal', + 'gettext', + 'install', + state.subdir, + packagename, + state.environment.coredata.get_builtin_option('localedir'), + ] + languages + iscript = build.InstallScript(installcmd) + return [pottarget, gmotarget, iscript] def initialize(): return I18nModule() diff --git a/mesonbuild/scripts/gettext.py b/mesonbuild/scripts/gettext.py new file mode 100644 index 000000000..adc448327 --- /dev/null +++ b/mesonbuild/scripts/gettext.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +# Copyright 2016 The Meson development team + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os, subprocess, shutil + +def run_potgen(src_sub, pkgname, langs): + listfile = os.path.join(src_sub, 'POTFILES') + ofile = os.path.join(src_sub, pkgname + '.pot') + return subprocess.call(['xgettext', '--package-name=' + pkgname, '-p', src_sub, listfile, + '-D', os.environ['MESON_SOURCE_ROOT'], '-k_', '-o', ofile]) + +def gen_gmo(src_sub, bld_sub, langs): + for l in langs: + subprocess.check_call(['msgfmt', os.path.join(src_sub, l + '.po'), + '-o', os.path.join(bld_sub, l + '.gmo')]) + return 0 + +def do_install(src_sub, bld_sub, dest, pkgname, langs): + for l in langs: + srcfile = os.path.join(bld_sub, l + '.gmo') + outfile = os.path.join(dest, l, 'LC_MESSAGES', + pkgname + '.mo') + os.makedirs(os.path.split(outfile)[0], exist_ok=True) + shutil.copyfile(srcfile, outfile) + shutil.copystat(srcfile, outfile) + print('Installing %s to %s.' % (srcfile, outfile)) + return 0 + +def run(args): + subcmd = args[0] + if subcmd == 'pot': + src_sub = os.path.join(os.environ['MESON_SOURCE_ROOT'], os.environ['MESON_SUBDIR']) + bld_sub = os.path.join(os.environ['MESON_BUILD_ROOT'], os.environ['MESON_SUBDIR']) + return run_potgen(src_sub, args[1], args[2:]) + elif subcmd == 'gen_gmo': + src_sub = os.path.join(os.environ['MESON_SOURCE_ROOT'], os.environ['MESON_SUBDIR']) + bld_sub = os.path.join(os.environ['MESON_BUILD_ROOT'], os.environ['MESON_SUBDIR']) + return gen_gmo(src_sub, bld_sub, args[1:]) + elif subcmd == 'install': + subdir = args[1] + pkgname = args[2] + instsubdir = args[3] + langs = args[4:] + src_sub = os.path.join(os.environ['MESON_SOURCE_ROOT'], subdir) + bld_sub = os.path.join(os.environ['MESON_BUILD_ROOT'], subdir) + dest = os.environ.get('DESTDIR') + os.path.join(os.environ['MESON_INSTALL_PREFIX'], instsubdir) + if gen_gmo(src_sub, bld_sub, langs) != 0: + return 1 + do_install(src_sub, bld_sub, dest, pkgname, langs) + else: + print('Unknown subcommand.') + return 1 diff --git a/mesonbuild/scripts/meson_install.py b/mesonbuild/scripts/meson_install.py index 8e3d0ca0b..765969db5 100644 --- a/mesonbuild/scripts/meson_install.py +++ b/mesonbuild/scripts/meson_install.py @@ -32,7 +32,6 @@ def do_install(datafilename): install_headers(d) install_man(d) install_data(d) - install_po(d) run_install_script(d) def install_subdirs(d): @@ -50,19 +49,6 @@ def install_subdirs(d): shutil.copytree(src_dir, final_dst, symlinks=True) print('Installing subdir %s to %s.' % (src_dir, dst_dir)) -def install_po(d): - packagename = d.po_package_name - for f in d.po: - srcfile = f[0] - localedir = f[1] - languagename = f[2] - outfile = os.path.join(d.fullprefix, localedir, languagename, 'LC_MESSAGES', - packagename + '.mo') - os.makedirs(os.path.split(outfile)[0], exist_ok=True) - shutil.copyfile(srcfile, outfile) - shutil.copystat(srcfile, outfile) - print('Installing %s to %s.' % (srcfile, outfile)) - def install_data(d): for i in d.data: fullfilename = i[0]