mapnik/scons/scons-local-4.1.0/SCons/Tool/packaging/__init__.py

329 lines
10 KiB
Python
Raw Normal View History

2015-10-01 15:25:41 +02:00
#
2021-03-05 11:18:26 +01:00
# __COPYRIGHT__
2015-10-01 15:25:41 +02:00
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2021-03-05 11:18:26 +01:00
"""SCons.Tool.Packaging
SCons Packaging Tool.
"""
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
import importlib
from inspect import getfullargspec
2015-10-01 15:25:41 +02:00
2021-03-05 11:18:26 +01:00
import SCons.Defaults
2015-10-01 15:25:41 +02:00
import SCons.Environment
2021-03-05 11:18:26 +01:00
from SCons.Errors import UserError, SConsEnvironmentError
from SCons.Script import AddOption, GetOption
2015-10-01 15:25:41 +02:00
from SCons.Util import is_List, make_path_relative
2021-03-05 11:18:26 +01:00
from SCons.Variables import EnumVariable
from SCons.Warnings import warn, SConsWarning
2015-10-01 15:25:41 +02:00
2021-03-05 11:18:26 +01:00
__all__ = [
'src_targz', 'src_tarbz2', 'src_tarxz', 'src_zip',
'targz', 'tarbz2', 'tarxz', 'zip',
'rpm', 'msi', 'ipk',
]
2015-10-01 15:25:41 +02:00
#
# Utility and Builder function
#
def Tag(env, target, source, *more_tags, **kw_tags):
2021-03-05 11:18:26 +01:00
""" Tag a file with the given arguments.
Just sets the accordingly named attribute on the file object.
2015-10-01 15:25:41 +02:00
TODO: FIXME
"""
if not target:
2021-03-05 11:18:26 +01:00
target = source
first_tag = None
2015-10-01 15:25:41 +02:00
else:
2021-03-05 11:18:26 +01:00
first_tag = source
2015-10-01 15:25:41 +02:00
if first_tag:
kw_tags[first_tag[0]] = ''
2021-03-05 11:18:26 +01:00
if not kw_tags and not more_tags:
2015-10-01 15:25:41 +02:00
raise UserError("No tags given.")
# XXX: sanity checks
for x in more_tags:
kw_tags[x] = ''
if not SCons.Util.is_List(target):
2021-03-05 11:18:26 +01:00
target = [target]
2015-10-01 15:25:41 +02:00
else:
2021-03-05 11:18:26 +01:00
# hmm, sometimes the target list is a list of a list
2015-10-01 15:25:41 +02:00
# make sure it is flattened prior to processing.
# TODO: perhaps some bug ?!?
2021-03-05 11:18:26 +01:00
target = env.Flatten(target)
2015-10-01 15:25:41 +02:00
for t in target:
2021-03-05 11:18:26 +01:00
for k, v in kw_tags.items():
2015-10-01 15:25:41 +02:00
# all file tags have to start with PACKAGING_, so we can later
# differentiate between "normal" object attributes and the
# packaging attributes. As the user should not be bothered with
# that, the prefix will be added here if missing.
2021-03-05 11:18:26 +01:00
if not k.startswith('PACKAGING_'):
k = 'PACKAGING_' + k
2015-10-01 15:25:41 +02:00
t.Tag(k, v)
def Package(env, target=None, source=None, **kw):
""" Entry point for the package tool.
"""
2021-03-05 11:18:26 +01:00
# check if we need to find the source files ourselves
2015-10-01 15:25:41 +02:00
if not source:
source = env.FindInstalledFiles()
2021-03-05 11:18:26 +01:00
if not source:
2015-10-01 15:25:41 +02:00
raise UserError("No source for Package() given")
# decide which types of packages shall be built. Can be defined through
# four mechanisms: command line argument, keyword argument,
2021-03-05 11:18:26 +01:00
# environment argument and default selection (zip or tar.gz) in that
2015-10-01 15:25:41 +02:00
# order.
2021-03-05 11:18:26 +01:00
try:
kw['PACKAGETYPE'] = env['PACKAGETYPE']
except KeyError:
pass
2015-10-01 15:25:41 +02:00
if not kw.get('PACKAGETYPE'):
kw['PACKAGETYPE'] = GetOption('package_type')
2021-03-05 11:18:26 +01:00
if kw['PACKAGETYPE'] is None:
2015-10-01 15:25:41 +02:00
if 'Tar' in env['BUILDERS']:
2021-03-05 11:18:26 +01:00
kw['PACKAGETYPE'] = 'targz'
2015-10-01 15:25:41 +02:00
elif 'Zip' in env['BUILDERS']:
2021-03-05 11:18:26 +01:00
kw['PACKAGETYPE'] = 'zip'
2015-10-01 15:25:41 +02:00
else:
raise UserError("No type for Package() given")
2021-03-05 11:18:26 +01:00
packagetype = kw['PACKAGETYPE']
if not is_List(packagetype):
packagetype = packagetype.split(',')
2015-10-01 15:25:41 +02:00
# load the needed packagers.
2021-03-05 11:18:26 +01:00
def load_packager(pkgtype):
2015-10-01 15:25:41 +02:00
try:
2021-03-05 11:18:26 +01:00
# the specific packager is a relative import
return importlib.import_module("." + pkgtype, __name__)
2017-11-16 11:59:55 +01:00
except ImportError as e:
2021-03-05 11:18:26 +01:00
raise SConsEnvironmentError("packager %s not available: %s"
% (pkgtype, str(e)))
2015-10-01 15:25:41 +02:00
2021-03-05 11:18:26 +01:00
packagers = [load_packager(p) for p in packagetype]
2015-10-01 15:25:41 +02:00
# set up targets and the PACKAGEROOT
try:
2021-03-05 11:18:26 +01:00
# fill up the target list with a default target name until the
# PACKAGETYPE list is of the same size as the target list.
if not target:
target = []
2015-10-01 15:25:41 +02:00
2021-03-05 11:18:26 +01:00
size_diff = len(packagetype) - len(target)
default_name = "%(NAME)s-%(VERSION)s"
2015-10-01 15:25:41 +02:00
2021-03-05 11:18:26 +01:00
if size_diff > 0:
default_target = default_name % kw
target.extend([default_target] * size_diff)
2015-10-01 15:25:41 +02:00
if 'PACKAGEROOT' not in kw:
2021-03-05 11:18:26 +01:00
kw['PACKAGEROOT'] = default_name % kw
2015-10-01 15:25:41 +02:00
2017-11-16 11:59:55 +01:00
except KeyError as e:
2021-03-05 11:18:26 +01:00
raise UserError("Missing Packagetag '%s'" % e.args[0])
2015-10-01 15:25:41 +02:00
# setup the source files
2021-03-05 11:18:26 +01:00
source = env.arg2nodes(source, env.fs.Entry)
2015-10-01 15:25:41 +02:00
# call the packager to setup the dependencies.
2021-03-05 11:18:26 +01:00
targets = []
2015-10-01 15:25:41 +02:00
try:
for packager in packagers:
2021-03-05 11:18:26 +01:00
t = [target.pop(0)]
t = packager.package(env, t, source, **kw)
2015-10-01 15:25:41 +02:00
targets.extend(t)
2021-03-05 11:18:26 +01:00
assert len(target) == 0
2015-10-01 15:25:41 +02:00
2017-11-16 11:59:55 +01:00
except KeyError as e:
2021-03-05 11:18:26 +01:00
raise UserError("Missing Packagetag '%s' for %s packager"
% (e.args[0], packager.__name__))
except TypeError:
2015-10-01 15:25:41 +02:00
# this exception means that a needed argument for the packager is
# missing. As our packagers get their "tags" as named function
# arguments we need to find out which one is missing.
2021-03-05 11:18:26 +01:00
argspec = getfullargspec(packager.package)
args = argspec.args
if argspec.defaults:
# throw away arguments with default values
args = args[:-len(argspec.defaults)]
2015-10-01 15:25:41 +02:00
args.remove('env')
args.remove('target')
args.remove('source')
# now remove any args for which we have a value in kw.
2021-03-05 11:18:26 +01:00
args = [x for x in args if x not in kw]
if not args:
raise # must be a different error, so re-raise
elif len(args) == 1:
raise UserError("Missing Packagetag '%s' for %s packager"
% (args[0], packager.__name__))
raise UserError("Missing Packagetags '%s' for %s packager"
% (", ".join(args), packager.__name__))
target = env.arg2nodes(target, env.fs.Entry)
#XXX: target set above unused... what was the intent?
targets.extend(env.Alias('package', targets))
2015-10-01 15:25:41 +02:00
return targets
#
# SCons tool initialization functions
#
2021-03-05 11:18:26 +01:00
added = False
2015-10-01 15:25:41 +02:00
def generate(env):
global added
if not added:
2021-03-05 11:18:26 +01:00
added = True
2015-10-01 15:25:41 +02:00
AddOption('--package-type',
dest='package_type',
default=None,
type="string",
action="store",
help='The type of package to create.')
try:
env['BUILDERS']['Package']
env['BUILDERS']['Tag']
except KeyError:
env['BUILDERS']['Package'] = Package
env['BUILDERS']['Tag'] = Tag
def exists(env):
return 1
# XXX
def options(opts):
opts.AddVariables(
2021-03-05 11:18:26 +01:00
EnumVariable('PACKAGETYPE',
2015-10-01 15:25:41 +02:00
'the type of package to create.',
None, allowed_values=list(map( str, __all__ )),
ignorecase=2
2021-03-05 11:18:26 +01:00
)
2015-10-01 15:25:41 +02:00
)
#
# Internal utility functions
#
def copy_attr(f1, f2):
2021-03-05 11:18:26 +01:00
""" Copies the special packaging file attributes from f1 to f2.
2015-10-01 15:25:41 +02:00
"""
if f1._tags:
2021-03-05 11:18:26 +01:00
pattrs = [
tag
for tag in f1._tags
if not hasattr(f2, tag) and tag.startswith('PACKAGING_')
]
2015-10-01 15:25:41 +02:00
for attr in pattrs:
f2.Tag(attr, f1.GetTag(attr))
def putintopackageroot(target, source, env, pkgroot, honor_install_location=1):
2021-03-05 11:18:26 +01:00
""" Copies all source files to the directory given in pkgroot.
2015-10-01 15:25:41 +02:00
If honor_install_location is set and the copied source file has an
PACKAGING_INSTALL_LOCATION attribute, the PACKAGING_INSTALL_LOCATION is
used as the new name of the source file under pkgroot.
The source file will not be copied if it is already under the the pkgroot
directory.
All attributes of the source file will be copied to the new file.
2021-03-05 11:18:26 +01:00
Note:
Uses CopyAs builder.
2015-10-01 15:25:41 +02:00
"""
# make sure the packageroot is a Dir object.
2021-03-05 11:18:26 +01:00
if SCons.Util.is_String(pkgroot):
pkgroot = env.Dir(pkgroot)
if not SCons.Util.is_List(source):
source = [source]
2015-10-01 15:25:41 +02:00
new_source = []
for file in source:
2021-03-05 11:18:26 +01:00
if SCons.Util.is_String(file):
file = env.File(file)
2015-10-01 15:25:41 +02:00
if file.is_under(pkgroot):
new_source.append(file)
else:
2021-03-05 11:18:26 +01:00
if file.GetTag('PACKAGING_INSTALL_LOCATION') and \
2015-10-01 15:25:41 +02:00
honor_install_location:
2021-03-05 11:18:26 +01:00
new_name = make_path_relative(file.GetTag('PACKAGING_INSTALL_LOCATION'))
2015-10-01 15:25:41 +02:00
else:
2021-03-05 11:18:26 +01:00
new_name = make_path_relative(file.get_path())
2015-10-01 15:25:41 +02:00
2021-03-05 11:18:26 +01:00
new_file = pkgroot.File(new_name)
new_file = env.CopyAs(new_file, file)[0]
2015-10-01 15:25:41 +02:00
copy_attr(file, new_file)
new_source.append(new_file)
2021-03-05 11:18:26 +01:00
return target, new_source
2015-10-01 15:25:41 +02:00
def stripinstallbuilder(target, source, env):
2016-08-05 11:16:06 +02:00
""" Strips the install builder action from the source list and stores
2015-10-01 15:25:41 +02:00
the final installation location as the "PACKAGING_INSTALL_LOCATION" of
the source of the source file. This effectively removes the final installed
files from the source list while remembering the installation location.
It also warns about files which have no install builder attached.
"""
def has_no_install_location(file):
2021-03-05 11:18:26 +01:00
return not (file.has_builder() and hasattr(file.builder, 'name')
and file.builder.name in ["InstallBuilder", "InstallAsBuilder"])
2015-10-01 15:25:41 +02:00
2017-11-16 11:59:55 +01:00
if len([src for src in source if has_no_install_location(src)]):
2021-03-05 11:18:26 +01:00
warn(SConsWarning, "there are files to package which have no\
2015-10-01 15:25:41 +02:00
InstallBuilder attached, this might lead to irreproducible packages")
2021-03-05 11:18:26 +01:00
n_source = []
2015-10-01 15:25:41 +02:00
for s in source:
if has_no_install_location(s):
n_source.append(s)
else:
for ss in s.sources:
n_source.append(ss)
copy_attr(s, ss)
ss.Tag('PACKAGING_INSTALL_LOCATION', s.get_path())
2021-03-05 11:18:26 +01:00
return target, n_source
2015-10-01 15:25:41 +02:00
# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: