logging working in NewParallel, but changed to be default. Need to figure out how...
[scons.git] / SCons / Tool / packaging / rpm.py
blobcdeabcf62d6fc273c45bd6110e3e90cbb2157a69
1 """SCons.Tool.Packaging.rpm
3 The rpm packager.
4 """
7 # __COPYRIGHT__
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be included
18 # in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
21 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
22 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
31 import SCons.Builder
32 import SCons.Tool.rpmutils
34 from SCons.Environment import OverrideEnvironment
35 from SCons.Tool.packaging import stripinstallbuilder, src_targz
36 from SCons.Errors import UserError
38 def package(env, target, source, PACKAGEROOT, NAME, VERSION,
39 PACKAGEVERSION, DESCRIPTION, SUMMARY, X_RPM_GROUP, LICENSE,
40 **kw):
41 # initialize the rpm tool
42 SCons.Tool.Tool('rpm').generate(env)
44 bld = env['BUILDERS']['Rpm']
46 # Generate a UserError whenever the target name has been set explicitly,
47 # since rpm does not allow for controlling it. This is detected by
48 # checking if the target has been set to the default by the Package()
49 # Environment function.
50 if str(target[0])!="%s-%s"%(NAME, VERSION):
51 raise UserError( "Setting target is not supported for rpm." )
52 else:
53 # Deduce the build architecture, but allow it to be overridden
54 # by setting ARCHITECTURE in the construction env.
55 buildarchitecture = SCons.Tool.rpmutils.defaultMachine()
56 if 'ARCHITECTURE' in kw:
57 buildarchitecture = kw['ARCHITECTURE']
59 fmt = '%s-%s-%s.%s.rpm'
60 srcrpm = fmt % (NAME, VERSION, PACKAGEVERSION, 'src')
61 binrpm = fmt % (NAME, VERSION, PACKAGEVERSION, buildarchitecture)
63 target = [ srcrpm, binrpm ]
65 # get the correct arguments into the kw hash
66 loc=locals()
67 del loc['kw']
68 kw.update(loc)
69 del kw['source'], kw['target'], kw['env']
71 # if no "SOURCE_URL" tag is given add a default one.
72 if 'SOURCE_URL' not in kw:
73 kw['SOURCE_URL']=(str(target[0])+".tar.gz").replace('.rpm', '')
75 # mangle the source and target list for the rpmbuild
76 env = OverrideEnvironment(env, kw)
77 target, source = stripinstallbuilder(target, source, env)
78 target, source = addspecfile(target, source, env)
79 target, source = collectintargz(target, source, env)
81 # now call the rpm builder to actually build the packet.
82 return bld(env, target, source, **kw)
84 def collectintargz(target, source, env):
85 """ Puts all source files into a tar.gz file. """
86 # the rpm tool depends on a source package, until this is changed
87 # this hack needs to be here that tries to pack all sources in.
88 sources = env.FindSourceFiles()
90 # filter out the target we are building the source list for.
91 sources = [s for s in sources if s not in target]
93 # find the .spec file for rpm and add it since it is not necessarily found
94 # by the FindSourceFiles function.
95 sources.extend( [s for s in source if str(s).rfind('.spec')!=-1] )
96 # sort to keep sources from changing order across builds
97 sources.sort()
99 # as the source contains the url of the source package this rpm package
100 # is built from, we extract the target name
101 tarball = (str(target[0])+".tar.gz").replace('.rpm', '')
102 try:
103 tarball = env['SOURCE_URL'].split('/')[-1]
104 except KeyError as e:
105 raise SCons.Errors.UserError( "Missing PackageTag '%s' for RPM packager" % e.args[0] )
107 tarball = src_targz.package(env, source=sources, target=tarball,
108 PACKAGEROOT=env['PACKAGEROOT'], )
110 return (target, tarball)
112 def addspecfile(target, source, env):
113 specfile = "%s-%s" % (env['NAME'], env['VERSION'])
115 bld = SCons.Builder.Builder(action = build_specfile,
116 suffix = '.spec',
117 target_factory = SCons.Node.FS.File)
119 source.extend(bld(env, specfile, source))
121 return (target,source)
123 def build_specfile(target, source, env):
124 """ Builds a RPM specfile from a dictionary with string metadata and
125 by analyzing a tree of nodes.
127 with open(target[0].get_abspath(), 'w') as ofp:
128 try:
129 ofp.write(build_specfile_header(env))
130 ofp.write(build_specfile_sections(env))
131 ofp.write(build_specfile_filesection(env, source))
133 # call a user specified function
134 if 'CHANGE_SPECFILE' in env:
135 env['CHANGE_SPECFILE'](target, source)
137 except KeyError as e:
138 raise SCons.Errors.UserError('"%s" package field for RPM is missing.' % e.args[0])
142 # mandatory and optional package tag section
144 def build_specfile_sections(spec):
145 """ Builds the sections of a rpm specfile.
147 str = ""
149 mandatory_sections = {
150 'DESCRIPTION' : '\n%%description\n%s\n\n', }
152 str = str + SimpleTagCompiler(mandatory_sections).compile( spec )
154 optional_sections = {
155 'DESCRIPTION_' : '%%description -l %s\n%s\n\n',
156 'CHANGELOG' : '%%changelog\n%s\n\n',
157 'X_RPM_PREINSTALL' : '%%pre\n%s\n\n',
158 'X_RPM_POSTINSTALL' : '%%post\n%s\n\n',
159 'X_RPM_PREUNINSTALL' : '%%preun\n%s\n\n',
160 'X_RPM_POSTUNINSTALL' : '%%postun\n%s\n\n',
161 'X_RPM_VERIFY' : '%%verify\n%s\n\n',
163 # These are for internal use but could possibly be overridden
164 'X_RPM_PREP' : '%%prep\n%s\n\n',
165 'X_RPM_BUILD' : '%%build\n%s\n\n',
166 'X_RPM_INSTALL' : '%%install\n%s\n\n',
167 'X_RPM_CLEAN' : '%%clean\n%s\n\n',
170 # Default prep, build, install and clean rules
171 # TODO: optimize those build steps, to not compile the project a second time
172 if 'X_RPM_PREP' not in spec:
173 spec['X_RPM_PREP'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"' + '\n%setup -q'
175 if 'X_RPM_BUILD' not in spec:
176 spec['X_RPM_BUILD'] = '[ ! -e "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && mkdir "$RPM_BUILD_ROOT"'
178 if 'X_RPM_INSTALL' not in spec:
179 spec['X_RPM_INSTALL'] = 'scons --install-sandbox="$RPM_BUILD_ROOT" "$RPM_BUILD_ROOT"'
181 if 'X_RPM_CLEAN' not in spec:
182 spec['X_RPM_CLEAN'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"'
184 str = str + SimpleTagCompiler(optional_sections, mandatory=0).compile( spec )
186 return str
188 def build_specfile_header(spec):
189 """ Builds all sections but the %file of a rpm specfile
191 str = ""
193 # first the mandatory sections
194 mandatory_header_fields = {
195 'NAME' : '%%define name %s\nName: %%{name}\n',
196 'VERSION' : '%%define version %s\nVersion: %%{version}\n',
197 'PACKAGEVERSION' : '%%define release %s\nRelease: %%{release}\n',
198 'X_RPM_GROUP' : 'Group: %s\n',
199 'SUMMARY' : 'Summary: %s\n',
200 'LICENSE' : 'License: %s\n',
203 str = str + SimpleTagCompiler(mandatory_header_fields).compile( spec )
205 # now the optional tags
206 optional_header_fields = {
207 'VENDOR' : 'Vendor: %s\n',
208 'X_RPM_URL' : 'Url: %s\n',
209 'SOURCE_URL' : 'Source: %s\n',
210 'SUMMARY_' : 'Summary(%s): %s\n',
211 'ARCHITECTURE' : 'BuildArch: %s\n',
212 'X_RPM_DISTRIBUTION' : 'Distribution: %s\n',
213 'X_RPM_ICON' : 'Icon: %s\n',
214 'X_RPM_PACKAGER' : 'Packager: %s\n',
215 'X_RPM_GROUP_' : 'Group(%s): %s\n',
217 'X_RPM_REQUIRES' : 'Requires: %s\n',
218 'X_RPM_PROVIDES' : 'Provides: %s\n',
219 'X_RPM_CONFLICTS' : 'Conflicts: %s\n',
220 'X_RPM_BUILDREQUIRES' : 'BuildRequires: %s\n',
222 'X_RPM_SERIAL' : 'Serial: %s\n',
223 'X_RPM_EPOCH' : 'Epoch: %s\n',
224 'X_RPM_AUTOREQPROV' : 'AutoReqProv: %s\n',
225 'X_RPM_EXCLUDEARCH' : 'ExcludeArch: %s\n',
226 'X_RPM_EXCLUSIVEARCH' : 'ExclusiveArch: %s\n',
227 'X_RPM_PREFIX' : 'Prefix: %s\n',
229 # internal use
230 'X_RPM_BUILDROOT' : 'BuildRoot: %s\n',
233 # fill in default values:
234 # Adding a BuildRequires renders the .rpm unbuildable under systems which
235 # are not managed by rpm, since the database to resolve this dependency is
236 # missing (take Gentoo as an example)
237 #if 'X_RPM_BUILDREQUIRES' not in spec:
238 # spec['X_RPM_BUILDREQUIRES'] = 'scons'
240 if 'X_RPM_BUILDROOT' not in spec:
241 spec['X_RPM_BUILDROOT'] = '%{_tmppath}/%{name}-%{version}-%{release}'
243 str = str + SimpleTagCompiler(optional_header_fields, mandatory=0).compile( spec )
245 # Add any extra specfile definitions the user may have supplied.
246 # These flags get no processing, they are just added.
247 # github #3164: if we don't turn off debug package generation
248 # the tests which build packages all fail. If there are no
249 # extra flags, default to adding this one. If the user wants
250 # to turn this back on, supply the flag set to None.
252 if 'X_RPM_EXTRADEFS' not in spec:
253 spec['X_RPM_EXTRADEFS'] = ['%global debug_package %{nil}']
254 for extra in spec['X_RPM_EXTRADEFS']:
255 str += extra + '\n'
257 return str
260 # mandatory and optional file tags
262 def build_specfile_filesection(spec, files):
263 """ builds the %file section of the specfile
265 str = '%files\n'
267 if 'X_RPM_DEFATTR' not in spec:
268 spec['X_RPM_DEFATTR'] = '(-,root,root)'
270 str = str + '%%defattr %s\n' % spec['X_RPM_DEFATTR']
272 supported_tags = {
273 'PACKAGING_CONFIG' : '%%config %s',
274 'PACKAGING_CONFIG_NOREPLACE' : '%%config(noreplace) %s',
275 'PACKAGING_DOC' : '%%doc %s',
276 'PACKAGING_UNIX_ATTR' : '%%attr %s',
277 'PACKAGING_LANG_' : '%%lang(%s) %s',
278 'PACKAGING_X_RPM_VERIFY' : '%%verify %s',
279 'PACKAGING_X_RPM_DIR' : '%%dir %s',
280 'PACKAGING_X_RPM_DOCDIR' : '%%docdir %s',
281 'PACKAGING_X_RPM_GHOST' : '%%ghost %s', }
283 for file in files:
284 # build the tagset
285 tags = {}
286 for k in supported_tags.keys():
287 try:
288 v = file.GetTag(k)
289 if v:
290 tags[k] = v
291 except AttributeError:
292 pass
294 # compile the tagset
295 str = str + SimpleTagCompiler(supported_tags, mandatory=0).compile( tags )
297 str = str + ' '
298 str = str + file.GetTag('PACKAGING_INSTALL_LOCATION')
299 str = str + '\n\n'
301 return str
303 class SimpleTagCompiler:
304 """ Compile RPM tags by doing simple string substitution.
306 The replacement specfication is stored in the *tagset* dictionary,
307 something like::
309 {"abc" : "cdef %s ", "abc_": "cdef %s %s"}
311 The :func:`compile` function gets a value dictionary, which may look like::
313 {"abc": "ghij", "abc_gh": "ij"}
315 The resulting string will be::
317 "cdef ghij cdef gh ij"
320 def __init__(self, tagset, mandatory=1):
321 self.tagset = tagset
322 self.mandatory = mandatory
324 def compile(self, values):
325 """ Compiles the tagset and returns a str containing the result
327 def is_international(tag):
328 return tag.endswith('_')
330 def get_country_code(tag):
331 return tag[-2:]
333 def strip_country_code(tag):
334 return tag[:-2]
336 replacements = list(self.tagset.items())
338 str = ""
339 domestic = [t for t in replacements if not is_international(t[0])]
340 for key, replacement in domestic:
341 try:
342 str = str + replacement % values[key]
343 except KeyError as e:
344 if self.mandatory:
345 raise e
347 international = [t for t in replacements if is_international(t[0])]
348 for key, replacement in international:
349 try:
350 x = [t for t in values.items() if strip_country_code(t[0]) == key]
351 int_values_for_key = [(get_country_code(t[0]),t[1]) for t in x]
352 for v in int_values_for_key:
353 str = str + replacement % v
354 except KeyError as e:
355 if self.mandatory:
356 raise e
358 return str
360 # Local Variables:
361 # tab-width:4
362 # indent-tabs-mode:nil
363 # End:
364 # vim: set expandtab tabstop=4 shiftwidth=4: