1 """SCons.Tool.Packaging.rpm
7 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
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.
29 __revision__ = "src/engine/SCons/Tool/packaging/rpm.py 3842 2008/12/20 22:59:52 scons"
36 from SCons.Environment import OverrideEnvironment
37 from SCons.Tool.packaging import stripinstallbuilder, src_targz
38 from SCons.Errors import UserError
40 def package(env, target, source, PACKAGEROOT, NAME, VERSION,
41 PACKAGEVERSION, DESCRIPTION, SUMMARY, X_RPM_GROUP, LICENSE,
43 # initialize the rpm tool
44 SCons.Tool.Tool('rpm').generate(env)
46 bld = env['BUILDERS']['Rpm']
48 # Generate a UserError whenever the target name has been set explicitly,
49 # since rpm does not allow for controlling it. This is detected by
50 # checking if the target has been set to the default by the Package()
51 # Environment function.
52 if str(target[0])!="%s-%s"%(NAME, VERSION):
53 raise UserError( "Setting target is not supported for rpm." )
55 # This should be overridable from the construction environment,
56 # which it is by using ARCHITECTURE=.
57 # Guessing based on what os.uname() returns at least allows it
58 # to work for both i386 and x86_64 Linux systems.
65 buildarchitecture = os.uname()[4]
66 buildarchitecture = archmap.get(buildarchitecture, buildarchitecture)
68 if kw.has_key('ARCHITECTURE'):
69 buildarchitecture = kw['ARCHITECTURE']
71 fmt = '%s-%s-%s.%s.rpm'
72 srcrpm = fmt % (NAME, VERSION, PACKAGEVERSION, 'src')
73 binrpm = fmt % (NAME, VERSION, PACKAGEVERSION, buildarchitecture)
75 target = [ srcrpm, binrpm ]
77 # get the correct arguments into the kw hash
81 del kw['source'], kw['target'], kw['env']
83 # if no "SOURCE_URL" tag is given add a default one.
84 if not kw.has_key('SOURCE_URL'):
85 #kw['SOURCE_URL']=(str(target[0])+".tar.gz").replace('.rpm', '')
86 kw['SOURCE_URL']=string.replace(str(target[0])+".tar.gz", '.rpm', '')
88 # mangle the source and target list for the rpmbuild
89 env = OverrideEnvironment(env, kw)
90 target, source = stripinstallbuilder(target, source, env)
91 target, source = addspecfile(target, source, env)
92 target, source = collectintargz(target, source, env)
94 # now call the rpm builder to actually build the packet.
95 return apply(bld, [env, target, source], kw)
97 def collectintargz(target, source, env):
98 """ Puts all source files into a tar.gz file. """
99 # the rpm tool depends on a source package, until this is chagned
100 # this hack needs to be here that tries to pack all sources in.
101 sources = env.FindSourceFiles()
103 # filter out the target we are building the source list for.
104 #sources = [s for s in sources if not (s in target)]
105 sources = filter(lambda s, t=target: not (s in t), sources)
107 # find the .spec file for rpm and add it since it is not necessarily found
108 # by the FindSourceFiles function.
109 #sources.extend( [s for s in source if str(s).rfind('.spec')!=-1] )
110 spec_file = lambda s: string.rfind(str(s), '.spec') != -1
111 sources.extend( filter(spec_file, source) )
113 # as the source contains the url of the source package this rpm package
114 # is built from, we extract the target name
115 #tarball = (str(target[0])+".tar.gz").replace('.rpm', '')
116 tarball = string.replace(str(target[0])+".tar.gz", '.rpm', '')
118 #tarball = env['SOURCE_URL'].split('/')[-1]
119 tarball = string.split(env['SOURCE_URL'], '/')[-1]
121 raise SCons.Errors.UserError( "Missing PackageTag '%s' for RPM packager" % e.args[0] )
123 tarball = src_targz.package(env, source=sources, target=tarball,
124 PACKAGEROOT=env['PACKAGEROOT'], )
126 return (target, tarball)
128 def addspecfile(target, source, env):
129 specfile = "%s-%s" % (env['NAME'], env['VERSION'])
131 bld = SCons.Builder.Builder(action = build_specfile,
133 target_factory = SCons.Node.FS.File)
135 source.extend(bld(env, specfile, source))
137 return (target,source)
139 def build_specfile(target, source, env):
140 """ Builds a RPM specfile from a dictionary with string metadata and
141 by analyzing a tree of nodes.
143 file = open(target[0].abspath, 'w')
147 file.write( build_specfile_header(env) )
148 file.write( build_specfile_sections(env) )
149 file.write( build_specfile_filesection(env, source) )
152 # call a user specified function
153 if env.has_key('CHANGE_SPECFILE'):
154 env['CHANGE_SPECFILE'](target, source)
157 raise SCons.Errors.UserError( '"%s" package field for RPM is missing.' % e.args[0] )
161 # mandatory and optional package tag section
163 def build_specfile_sections(spec):
164 """ Builds the sections of a rpm specfile.
168 mandatory_sections = {
169 'DESCRIPTION' : '\n%%description\n%s\n\n', }
171 str = str + SimpleTagCompiler(mandatory_sections).compile( spec )
173 optional_sections = {
174 'DESCRIPTION_' : '%%description -l %s\n%s\n\n',
175 'CHANGELOG' : '%%changelog\n%s\n\n',
176 'X_RPM_PREINSTALL' : '%%pre\n%s\n\n',
177 'X_RPM_POSTINSTALL' : '%%post\n%s\n\n',
178 'X_RPM_PREUNINSTALL' : '%%preun\n%s\n\n',
179 'X_RPM_POSTUNINSTALL' : '%%postun\n%s\n\n',
180 'X_RPM_VERIFY' : '%%verify\n%s\n\n',
182 # These are for internal use but could possibly be overriden
183 'X_RPM_PREP' : '%%prep\n%s\n\n',
184 'X_RPM_BUILD' : '%%build\n%s\n\n',
185 'X_RPM_INSTALL' : '%%install\n%s\n\n',
186 'X_RPM_CLEAN' : '%%clean\n%s\n\n',
189 # Default prep, build, install and clean rules
190 # TODO: optimize those build steps, to not compile the project a second time
191 if not spec.has_key('X_RPM_PREP'):
192 spec['X_RPM_PREP'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"' + '\n%setup -q'
194 if not spec.has_key('X_RPM_BUILD'):
195 spec['X_RPM_BUILD'] = 'mkdir "$RPM_BUILD_ROOT"'
197 if not spec.has_key('X_RPM_INSTALL'):
198 spec['X_RPM_INSTALL'] = 'scons --install-sandbox="$RPM_BUILD_ROOT" "$RPM_BUILD_ROOT"'
200 if not spec.has_key('X_RPM_CLEAN'):
201 spec['X_RPM_CLEAN'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"'
203 str = str + SimpleTagCompiler(optional_sections, mandatory=0).compile( spec )
207 def build_specfile_header(spec):
208 """ Builds all section but the %file of a rpm specfile
212 # first the mandatory sections
213 mandatory_header_fields = {
214 'NAME' : '%%define name %s\nName: %%{name}\n',
215 'VERSION' : '%%define version %s\nVersion: %%{version}\n',
216 'PACKAGEVERSION' : '%%define release %s\nRelease: %%{release}\n',
217 'X_RPM_GROUP' : 'Group: %s\n',
218 'SUMMARY' : 'Summary: %s\n',
219 'LICENSE' : 'License: %s\n', }
221 str = str + SimpleTagCompiler(mandatory_header_fields).compile( spec )
223 # now the optional tags
224 optional_header_fields = {
225 'VENDOR' : 'Vendor: %s\n',
226 'X_RPM_URL' : 'Url: %s\n',
227 'SOURCE_URL' : 'Source: %s\n',
228 'SUMMARY_' : 'Summary(%s): %s\n',
229 'X_RPM_DISTRIBUTION' : 'Distribution: %s\n',
230 'X_RPM_ICON' : 'Icon: %s\n',
231 'X_RPM_PACKAGER' : 'Packager: %s\n',
232 'X_RPM_GROUP_' : 'Group(%s): %s\n',
234 'X_RPM_REQUIRES' : 'Requires: %s\n',
235 'X_RPM_PROVIDES' : 'Provides: %s\n',
236 'X_RPM_CONFLICTS' : 'Conflicts: %s\n',
237 'X_RPM_BUILDREQUIRES' : 'BuildRequires: %s\n',
239 'X_RPM_SERIAL' : 'Serial: %s\n',
240 'X_RPM_EPOCH' : 'Epoch: %s\n',
241 'X_RPM_AUTOREQPROV' : 'AutoReqProv: %s\n',
242 'X_RPM_EXCLUDEARCH' : 'ExcludeArch: %s\n',
243 'X_RPM_EXCLUSIVEARCH' : 'ExclusiveArch: %s\n',
244 'X_RPM_PREFIX' : 'Prefix: %s\n',
245 'X_RPM_CONFLICTS' : 'Conflicts: %s\n',
248 'X_RPM_BUILDROOT' : 'BuildRoot: %s\n', }
250 # fill in default values:
251 # Adding a BuildRequires renders the .rpm unbuildable under System, which
252 # are not managed by rpm, since the database to resolve this dependency is
253 # missing (take Gentoo as an example)
254 # if not s.has_key('x_rpm_BuildRequires'):
255 # s['x_rpm_BuildRequires'] = 'scons'
257 if not spec.has_key('X_RPM_BUILDROOT'):
258 spec['X_RPM_BUILDROOT'] = '%{_tmppath}/%{name}-%{version}-%{release}'
260 str = str + SimpleTagCompiler(optional_header_fields, mandatory=0).compile( spec )
264 # mandatory and optional file tags
266 def build_specfile_filesection(spec, files):
267 """ builds the %file section of the specfile
271 if not spec.has_key('X_RPM_DEFATTR'):
272 spec['X_RPM_DEFATTR'] = '(-,root,root)'
274 str = str + '%%defattr %s\n' % spec['X_RPM_DEFATTR']
277 'PACKAGING_CONFIG' : '%%config %s',
278 'PACKAGING_CONFIG_NOREPLACE' : '%%config(noreplace) %s',
279 'PACKAGING_DOC' : '%%doc %s',
280 'PACKAGING_UNIX_ATTR' : '%%attr %s',
281 'PACKAGING_LANG_' : '%%lang(%s) %s',
282 'PACKAGING_X_RPM_VERIFY' : '%%verify %s',
283 'PACKAGING_X_RPM_DIR' : '%%dir %s',
284 'PACKAGING_X_RPM_DOCDIR' : '%%docdir %s',
285 'PACKAGING_X_RPM_GHOST' : '%%ghost %s', }
290 for k in supported_tags.keys():
292 tags[k]=getattr(file, k)
293 except AttributeError:
297 str = str + SimpleTagCompiler(supported_tags, mandatory=0).compile( tags )
300 str = str + file.PACKAGING_INSTALL_LOCATION
305 class SimpleTagCompiler:
306 """ This class is a simple string substition utility:
307 the replacement specfication is stored in the tagset dictionary, something
309 { "abc" : "cdef %s ",
310 "abc_" : "cdef %s %s" }
312 the compile function gets a value dictionary, which may look like:
316 The resulting string will be:
317 "cdef ghij cdef gh ij"
319 def __init__(self, tagset, mandatory=1):
321 self.mandatory = mandatory
323 def compile(self, values):
324 """ compiles the tagset and returns a str containing the result
326 def is_international(tag):
327 #return tag.endswith('_')
328 return tag[-1:] == '_'
330 def get_country_code(tag):
333 def strip_country_code(tag):
336 replacements = self.tagset.items()
339 #domestic = [ (k,v) for k,v in replacements if not is_international(k) ]
340 domestic = filter(lambda t, i=is_international: not i(t[0]), replacements)
341 for key, replacement in domestic:
343 str = str + replacement % values[key]
348 #international = [ (k,v) for k,v in replacements if is_international(k) ]
349 international = filter(lambda t, i=is_international: i(t[0]), replacements)
350 for key, replacement in international:
352 #int_values_for_key = [ (get_country_code(k),v) for k,v in values.items() if strip_country_code(k) == key ]
353 x = filter(lambda t,key=key,s=strip_country_code: s(t[0]) == key, values.items())
354 int_values_for_key = map(lambda t,g=get_country_code: (g(t[0]),t[1]), x)
355 for v in int_values_for_key:
356 str = str + replacement % v