Variables testing: confirm space-containing values
[scons.git] / SCons / Tool / rpm.py
blob94f5e574a6b572573b7036e3c8833ad026ad2364
1 """SCons.Tool.rpm
3 Tool-specific initialization for rpm.
5 There normally shouldn't be any need to import this module directly.
6 It will usually be imported through the generic SCons.Tool.Tool()
7 selection method.
9 The rpm tool calls the rpmbuild command. The first and only argument should a
10 tar.gz consisting of the source file and a specfile.
11 """
14 # __COPYRIGHT__
16 # Permission is hereby granted, free of charge, to any person obtaining
17 # a copy of this software and associated documentation files (the
18 # "Software"), to deal in the Software without restriction, including
19 # without limitation the rights to use, copy, modify, merge, publish,
20 # distribute, sublicense, and/or sell copies of the Software, and to
21 # permit persons to whom the Software is furnished to do so, subject to
22 # the following conditions:
24 # The above copyright notice and this permission notice shall be included
25 # in all copies or substantial portions of the Software.
27 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
28 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
29 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
38 import os
39 import re
40 import shutil
41 import subprocess
43 import SCons.Builder
44 import SCons.Node.FS
45 import SCons.Util
46 import SCons.Action
47 import SCons.Defaults
49 def get_cmd(source, env) -> str:
50 tar_file_with_included_specfile = source
51 if SCons.Util.is_List(source):
52 tar_file_with_included_specfile = source[0]
53 return "%s %s %s"%(env['RPM'], env['RPMFLAGS'],
54 tar_file_with_included_specfile.get_abspath())
56 def build_rpm(target, source, env):
57 # create a temporary rpm build root.
58 tmpdir = os.path.join(os.path.dirname(target[0].get_abspath()), 'rpmtemp')
59 if os.path.exists(tmpdir):
60 shutil.rmtree(tmpdir)
62 # now create the mandatory rpm directory structure.
63 for d in ['RPMS', 'SRPMS', 'SPECS', 'BUILD']:
64 os.makedirs(os.path.join(tmpdir, d))
66 # set the topdir as an rpmflag.
67 env.Prepend(RPMFLAGS = '--define \'_topdir %s\'' % tmpdir)
69 # now call rpmbuild to create the rpm package.
70 handle = subprocess.Popen(get_cmd(source, env),
71 stdout=subprocess.PIPE,
72 stderr=subprocess.STDOUT,
73 shell=True)
74 with handle.stdout:
75 output = SCons.Util.to_str(handle.stdout.read())
76 status = handle.wait()
78 if status:
79 raise SCons.Errors.BuildError(node=target[0],
80 errstr=output,
81 filename=str(target[0]))
82 else:
83 # XXX: assume that LC_ALL=C is set while running rpmbuild
84 output_files = re.compile('Wrote: (.*)').findall(output)
86 for output, input in zip(output_files, target):
87 rpm_output = os.path.basename(output)
88 expected = os.path.basename(input.get_path())
90 assert expected == rpm_output, "got %s but expected %s" % (rpm_output, expected)
91 shutil.copy(output, input.get_abspath())
94 # cleanup before leaving.
95 shutil.rmtree(tmpdir)
97 return status
99 def string_rpm(target, source, env):
100 try:
101 return env['RPMCOMSTR']
102 except KeyError:
103 return get_cmd(source, env)
105 rpmAction = SCons.Action.Action(build_rpm, string_rpm)
107 RpmBuilder = SCons.Builder.Builder(action = SCons.Action.Action('$RPMCOM', '$RPMCOMSTR'),
108 source_scanner = SCons.Defaults.DirScanner,
109 suffix = '$RPMSUFFIX')
113 def generate(env) -> None:
114 """Add Builders and construction variables for rpm to an Environment."""
115 try:
116 bld = env['BUILDERS']['Rpm']
117 except KeyError:
118 bld = RpmBuilder
119 env['BUILDERS']['Rpm'] = bld
121 env.SetDefault(RPM = 'LC_ALL=C rpmbuild')
122 env.SetDefault(RPMFLAGS = SCons.Util.CLVar('-ta'))
123 env.SetDefault(RPMCOM = rpmAction)
124 env.SetDefault(RPMSUFFIX = '.rpm')
126 def exists(env):
127 return env.Detect('rpmbuild')
129 # Local Variables:
130 # tab-width:4
131 # indent-tabs-mode:nil
132 # End:
133 # vim: set expandtab tabstop=4 shiftwidth=4: