renamed SCons.Tool.ninja -> SCons.Tool.ninja_tool and added alias in tool loading...
[scons.git] / SCons / Tool / msginit.py
blob19aaccb4ee2e5f2618fb4debabe5b74c4e48fa71
1 # MIT License
3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 """Tool specific initialization of msginit tool."""
26 import sys
27 import os
29 import SCons.Action
30 import SCons.Tool
31 import SCons.Util
32 import SCons.Warnings
33 from SCons.Environment import _null
34 from SCons.Errors import StopError
35 from SCons.Platform.cygwin import CYGWIN_DEFAULT_PATHS
36 from SCons.Platform.mingw import MINGW_DEFAULT_PATHS
37 from SCons.Tool.GettextCommon import (
38 _detect_msginit,
39 _init_po_files,
40 _msginit_exists,
41 # MsginitToolWarning,
42 _POFileBuilder,
46 def _optional_no_translator_flag(env):
47 """Return '--no-translator' flag if we run *msginit(1)* in non-interactive
48 mode."""
49 if 'POAUTOINIT' in env:
50 autoinit = env['POAUTOINIT']
51 else:
52 autoinit = False
53 if autoinit:
54 return [SCons.Util.CLVar('--no-translator')]
55 else:
56 return [SCons.Util.CLVar('')]
59 def _POInitBuilder(env, **kw):
60 """ Create builder object for `POInit` builder. """
62 action = SCons.Action.Action(_init_po_files, None)
63 return _POFileBuilder(env, action=action, target_alias='$POCREATE_ALIAS')
66 def _POInitBuilderWrapper(env, target=None, source=_null, **kw):
67 """Wrapper for _POFileBuilder. We use it to make user's life easier.
69 This wrapper checks for `$POTDOMAIN` construction variable (or override in
70 `**kw`) and treats it appropriatelly.
71 """
72 if source is _null:
73 if 'POTDOMAIN' in kw:
74 domain = kw['POTDOMAIN']
75 elif 'POTDOMAIN' in env:
76 domain = env['POTDOMAIN']
77 else:
78 domain = 'messages'
79 source = [domain] # NOTE: Suffix shall be appended automatically
80 return env._POInitBuilder(target, source, **kw)
82 def generate(env, **kw) -> None:
83 """ Generate the `msginit` tool """
85 if sys.platform == 'win32':
86 msginit = SCons.Tool.find_program_path(
87 env, 'msginit', default_paths=MINGW_DEFAULT_PATHS + CYGWIN_DEFAULT_PATHS
89 if msginit:
90 msginit_bin_dir = os.path.dirname(msginit)
91 env.AppendENVPath('PATH', msginit_bin_dir)
92 else:
93 SCons.Warnings.warn(
94 # MsginitToolWarning, # using this breaks test, so keep:
95 SCons.Warnings.SConsWarning,
96 'msginit tool requested, but binary not found in ENV PATH',
99 try:
100 env['MSGINIT'] = _detect_msginit(env)
101 except StopError:
102 env['MSGINIT'] = 'msginit'
103 msginitcom = (
104 '$MSGINIT ${_MSGNoTranslator(__env__)} -l ${_MSGINITLOCALE}'
105 + ' $MSGINITFLAGS -i $SOURCE -o $TARGET'
107 # NOTE: We set POTSUFFIX here, in case the 'xgettext' is not loaded
108 # (sometimes we really don't need it)
109 env.SetDefault(
110 POSUFFIX=['.po'],
111 POTSUFFIX=['.pot'],
112 _MSGINITLOCALE='${TARGET.filebase}',
113 _MSGNoTranslator=_optional_no_translator_flag,
114 MSGINITCOM=msginitcom,
115 MSGINITCOMSTR='',
116 MSGINITFLAGS=[],
117 POAUTOINIT=False,
118 POCREATE_ALIAS='po-create',
120 env.Append(BUILDERS={'_POInitBuilder': _POInitBuilder(env)})
121 env.AddMethod(_POInitBuilderWrapper, 'POInit')
122 env.AlwaysBuild(env.Alias('$POCREATE_ALIAS'))
125 def exists(env):
126 """ Check if the tool exists """
128 try:
129 return _msginit_exists(env)
130 except StopError:
131 return False
133 # Local Variables:
134 # tab-width:4
135 # indent-tabs-mode:nil
136 # End:
137 # vim: set expandtab tabstop=4 shiftwidth=4: