1"""SCons.Tool.Packaging.rpm 2 3The rpm packager. 4""" 5 6# 7# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 The SCons Foundation 8# 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: 16# 17# The above copyright notice and this permission notice shall be included 18# in all copies or substantial portions of the Software. 19# 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. 27# 28 29__revision__ = "src/engine/SCons/Tool/packaging/rpm.py 4369 2009/09/19 15:58:29 scons" 30 31import os 32import string 33 34import SCons.Builder 35 36from SCons.Environment import OverrideEnvironment 37from SCons.Tool.packaging import stripinstallbuilder, src_targz 38from SCons.Errors import UserError 39 40def package(env, target, source, PACKAGEROOT, NAME, VERSION, 41 PACKAGEVERSION, DESCRIPTION, SUMMARY, X_RPM_GROUP, LICENSE, 42 **kw): 43 # initialize the rpm tool 44 SCons.Tool.Tool('rpm').generate(env) 45 46 bld = env['BUILDERS']['Rpm'] 47 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." ) 54 else: 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. 59 archmap = { 60 'i686' : 'i386', 61 'i586' : 'i386', 62 'i486' : 'i386', 63 } 64 65 buildarchitecture = os.uname()[4] 66 buildarchitecture = archmap.get(buildarchitecture, buildarchitecture) 67 68 if 'ARCHITECTURE' in kw: 69 buildarchitecture = kw['ARCHITECTURE'] 70 71 fmt = '%s-%s-%s.%s.rpm' 72 srcrpm = fmt % (NAME, VERSION, PACKAGEVERSION, 'src') 73 binrpm = fmt % (NAME, VERSION, PACKAGEVERSION, buildarchitecture) 74 75 target = [ srcrpm, binrpm ] 76 77 # get the correct arguments into the kw hash 78 loc=locals() 79 del loc['kw'] 80 kw.update(loc) 81 del kw['source'], kw['target'], kw['env'] 82 83 # if no "SOURCE_URL" tag is given add a default one. 84 if 'SOURCE_URL' not in kw: 85 #kw['SOURCE_URL']=(str(target[0])+".tar.gz").replace('.rpm', '') 86 kw['SOURCE_URL']=string.replace(str(target[0])+".tar.gz", '.rpm', '') 87 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) 93 94 # now call the rpm builder to actually build the packet. 95 return bld(*[env, target, source], **kw) 96 97def 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() 102 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) 106 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) ) 112 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', '') 117 try: 118 #tarball = env['SOURCE_URL'].split('/')[-1] 119 tarball = string.split(env['SOURCE_URL'], '/')[-1] 120 except KeyError as e: 121 raise SCons.Errors.UserError( "Missing PackageTag '%s' for RPM packager" % e.args[0] ) 122 123 tarball = src_targz.package(env, source=sources, target=tarball, 124 PACKAGEROOT=env['PACKAGEROOT'], ) 125 126 return (target, tarball) 127 128def addspecfile(target, source, env): 129 specfile = "%s-%s" % (env['NAME'], env['VERSION']) 130 131 bld = SCons.Builder.Builder(action = build_specfile, 132 suffix = '.spec', 133 target_factory = SCons.Node.FS.File) 134 135 source.extend(bld(env, specfile, source)) 136 137 return (target,source) 138 139def build_specfile(target, source, env): 140 """ Builds a RPM specfile from a dictionary with string metadata and 141 by analyzing a tree of nodes. 142 """ 143 file = open(target[0].abspath, 'w') 144 str = "" 145 146 try: 147 file.write( build_specfile_header(env) ) 148 file.write( build_specfile_sections(env) ) 149 file.write( build_specfile_filesection(env, source) ) 150 file.close() 151 152 # call a user specified function 153 if 'CHANGE_SPECFILE' in env: 154 env['CHANGE_SPECFILE'](target, source) 155 156 except KeyError as e: 157 raise SCons.Errors.UserError( '"%s" package field for RPM is missing.' % e.args[0] ) 158 159 160# 161# mandatory and optional package tag section 162# 163def build_specfile_sections(spec): 164 """ Builds the sections of a rpm specfile. 165 """ 166 str = "" 167 168 mandatory_sections = { 169 'DESCRIPTION' : '\n%%description\n%s\n\n', } 170 171 str = str + SimpleTagCompiler(mandatory_sections).compile( spec ) 172 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', 181 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', 187 } 188 189 # Default prep, build, install and clean rules 190 # TODO: optimize those build steps, to not compile the project a second time 191 if 'X_RPM_PREP' not in spec: 192 spec['X_RPM_PREP'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"' + '\n%setup -q' 193 194 if 'X_RPM_BUILD' not in spec: 195 spec['X_RPM_BUILD'] = 'mkdir "$RPM_BUILD_ROOT"' 196 197 if 'X_RPM_INSTALL' not in spec: 198 spec['X_RPM_INSTALL'] = 'scons --install-sandbox="$RPM_BUILD_ROOT" "$RPM_BUILD_ROOT"' 199 200 if 'X_RPM_CLEAN' not in spec: 201 spec['X_RPM_CLEAN'] = '[ -n "$RPM_BUILD_ROOT" -a "$RPM_BUILD_ROOT" != / ] && rm -rf "$RPM_BUILD_ROOT"' 202 203 str = str + SimpleTagCompiler(optional_sections, mandatory=0).compile( spec ) 204 205 return str 206 207def build_specfile_header(spec): 208 """ Builds all section but the %file of a rpm specfile 209 """ 210 str = "" 211 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', } 220 221 str = str + SimpleTagCompiler(mandatory_header_fields).compile( spec ) 222 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', 233 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', 238 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', 246 247 # internal use 248 'X_RPM_BUILDROOT' : 'BuildRoot: %s\n', } 249 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' 256 257 if 'X_RPM_BUILDROOT' not in spec: 258 spec['X_RPM_BUILDROOT'] = '%{_tmppath}/%{name}-%{version}-%{release}' 259 260 str = str + SimpleTagCompiler(optional_header_fields, mandatory=0).compile( spec ) 261 return str 262 263# 264# mandatory and optional file tags 265# 266def build_specfile_filesection(spec, files): 267 """ builds the %file section of the specfile 268 """ 269 str = '%files\n' 270 271 if 'X_RPM_DEFATTR' not in spec: 272 spec['X_RPM_DEFATTR'] = '(-,root,root)' 273 274 str = str + '%%defattr %s\n' % spec['X_RPM_DEFATTR'] 275 276 supported_tags = { 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', } 286 287 for file in files: 288 # build the tagset 289 tags = {} 290 for k in supported_tags.keys(): 291 try: 292 tags[k]=getattr(file, k) 293 except AttributeError: 294 pass 295 296 # compile the tagset 297 str = str + SimpleTagCompiler(supported_tags, mandatory=0).compile( tags ) 298 299 str = str + ' ' 300 str = str + file.PACKAGING_INSTALL_LOCATION 301 str = str + '\n\n' 302 303 return str 304 305class SimpleTagCompiler: 306 """ This class is a simple string substition utility: 307 the replacement specfication is stored in the tagset dictionary, something 308 like: 309 { "abc" : "cdef %s ", 310 "abc_" : "cdef %s %s" } 311 312 the compile function gets a value dictionary, which may look like: 313 { "abc" : "ghij", 314 "abc_gh" : "ij" } 315 316 The resulting string will be: 317 "cdef ghij cdef gh ij" 318 """ 319 def __init__(self, tagset, mandatory=1): 320 self.tagset = tagset 321 self.mandatory = mandatory 322 323 def compile(self, values): 324 """ compiles the tagset and returns a str containing the result 325 """ 326 def is_international(tag): 327 #return tag.endswith('_') 328 return tag[-1:] == '_' 329 330 def get_country_code(tag): 331 return tag[-2:] 332 333 def strip_country_code(tag): 334 return tag[:-2] 335 336 replacements = self.tagset.items() 337 338 str = "" 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: 342 try: 343 str = str + replacement % values[key] 344 except KeyError as e: 345 if self.mandatory: 346 raise e 347 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: 351 try: 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 357 except KeyError as e: 358 if self.mandatory: 359 raise e 360 361 return str 362 363# Local Variables: 364# tab-width:4 365# indent-tabs-mode:nil 366# End: 367# vim: set expandtab tabstop=4 shiftwidth=4: 368