Sam Clegg | 6386061 | 2015-04-09 18:01:33 -0700 | [diff] [blame] | 1 | # 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 Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 6 | """tarball |
| 7 | |
| 8 | Tool-specific initialization for tarball. |
| 9 | |
| 10 | """ |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 11 | |
| 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 Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 17 | |
| 18 | import os.path |
| 19 | |
| 20 | import SCons.Builder |
| 21 | import SCons.Node.FS |
| 22 | import SCons.Util |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 23 | |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 24 | try: |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 25 | import gzip |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 26 | import tarfile |
| 27 | internal_targz = 1 |
| 28 | except ImportError: |
| 29 | internal_targz = 0 |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 30 | |
| 31 | TARGZ_DEFAULT_COMPRESSION_LEVEL = 9 |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 32 | |
| 33 | if internal_targz: |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 34 | def targz(target, source, env): |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 35 | def archive_name(path): |
| 36 | path = os.path.normpath(os.path.abspath(path)) |
| 37 | common_path = os.path.commonprefix((base_dir, path)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 38 | archive_name = path[len(common_path):] |
| 39 | return archive_name |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 40 | |
| 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 Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 45 | tar.add(path, archive_name(path)) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 46 | compression = env.get('TARGZ_COMPRESSION_LEVEL',TARGZ_DEFAULT_COMPRESSION_LEVEL) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 47 | base_dir = os.path.normpath(env.get('TARGZ_BASEDIR', env.Dir('.')).abspath) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 48 | target_path = str(target[0]) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 49 | fileobj = gzip.GzipFile(target_path, 'wb', compression) |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 50 | tar = tarfile.TarFile(os.path.splitext(target_path)[0], 'w', fileobj) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 51 | for source in source: |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 52 | source_path = str(source) |
| 53 | if source.isdir(): |
| 54 | os.path.walk(source_path, visit, tar) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 55 | else: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 56 | tar.add(source_path, archive_name(source_path)) # filename, arcname |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 57 | tar.close() |
| 58 | |
Baptiste Lepilleur | 86ccb76 | 2009-11-19 13:05:54 +0000 | [diff] [blame] | 59 | targzAction = SCons.Action.Action(targz, varlist=['TARGZ_COMPRESSION_LEVEL','TARGZ_BASEDIR']) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 60 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 61 | def makeBuilder(emitter = None): |
Baptiste Lepilleur | 86ccb76 | 2009-11-19 13:05:54 +0000 | [diff] [blame] | 62 | 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 Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 68 | |
Baptiste Lepilleur | 86ccb76 | 2009-11-19 13:05:54 +0000 | [diff] [blame] | 69 | 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. |
| 81 | else: |
| 82 | def generate(env): |
| 83 | pass |
| 84 | |
Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame] | 85 | |
| 86 | def exists(env): |
| 87 | return internal_targz |