summaryrefslogtreecommitdiff
path: root/SCons/Tool/msgfmt.py
blob: a15c1dcc02fc076f13f64b71ee0f973aecf7ad7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# MIT License
#
# Copyright The SCons Foundation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

""" msgfmt tool """

import sys
import os

import SCons.Action
import SCons.Tool
import SCons.Util
import SCons.Warnings
from SCons.Builder import BuilderBase
from SCons.Errors import StopError
from SCons.Platform.cygwin import CYGWIN_DEFAULT_PATHS
from SCons.Platform.mingw import MINGW_DEFAULT_PATHS
from SCons.Tool.GettextCommon import (
    _detect_msgfmt,
    _msgfmt_exists,
    # MsgfmtToolWarning,
    _read_linguas_from_files,
)

class _MOFileBuilder(BuilderBase):
    """The builder class for `MO` files.

    The reason for this builder to exists and its purpose is quite simillar
    as for `_POFileBuilder`. This time, we extend list of sources, not targets,
    and call `BuilderBase._execute()` only once (as we assume single-target
    here).
    """

    def _execute(self, env, target, source, *args, **kw):
        # Here we add support for 'LINGUAS_FILE' keyword. Emitter is not suitable
        # in this case, as it is called too late (after multiple sources
        # are handled single_source builder.

        linguas_files = None
        if 'LINGUAS_FILE' in env and env['LINGUAS_FILE'] is not None:
            linguas_files = env['LINGUAS_FILE']
            # This should prevent from endless recursion.
            env['LINGUAS_FILE'] = None
            # We read only languages. Suffixes shall be added automatically.
            linguas = _read_linguas_from_files(env, linguas_files)
            if SCons.Util.is_List(source):
                source.extend(linguas)
            elif source is not None:
                source = [source] + linguas
            else:
                source = linguas
        result = BuilderBase._execute(self, env, target, source, *args, **kw)
        if linguas_files is not None:
            env['LINGUAS_FILE'] = linguas_files
        return result


def _create_mo_file_builder(env, **kw):
    """ Create builder object for `MOFiles` builder """

    # FIXME: What factory use for source? Ours or their?
    kw['action'] = SCons.Action.Action('$MSGFMTCOM', '$MSGFMTCOMSTR')
    kw['suffix'] = '$MOSUFFIX'
    kw['src_suffix'] = '$POSUFFIX'
    kw['src_builder'] = '_POUpdateBuilder'
    kw['single_source'] = True
    return _MOFileBuilder(**kw)


def generate(env, **kw):
    """ Generate `msgfmt` tool """

    if sys.platform == 'win32':
        msgfmt = SCons.Tool.find_program_path(
            env, 'msgfmt', default_paths=MINGW_DEFAULT_PATHS + CYGWIN_DEFAULT_PATHS
        )
        if msgfmt:
            msgfmt_bin_dir = os.path.dirname(msgfmt)
            env.AppendENVPath('PATH', msgfmt_bin_dir)
        else:
            SCons.Warnings.warn(
                # MsgfmtToolWarning,  # using this breaks test, so keep:
                SCons.Warnings.SConsWarning,
                'msgfmt tool requested, but binary not found in ENV PATH',
            )

    try:
        env['MSGFMT'] = _detect_msgfmt(env)
    except StopError:
        env['MSGFMT'] = 'msgfmt'
    env.SetDefault(
        MSGFMTFLAGS=[SCons.Util.CLVar('-c')],
        MSGFMTCOM='$MSGFMT $MSGFMTFLAGS -o $TARGET $SOURCE',
        MSGFMTCOMSTR='',
        MOSUFFIX=['.mo'],
        POSUFFIX=['.po'],
    )
    env.Append(BUILDERS={'MOFiles': _create_mo_file_builder(env)})


def exists(env):
    """ Check if the tool exists """

    try:
        return _msgfmt_exists(env)
    except StopError:
        return False

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: