blob: a655b11d374530a3998edfd035aa9f62d46e799d [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):
30 def archive_name( path ):
31 path = os.path.normpath( os.path.abspath( path ) )
32 common_path = os.path.commonprefix( (base_dir, path) )
33 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):
40 tar.add(path, archive_name(path) )
Christopher Dunnf9864232007-06-14 21:01:26 +000041 compression = env.get('TARGZ_COMPRESSION_LEVEL',TARGZ_DEFAULT_COMPRESSION_LEVEL)
42 base_dir = os.path.normpath( env.get('TARGZ_BASEDIR', env.Dir('.')).abspath )
43 target_path = str(target[0])
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000044 fileobj = gzip.GzipFile( target_path, 'wb', compression )
45 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:
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000051 tar.add(source_path, archive_name(source_path) ) # filename, arcname
52 tar.close()
53
54targzAction = SCons.Action.Action(targz, varlist=['TARGZ_COMPRESSION_LEVEL','TARGZ_BASEDIR'])
Christopher Dunnf9864232007-06-14 21:01:26 +000055
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000056def makeBuilder( emitter = None ):
Christopher Dunnf9864232007-06-14 21:01:26 +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)
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000062TarGzBuilder = makeBuilder()
63
64def generate(env):
Christopher Dunnf9864232007-06-14 21:01:26 +000065 """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).
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000070 """
Christopher Dunnf9864232007-06-14 21:01:26 +000071 env['BUILDERS']['TarGz'] = TarGzBuilder
72 env['TARGZ_COM'] = targzAction
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000073 env['TARGZ_COMPRESSION_LEVEL'] = TARGZ_DEFAULT_COMPRESSION_LEVEL # range 0-9
Christopher Dunnf9864232007-06-14 21:01:26 +000074 env['TARGZ_SUFFIX'] = '.tar.gz'
Baptiste Lepilleur4cd8bae2007-03-15 22:11:38 +000075 env['TARGZ_BASEDIR'] = env.Dir('.') # Sources archive name are made relative to that directory.
76
77def exists(env):
78 return internal_targz