blob: 6a4f3fa8de162260a87c8c27104c0a17cbb246d6 [file] [log] [blame]
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +00001"""tarball
2
3Tool-specific initialization for tarball.
4
5"""
Christopher Dunnf9864232007-06-14 21:01:26 +00006
7## Commands to tackle a command based implementation:
8##to unpack on the fly...
9##gunzip < FILE.tar.gz | tar xvf -
10##to pack on the fly...
11##tar cvf - FILE-LIST | gzip -c > FILE.tar.gz
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000012
13import os.path
14
15import SCons.Builder
16import SCons.Node.FS
17import SCons.Util
Christopher Dunnf9864232007-06-14 21:01:26 +000018
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000019try:
Christopher Dunnf9864232007-06-14 21:01:26 +000020 import gzip
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000021 import tarfile
22 internal_targz = 1
23except ImportError:
24 internal_targz = 0
Christopher Dunnf9864232007-06-14 21:01:26 +000025
26TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000027
28if internal_targz:
Christopher Dunnf9864232007-06-14 21:01:26 +000029 def targz(target, source, env):
Christopher Dunn494950a2015-01-24 15:29:52 -060030 def archive_name(path):
31 path = os.path.normpath(os.path.abspath(path))
32 common_path = os.path.commonprefix((base_dir, path))
Christopher Dunnf9864232007-06-14 21:01:26 +000033 archive_name = path[len(common_path):]
34 return archive_name
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000035
36 def visit(tar, dirname, names):
37 for name in names:
38 path = os.path.join(dirname, name)
39 if os.path.isfile(path):
Christopher Dunn494950a2015-01-24 15:29:52 -060040 tar.add(path, archive_name(path))
Christopher Dunnf9864232007-06-14 21:01:26 +000041 compression = env.get('TARGZ_COMPRESSION_LEVEL',TARGZ_DEFAULT_COMPRESSION_LEVEL)
Christopher Dunn494950a2015-01-24 15:29:52 -060042 base_dir = os.path.normpath(env.get('TARGZ_BASEDIR', env.Dir('.')).abspath)
Christopher Dunnf9864232007-06-14 21:01:26 +000043 target_path = str(target[0])
Christopher Dunn494950a2015-01-24 15:29:52 -060044 fileobj = gzip.GzipFile(target_path, 'wb', compression)
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000045 tar = tarfile.TarFile(os.path.splitext(target_path)[0], 'w', fileobj)
Christopher Dunnf9864232007-06-14 21:01:26 +000046 for source in source:
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000047 source_path = str(source)
48 if source.isdir():
49 os.path.walk(source_path, visit, tar)
Christopher Dunnf9864232007-06-14 21:01:26 +000050 else:
Christopher Dunn494950a2015-01-24 15:29:52 -060051 tar.add(source_path, archive_name(source_path)) # filename, arcname
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000052 tar.close()
53
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000054 targzAction = SCons.Action.Action(targz, varlist=['TARGZ_COMPRESSION_LEVEL','TARGZ_BASEDIR'])
Christopher Dunnf9864232007-06-14 21:01:26 +000055
Christopher Dunn494950a2015-01-24 15:29:52 -060056 def makeBuilder(emitter = None):
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000057 return SCons.Builder.Builder(action = SCons.Action.Action('$TARGZ_COM', '$TARGZ_COMSTR'),
58 source_factory = SCons.Node.FS.Entry,
59 source_scanner = SCons.Defaults.DirScanner,
60 suffix = '$TARGZ_SUFFIX',
61 multi = 1)
62 TarGzBuilder = makeBuilder()
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000063
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000064 def generate(env):
65 """Add Builders and construction variables for zip to an Environment.
66 The following environnement variables may be set:
67 TARGZ_COMPRESSION_LEVEL: integer, [0-9]. 0: no compression, 9: best compression (same as gzip compression level).
68 TARGZ_BASEDIR: base-directory used to determine archive name (this allow archive name to be relative
69 to something other than top-dir).
70 """
71 env['BUILDERS']['TarGz'] = TarGzBuilder
72 env['TARGZ_COM'] = targzAction
73 env['TARGZ_COMPRESSION_LEVEL'] = TARGZ_DEFAULT_COMPRESSION_LEVEL # range 0-9
74 env['TARGZ_SUFFIX'] = '.tar.gz'
75 env['TARGZ_BASEDIR'] = env.Dir('.') # Sources archive name are made relative to that directory.
76else:
77 def generate(env):
78 pass
79
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000080
81def exists(env):
82 return internal_targz