blob: 8e5ba83003b33c7663c7963372d0fe3ffc7df845 [file] [log] [blame]
Sam Clegg63860612015-04-09 18:01:33 -07001# Copyright 2007 Baptiste Lepilleur
2# Distributed under MIT license, or public domain if desired and
3# recognized in your jurisdiction.
4# See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
5
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +00006"""tarball
7
8Tool-specific initialization for tarball.
9
10"""
Christopher Dunnf9864232007-06-14 21:01:26 +000011
12## Commands to tackle a command based implementation:
13##to unpack on the fly...
14##gunzip < FILE.tar.gz | tar xvf -
15##to pack on the fly...
16##tar cvf - FILE-LIST | gzip -c > FILE.tar.gz
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000017
18import os.path
19
20import SCons.Builder
21import SCons.Node.FS
22import SCons.Util
Christopher Dunnf9864232007-06-14 21:01:26 +000023
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000024try:
Christopher Dunnf9864232007-06-14 21:01:26 +000025 import gzip
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000026 import tarfile
27 internal_targz = 1
28except ImportError:
29 internal_targz = 0
Christopher Dunnf9864232007-06-14 21:01:26 +000030
31TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000032
33if internal_targz:
Christopher Dunnf9864232007-06-14 21:01:26 +000034 def targz(target, source, env):
Christopher Dunn494950a2015-01-24 15:29:52 -060035 def archive_name(path):
36 path = os.path.normpath(os.path.abspath(path))
37 common_path = os.path.commonprefix((base_dir, path))
Christopher Dunnf9864232007-06-14 21:01:26 +000038 archive_name = path[len(common_path):]
39 return archive_name
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000040
41 def visit(tar, dirname, names):
42 for name in names:
43 path = os.path.join(dirname, name)
44 if os.path.isfile(path):
Christopher Dunn494950a2015-01-24 15:29:52 -060045 tar.add(path, archive_name(path))
Christopher Dunnf9864232007-06-14 21:01:26 +000046 compression = env.get('TARGZ_COMPRESSION_LEVEL',TARGZ_DEFAULT_COMPRESSION_LEVEL)
Christopher Dunn494950a2015-01-24 15:29:52 -060047 base_dir = os.path.normpath(env.get('TARGZ_BASEDIR', env.Dir('.')).abspath)
Christopher Dunnf9864232007-06-14 21:01:26 +000048 target_path = str(target[0])
Christopher Dunn494950a2015-01-24 15:29:52 -060049 fileobj = gzip.GzipFile(target_path, 'wb', compression)
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000050 tar = tarfile.TarFile(os.path.splitext(target_path)[0], 'w', fileobj)
Christopher Dunnf9864232007-06-14 21:01:26 +000051 for source in source:
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000052 source_path = str(source)
53 if source.isdir():
54 os.path.walk(source_path, visit, tar)
Christopher Dunnf9864232007-06-14 21:01:26 +000055 else:
Christopher Dunn494950a2015-01-24 15:29:52 -060056 tar.add(source_path, archive_name(source_path)) # filename, arcname
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000057 tar.close()
58
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000059 targzAction = SCons.Action.Action(targz, varlist=['TARGZ_COMPRESSION_LEVEL','TARGZ_BASEDIR'])
Christopher Dunnf9864232007-06-14 21:01:26 +000060
Christopher Dunn494950a2015-01-24 15:29:52 -060061 def makeBuilder(emitter = None):
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000062 return SCons.Builder.Builder(action = SCons.Action.Action('$TARGZ_COM', '$TARGZ_COMSTR'),
63 source_factory = SCons.Node.FS.Entry,
64 source_scanner = SCons.Defaults.DirScanner,
65 suffix = '$TARGZ_SUFFIX',
66 multi = 1)
67 TarGzBuilder = makeBuilder()
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000068
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000069 def generate(env):
70 """Add Builders and construction variables for zip to an Environment.
71 The following environnement variables may be set:
72 TARGZ_COMPRESSION_LEVEL: integer, [0-9]. 0: no compression, 9: best compression (same as gzip compression level).
73 TARGZ_BASEDIR: base-directory used to determine archive name (this allow archive name to be relative
74 to something other than top-dir).
75 """
76 env['BUILDERS']['TarGz'] = TarGzBuilder
77 env['TARGZ_COM'] = targzAction
78 env['TARGZ_COMPRESSION_LEVEL'] = TARGZ_DEFAULT_COMPRESSION_LEVEL # range 0-9
79 env['TARGZ_SUFFIX'] = '.tar.gz'
80 env['TARGZ_BASEDIR'] = env.Dir('.') # Sources archive name are made relative to that directory.
81else:
82 def generate(env):
83 pass
84
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000085
86def exists(env):
87 return internal_targz