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 """The rpm packager."""
27 import SCons
.Tool
.rpmutils
28 from SCons
.Tool
.packaging
import stripinstallbuilder
, src_targz
29 from SCons
.Errors
import UserError
31 def package(env
, target
, source
, PACKAGEROOT
, NAME
, VERSION
,
32 PACKAGEVERSION
, DESCRIPTION
, SUMMARY
, X_RPM_GROUP
, LICENSE
,
34 # initialize the rpm tool
35 SCons
.Tool
.Tool('rpm').generate(env
)
37 bld
= env
['BUILDERS']['Rpm']
39 # Generate a UserError whenever the target name has been set explicitly,
40 # since rpm does not allow for controlling it. This is detected by
41 # checking if the target has been set to the default by the Package()
42 # Environment function.
43 if str(target
[0])!="%s-%s"%(NAME
, VERSION
):
44 raise UserError( "Setting target is not supported for rpm." )
46 # Deduce the build architecture, but allow it to be overridden
47 # by setting ARCHITECTURE in the construction env.
48 buildarchitecture
= SCons
.Tool
.rpmutils
.defaultMachine()
49 if 'ARCHITECTURE' in kw
:
50 buildarchitecture
= kw
['ARCHITECTURE']
52 fmt
= '%s-%s-%s.%s.rpm'
53 srcrpm
= fmt
% (NAME
, VERSION
, PACKAGEVERSION
, 'src')
54 binrpm
= fmt
% (NAME
, VERSION
, PACKAGEVERSION
, buildarchitecture
)
56 target
= [ srcrpm
, binrpm
]
58 # get the correct arguments into the kw hash
62 del kw
['source'], kw
['target'], kw
['env']
64 # if no "SOURCE_URL" tag is given add a default one.
65 if 'SOURCE_URL' not in kw
:
66 kw
['SOURCE_URL']=(str(target
[0])+".tar.gz").replace('.rpm', '')
68 # mangle the source and target list for the rpmbuild
69 env
= env
.Override(kw
)
70 target
, source
= stripinstallbuilder(target
, source
, env
)
71 target
, source
= addspecfile(target
, source
, env
)
72 target
, source
= collectintargz(target
, source
, env
)
74 # now call the rpm builder to actually build the packet.
75 return bld(env
, target
, source
, **kw
)
77 def collectintargz(target
, source
, env
):
78 """ Puts all source files into a tar.gz file. """
79 # the rpm tool depends on a source package, until this is changed
80 # this hack needs to be here that tries to pack all sources in.
81 sources
= env
.FindSourceFiles()
83 # filter out the target we are building the source list for.
84 sources
= [s
for s
in sources
if s
not in target
]
86 # find the .spec file for rpm and add it since it is not necessarily found
87 # by the FindSourceFiles function.
88 sources
.extend( [s
for s
in source
if str(s
).rfind('.spec')!=-1] )
89 # sort to keep sources from changing order across builds
92 # as the source contains the url of the source package this rpm package
93 # is built from, we extract the target name
94 tarball
= (str(target
[0])+".tar.gz").replace('.rpm', '')
96 tarball
= env
['SOURCE_URL'].split('/')[-1]
98 raise SCons
.Errors
.UserError( "Missing PackageTag '%s' for RPM packager" % e
.args
[0] )
100 tarball
= src_targz
.package(env
, source
=sources
, target
=tarball
,
101 PACKAGEROOT
=env
['PACKAGEROOT'], )
103 return (target
, tarball
)
105 def addspecfile(target
, source
, env
):
106 specfile
= "%s-%s" % (env
['NAME'], env
['VERSION'])
108 bld
= SCons
.Builder
.Builder(action
= build_specfile
,
110 target_factory
= SCons
.Node
.FS
.File
)
112 source
.extend(bld(env
, specfile
, source
))
114 return (target
,source
)
116 def build_specfile(target
, source
, env
):
117 """ Builds a RPM specfile from a dictionary with string metadata and
118 by analyzing a tree of nodes.
120 with
open(target
[0].get_abspath(), 'w') as ofp
:
122 ofp
.write(build_specfile_header(env
))
123 ofp
.write(build_specfile_sections(env
))
124 ofp
.write(build_specfile_filesection(env
, source
))
126 # call a user specified function
127 if 'CHANGE_SPECFILE' in env
:
128 env
['CHANGE_SPECFILE'](target
, source
)
130 except KeyError as e
:
131 raise SCons
.Errors
.UserError('"%s" package field for RPM is missing.' % e
.args
[0])
135 # mandatory and optional package tag section
137 def build_specfile_sections(spec
):
138 """ Builds the sections of a rpm specfile.
142 mandatory_sections
= {
143 'DESCRIPTION' : '\n%%description\n%s\n\n', }
145 str = str + SimpleTagCompiler(mandatory_sections
).compile( spec
)
147 optional_sections
= {
148 'DESCRIPTION_' : '%%description -l %s\n%s\n\n',
149 'CHANGELOG' : '%%changelog\n%s\n\n',
150 'X_RPM_PREINSTALL' : '%%pre\n%s\n\n',
151 'X_RPM_POSTINSTALL' : '%%post\n%s\n\n',
152 'X_RPM_PREUNINSTALL' : '%%preun\n%s\n\n',
153 'X_RPM_POSTUNINSTALL' : '%%postun\n%s\n\n',
154 'X_RPM_VERIFY' : '%%verify\n%s\n\n',
156 # These are for internal use but could possibly be overridden
157 'X_RPM_PREP' : '%%prep\n%s\n\n',
158 'X_RPM_BUILD' : '%%build\n%s\n\n',
159 'X_RPM_INSTALL' : '%%install\n%s\n\n',
160 'X_RPM_CLEAN' : '%%clean\n%s\n\n',
163 # Default prep, build, install and clean rules
164 # TODO: optimize those build steps, to not compile the project a second time
165 if 'X_RPM_PREP' not in spec
:
166 spec
['X_RPM_PREP'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"' + '\n%setup -q'
168 if 'X_RPM_BUILD' not in spec
:
169 spec
['X_RPM_BUILD'] = '[ ! -e "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && mkdir "$RPM_BUILD_ROOT"'
171 if 'X_RPM_INSTALL' not in spec
:
172 spec
['X_RPM_INSTALL'] = 'scons --install-sandbox="$RPM_BUILD_ROOT" "$RPM_BUILD_ROOT"'
174 if 'X_RPM_CLEAN' not in spec
:
175 spec
['X_RPM_CLEAN'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"'
177 str = str + SimpleTagCompiler(optional_sections
, mandatory
=0).compile( spec
)
181 def build_specfile_header(spec
):
182 """ Builds all sections but the %file of a rpm specfile
186 # first the mandatory sections
187 mandatory_header_fields
= {
188 'NAME' : '%%define name %s\nName: %%{name}\n',
189 'VERSION' : '%%define version %s\nVersion: %%{version}\n',
190 'PACKAGEVERSION' : '%%define release %s\nRelease: %%{release}\n',
191 'X_RPM_GROUP' : 'Group: %s\n',
192 'SUMMARY' : 'Summary: %s\n',
193 'LICENSE' : 'License: %s\n',
196 str = str + SimpleTagCompiler(mandatory_header_fields
).compile( spec
)
198 # now the optional tags
199 optional_header_fields
= {
200 'VENDOR' : 'Vendor: %s\n',
201 'X_RPM_URL' : 'Url: %s\n',
202 'SOURCE_URL' : 'Source: %s\n',
203 'SUMMARY_' : 'Summary(%s): %s\n',
204 'ARCHITECTURE' : 'BuildArch: %s\n',
205 'X_RPM_DISTRIBUTION' : 'Distribution: %s\n',
206 'X_RPM_ICON' : 'Icon: %s\n',
207 'X_RPM_PACKAGER' : 'Packager: %s\n',
208 'X_RPM_GROUP_' : 'Group(%s): %s\n',
210 'X_RPM_REQUIRES' : 'Requires: %s\n',
211 'X_RPM_PROVIDES' : 'Provides: %s\n',
212 'X_RPM_CONFLICTS' : 'Conflicts: %s\n',
213 'X_RPM_BUILDREQUIRES' : 'BuildRequires: %s\n',
215 'X_RPM_SERIAL' : 'Serial: %s\n',
216 'X_RPM_EPOCH' : 'Epoch: %s\n',
217 'X_RPM_AUTOREQPROV' : 'AutoReqProv: %s\n',
218 'X_RPM_EXCLUDEARCH' : 'ExcludeArch: %s\n',
219 'X_RPM_EXCLUSIVEARCH' : 'ExclusiveArch: %s\n',
220 'X_RPM_PREFIX' : 'Prefix: %s\n',
223 'X_RPM_BUILDROOT' : 'BuildRoot: %s\n',
226 # fill in default values:
227 # Adding a BuildRequires renders the .rpm unbuildable under systems which
228 # are not managed by rpm, since the database to resolve this dependency is
229 # missing (take Gentoo as an example)
230 #if 'X_RPM_BUILDREQUIRES' not in spec:
231 # spec['X_RPM_BUILDREQUIRES'] = 'scons'
233 if 'X_RPM_BUILDROOT' not in spec
:
234 spec
['X_RPM_BUILDROOT'] = '%{_tmppath}/%{name}-%{version}-%{release}'
236 str = str + SimpleTagCompiler(optional_header_fields
, mandatory
=0).compile( spec
)
238 # Add any extra specfile definitions the user may have supplied.
239 # These flags get no processing, they are just added.
240 # github #3164: if we don't turn off debug package generation
241 # the tests which build packages all fail. If there are no
242 # extra flags, default to adding this one. If the user wants
243 # to turn this back on, supply the flag set to None.
245 if 'X_RPM_EXTRADEFS' not in spec
:
246 spec
['X_RPM_EXTRADEFS'] = ['%global debug_package %{nil}']
247 for extra
in spec
['X_RPM_EXTRADEFS']:
253 # mandatory and optional file tags
255 def build_specfile_filesection(spec
, files
):
256 """ builds the %file section of the specfile
260 if 'X_RPM_DEFATTR' not in spec
:
261 spec
['X_RPM_DEFATTR'] = '(-,root,root)'
263 str = str + '%%defattr %s\n' % spec
['X_RPM_DEFATTR']
266 'PACKAGING_CONFIG' : '%%config %s',
267 'PACKAGING_CONFIG_NOREPLACE' : '%%config(noreplace) %s',
268 'PACKAGING_DOC' : '%%doc %s',
269 'PACKAGING_UNIX_ATTR' : '%%attr %s',
270 'PACKAGING_LANG_' : '%%lang(%s) %s',
271 'PACKAGING_X_RPM_VERIFY' : '%%verify %s',
272 'PACKAGING_X_RPM_DIR' : '%%dir %s',
273 'PACKAGING_X_RPM_DOCDIR' : '%%docdir %s',
274 'PACKAGING_X_RPM_GHOST' : '%%ghost %s', }
279 for k
in supported_tags
.keys():
284 except AttributeError:
288 str = str + SimpleTagCompiler(supported_tags
, mandatory
=0).compile( tags
)
291 str = str + file.GetTag('PACKAGING_INSTALL_LOCATION')
296 class SimpleTagCompiler
:
297 """ Compile RPM tags by doing simple string substitution.
299 The replacement specfication is stored in the *tagset* dictionary,
302 {"abc" : "cdef %s ", "abc_": "cdef %s %s"}
304 The :func:`compile` function gets a value dictionary, which may look like::
306 {"abc": "ghij", "abc_gh": "ij"}
308 The resulting string will be::
310 "cdef ghij cdef gh ij"
313 def __init__(self
, tagset
, mandatory
: int=1) -> None:
315 self
.mandatory
= mandatory
317 def compile(self
, values
):
318 """ Compiles the tagset and returns a str containing the result
320 def is_international(tag
) -> bool:
321 return tag
.endswith('_')
323 def get_country_code(tag
):
326 def strip_country_code(tag
):
329 replacements
= list(self
.tagset
.items())
332 domestic
= [t
for t
in replacements
if not is_international(t
[0])]
333 for key
, replacement
in domestic
:
335 str = str + replacement
% values
[key
]
336 except KeyError as e
:
340 international
= [t
for t
in replacements
if is_international(t
[0])]
341 for key
, replacement
in international
:
343 x
= [t
for t
in values
.items() if strip_country_code(t
[0]) == key
]
344 int_values_for_key
= [(get_country_code(t
[0]),t
[1]) for t
in x
]
345 for v
in int_values_for_key
:
346 str = str + replacement
% v
347 except KeyError as e
:
355 # indent-tabs-mode:nil
357 # vim: set expandtab tabstop=4 shiftwidth=4: