1 """SCons.Tool.Packaging
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/__init__.py 3842 2008/12/20 22:59:52 scons"
31 import SCons.Environment
32 from SCons.Variables import *
33 from SCons.Errors import *
34 from SCons.Util import is_List, make_path_relative
35 from SCons.Warnings import warn, Warning
40 __all__ = [ 'src_targz', 'src_tarbz2', 'src_zip', 'tarbz2', 'targz', 'zip', 'rpm', 'msi', 'ipk' ]
43 # Utility and Builder function
45 def Tag(env, target, source, *more_tags, **kw_tags):
46 """ Tag a file with the given arguments, just sets the accordingly named
47 attribute on the file object.
58 kw_tags[first_tag[0]] = ''
60 if len(kw_tags) == 0 and len(more_tags) == 0:
61 raise UserError, "No tags given."
67 if not SCons.Util.is_List(target):
70 # hmm, sometimes the target list, is a list of a list
71 # make sure it is flattened prior to processing.
72 # TODO: perhaps some bug ?!?
73 target=env.Flatten(target)
76 for (k,v) in kw_tags.items():
77 # all file tags have to start with PACKAGING_, so we can later
78 # differentiate between "normal" object attributes and the
79 # packaging attributes. As the user should not be bothered with
80 # that, the prefix will be added here if missing.
81 #if not k.startswith('PACKAGING_'):
82 if k[:10] != 'PACKAGING_':
86 def Package(env, target=None, source=None, **kw):
87 """ Entry point for the package tool.
89 # check if we need to find the source files ourself
91 source = env.FindInstalledFiles()
94 raise UserError, "No source for Package() given"
96 # decide which types of packages shall be built. Can be defined through
97 # four mechanisms: command line argument, keyword argument,
98 # environment argument and default selection( zip or tar.gz ) in that
100 try: kw['PACKAGETYPE']=env['PACKAGETYPE']
101 except KeyError: pass
103 if not kw.get('PACKAGETYPE'):
104 from SCons.Script import GetOption
105 kw['PACKAGETYPE'] = GetOption('package_type')
107 if kw['PACKAGETYPE'] == None:
108 if env['BUILDERS'].has_key('Tar'):
109 kw['PACKAGETYPE']='targz'
110 elif env['BUILDERS'].has_key('Zip'):
111 kw['PACKAGETYPE']='zip'
113 raise UserError, "No type for Package() given"
115 PACKAGETYPE=kw['PACKAGETYPE']
116 if not is_List(PACKAGETYPE):
117 PACKAGETYPE=string.split(PACKAGETYPE, ',')
119 # load the needed packagers.
120 def load_packager(type):
122 file,path,desc=imp.find_module(type, __path__)
123 return imp.load_module(type, file, path, desc)
124 except ImportError, e:
125 raise EnvironmentError("packager %s not available: %s"%(type,str(e)))
127 packagers=map(load_packager, PACKAGETYPE)
129 # set up targets and the PACKAGEROOT
131 # fill up the target list with a default target name until the PACKAGETYPE
132 # list is of the same size as the target list.
133 if not target: target = []
135 size_diff = len(PACKAGETYPE)-len(target)
136 default_name = "%(NAME)s-%(VERSION)s"
139 default_target = default_name%kw
140 target.extend( [default_target]*size_diff )
142 if not kw.has_key('PACKAGEROOT'):
143 kw['PACKAGEROOT'] = default_name%kw
146 raise SCons.Errors.UserError( "Missing Packagetag '%s'"%e.args[0] )
148 # setup the source files
149 source=env.arg2nodes(source, env.fs.Entry)
151 # call the packager to setup the dependencies.
154 for packager in packagers:
156 t=apply(packager.package, [env,t,source], kw)
159 assert( len(target) == 0 )
162 raise SCons.Errors.UserError( "Missing Packagetag '%s' for %s packager"\
163 % (e.args[0],packager.__name__) )
165 # this exception means that a needed argument for the packager is
166 # missing. As our packagers get their "tags" as named function
167 # arguments we need to find out which one is missing.
168 from inspect import getargspec
169 args,varargs,varkw,defaults=getargspec(packager.package)
171 args=args[:-len(defaults)] # throw away arguments with default values
172 map(args.remove, 'env target source'.split())
173 # now remove any args for which we have a value in kw.
174 #args=[x for x in args if not kw.has_key(x)]
175 args=filter(lambda x, kw=kw: not kw.has_key(x), args)
178 raise # must be a different error, so reraise
180 raise SCons.Errors.UserError( "Missing Packagetag '%s' for %s packager"\
181 % (args[0],packager.__name__) )
183 raise SCons.Errors.UserError( "Missing Packagetags '%s' for %s packager"\
184 % (", ".join(args),packager.__name__) )
186 target=env.arg2nodes(target, env.fs.Entry)
187 targets.extend(env.Alias( 'package', targets ))
191 # SCons tool initialization functions
197 from SCons.Script import AddOption
201 AddOption('--package-type',
206 help='The type of package to create.')
209 env['BUILDERS']['Package']
210 env['BUILDERS']['Tag']
212 env['BUILDERS']['Package'] = Package
213 env['BUILDERS']['Tag'] = Tag
221 EnumVariable( 'PACKAGETYPE',
222 'the type of package to create.',
223 None, allowed_values=map( str, __all__ ),
229 # Internal utility functions
232 def copy_attr(f1, f2):
233 """ copies the special packaging file attributes from f1 to f2.
235 #pattrs = [x for x in dir(f1) if not hasattr(f2, x) and\
236 # x.startswith('PACKAGING_')]
237 copyit = lambda x, f2=f2: not hasattr(f2, x) and x[:10] == 'PACKAGING_'
238 pattrs = filter(copyit, dir(f1))
240 setattr(f2, attr, getattr(f1, attr))
241 def putintopackageroot(target, source, env, pkgroot, honor_install_location=1):
242 """ Uses the CopyAs builder to copy all source files to the directory given
245 If honor_install_location is set and the copied source file has an
246 PACKAGING_INSTALL_LOCATION attribute, the PACKAGING_INSTALL_LOCATION is
247 used as the new name of the source file under pkgroot.
249 The source file will not be copied if it is already under the the pkgroot
252 All attributes of the source file will be copied to the new file.
254 # make sure the packageroot is a Dir object.
255 if SCons.Util.is_String(pkgroot): pkgroot=env.Dir(pkgroot)
256 if not SCons.Util.is_List(source): source=[source]
260 if SCons.Util.is_String(file): file = env.File(file)
262 if file.is_under(pkgroot):
263 new_source.append(file)
265 if hasattr(file, 'PACKAGING_INSTALL_LOCATION') and\
266 honor_install_location:
267 new_name=make_path_relative(file.PACKAGING_INSTALL_LOCATION)
269 new_name=make_path_relative(file.get_path())
271 new_file=pkgroot.File(new_name)
272 new_file=env.CopyAs(new_file, file)[0]
273 copy_attr(file, new_file)
274 new_source.append(new_file)
276 return (target, new_source)
278 def stripinstallbuilder(target, source, env):
279 """ strips the install builder action from the source list and stores
280 the final installation location as the "PACKAGING_INSTALL_LOCATION" of
281 the source of the source file. This effectively removes the final installed
282 files from the source list while remembering the installation location.
284 It also warns about files which have no install builder attached.
286 def has_no_install_location(file):
287 return not (file.has_builder() and\
288 hasattr(file.builder, 'name') and\
289 (file.builder.name=="InstallBuilder" or\
290 file.builder.name=="InstallAsBuilder"))
292 if len(filter(has_no_install_location, source)):
293 warn(Warning, "there are files to package which have no\
294 InstallBuilder attached, this might lead to irreproducible packages")
298 if has_no_install_location(s):
304 setattr(ss, 'PACKAGING_INSTALL_LOCATION', s.get_path())
306 return (target, n_source)