Merge pull request #4655 from bdbaddog/fix_new_ninja_package
[scons.git] / SCons / Tool / packaging / rpm.py
bloba51e8679e6df381429f924a162151192e151cc3a
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 """The rpm packager."""
26 import SCons.Builder
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,
33 **kw):
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." )
45 else:
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
59 loc=locals()
60 del loc['kw']
61 kw.update(loc)
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
90 sources.sort()
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', '')
95 try:
96 tarball = env['SOURCE_URL'].split('/')[-1]
97 except KeyError as e:
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,
109 suffix = '.spec',
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:
121 try:
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.
140 str = ""
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 )
179 return str
181 def build_specfile_header(spec):
182 """ Builds all sections but the %file of a rpm specfile
184 str = ""
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',
222 # internal use
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']:
248 str += extra + '\n'
250 return str
253 # mandatory and optional file tags
255 def build_specfile_filesection(spec, files):
256 """ builds the %file section of the specfile
258 str = '%files\n'
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']
265 supported_tags = {
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', }
276 for file in files:
277 # build the tagset
278 tags = {}
279 for k in supported_tags.keys():
280 try:
281 v = file.GetTag(k)
282 if v:
283 tags[k] = v
284 except AttributeError:
285 pass
287 # compile the tagset
288 str = str + SimpleTagCompiler(supported_tags, mandatory=0).compile( tags )
290 str = str + ' '
291 str = str + file.GetTag('PACKAGING_INSTALL_LOCATION')
292 str = str + '\n\n'
294 return str
296 class SimpleTagCompiler:
297 """ Compile RPM tags by doing simple string substitution.
299 The replacement specfication is stored in the *tagset* dictionary,
300 something like::
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:
314 self.tagset = tagset
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):
324 return tag[-2:]
326 def strip_country_code(tag):
327 return tag[:-2]
329 replacements = list(self.tagset.items())
331 str = ""
332 domestic = [t for t in replacements if not is_international(t[0])]
333 for key, replacement in domestic:
334 try:
335 str = str + replacement % values[key]
336 except KeyError as e:
337 if self.mandatory:
338 raise e
340 international = [t for t in replacements if is_international(t[0])]
341 for key, replacement in international:
342 try:
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:
348 if self.mandatory:
349 raise e
351 return str
353 # Local Variables:
354 # tab-width:4
355 # indent-tabs-mode:nil
356 # End:
357 # vim: set expandtab tabstop=4 shiftwidth=4: