2007-03-15 22:11:38 +00:00
|
|
|
"""tarball
|
|
|
|
|
|
|
|
Tool-specific initialization for tarball.
|
|
|
|
|
|
|
|
"""
|
2007-06-14 21:01:26 +00:00
|
|
|
|
|
|
|
## Commands to tackle a command based implementation:
|
|
|
|
##to unpack on the fly...
|
|
|
|
##gunzip < FILE.tar.gz | tar xvf -
|
|
|
|
##to pack on the fly...
|
|
|
|
##tar cvf - FILE-LIST | gzip -c > FILE.tar.gz
|
2007-03-15 22:11:38 +00:00
|
|
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
import SCons.Builder
|
|
|
|
import SCons.Node.FS
|
|
|
|
import SCons.Util
|
2007-06-14 21:01:26 +00:00
|
|
|
|
2007-03-15 22:11:38 +00:00
|
|
|
try:
|
2007-06-14 21:01:26 +00:00
|
|
|
import gzip
|
2007-03-15 22:11:38 +00:00
|
|
|
import tarfile
|
|
|
|
internal_targz = 1
|
|
|
|
except ImportError:
|
|
|
|
internal_targz = 0
|
2007-06-14 21:01:26 +00:00
|
|
|
|
|
|
|
TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
|
2007-03-15 22:11:38 +00:00
|
|
|
|
|
|
|
if internal_targz:
|
2007-06-14 21:01:26 +00:00
|
|
|
def targz(target, source, env):
|
|
|
|
def archive_name( path ):
|
|
|
|
path = os.path.normpath( os.path.abspath( path ) )
|
|
|
|
common_path = os.path.commonprefix( (base_dir, path) )
|
|
|
|
archive_name = path[len(common_path):]
|
|
|
|
return archive_name
|
2007-03-15 22:11:38 +00:00
|
|
|
|
|
|
|
def visit(tar, dirname, names):
|
|
|
|
for name in names:
|
|
|
|
path = os.path.join(dirname, name)
|
|
|
|
if os.path.isfile(path):
|
|
|
|
tar.add(path, archive_name(path) )
|
2007-06-14 21:01:26 +00:00
|
|
|
compression = env.get('TARGZ_COMPRESSION_LEVEL',TARGZ_DEFAULT_COMPRESSION_LEVEL)
|
|
|
|
base_dir = os.path.normpath( env.get('TARGZ_BASEDIR', env.Dir('.')).abspath )
|
|
|
|
target_path = str(target[0])
|
2007-03-15 22:11:38 +00:00
|
|
|
fileobj = gzip.GzipFile( target_path, 'wb', compression )
|
|
|
|
tar = tarfile.TarFile(os.path.splitext(target_path)[0], 'w', fileobj)
|
2007-06-14 21:01:26 +00:00
|
|
|
for source in source:
|
2007-03-15 22:11:38 +00:00
|
|
|
source_path = str(source)
|
|
|
|
if source.isdir():
|
|
|
|
os.path.walk(source_path, visit, tar)
|
2007-06-14 21:01:26 +00:00
|
|
|
else:
|
2007-03-15 22:11:38 +00:00
|
|
|
tar.add(source_path, archive_name(source_path) ) # filename, arcname
|
|
|
|
tar.close()
|
|
|
|
|
2009-11-19 13:05:54 +00:00
|
|
|
targzAction = SCons.Action.Action(targz, varlist=['TARGZ_COMPRESSION_LEVEL','TARGZ_BASEDIR'])
|
2007-06-14 21:01:26 +00:00
|
|
|
|
2009-11-19 13:05:54 +00:00
|
|
|
def makeBuilder( emitter = None ):
|
|
|
|
return SCons.Builder.Builder(action = SCons.Action.Action('$TARGZ_COM', '$TARGZ_COMSTR'),
|
|
|
|
source_factory = SCons.Node.FS.Entry,
|
|
|
|
source_scanner = SCons.Defaults.DirScanner,
|
|
|
|
suffix = '$TARGZ_SUFFIX',
|
|
|
|
multi = 1)
|
|
|
|
TarGzBuilder = makeBuilder()
|
|
|
|
|
|
|
|
def generate(env):
|
|
|
|
"""Add Builders and construction variables for zip to an Environment.
|
|
|
|
The following environnement variables may be set:
|
|
|
|
TARGZ_COMPRESSION_LEVEL: integer, [0-9]. 0: no compression, 9: best compression (same as gzip compression level).
|
|
|
|
TARGZ_BASEDIR: base-directory used to determine archive name (this allow archive name to be relative
|
|
|
|
to something other than top-dir).
|
|
|
|
"""
|
|
|
|
env['BUILDERS']['TarGz'] = TarGzBuilder
|
|
|
|
env['TARGZ_COM'] = targzAction
|
|
|
|
env['TARGZ_COMPRESSION_LEVEL'] = TARGZ_DEFAULT_COMPRESSION_LEVEL # range 0-9
|
|
|
|
env['TARGZ_SUFFIX'] = '.tar.gz'
|
|
|
|
env['TARGZ_BASEDIR'] = env.Dir('.') # Sources archive name are made relative to that directory.
|
|
|
|
else:
|
|
|
|
def generate(env):
|
|
|
|
pass
|
2007-03-15 22:11:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def exists(env):
|
|
|
|
return internal_targz
|