Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | # Copyright 2010 Baptiste Lepilleur and The JsonCpp Authors |
Sam Clegg | 6386061 | 2015-04-09 18:01:33 -0700 | [diff] [blame] | 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 | |
Christopher Dunn | 9cc0bb8 | 2015-01-24 15:44:51 -0600 | [diff] [blame] | 6 | from contextlib import closing |
Christopher Dunn | f357688 | 2015-01-24 16:20:25 -0600 | [diff] [blame] | 7 | import os |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 8 | import tarfile |
| 9 | |
| 10 | TARGZ_DEFAULT_COMPRESSION_LEVEL = 9 |
| 11 | |
| 12 | def make_tarball(tarball_path, sources, base_dir, prefix_dir=''): |
| 13 | """Parameters: |
| 14 | tarball_path: output path of the .tar.gz file |
| 15 | sources: list of sources to include in the tarball, relative to the current directory |
| 16 | base_dir: if a source file is in a sub-directory of base_dir, then base_dir is stripped |
| 17 | from path in the tarball. |
| 18 | prefix_dir: all files stored in the tarball be sub-directory of prefix_dir. Set to '' |
| 19 | to make them child of root. |
| 20 | """ |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 21 | base_dir = os.path.normpath(os.path.abspath(base_dir)) |
| 22 | def archive_name(path): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 23 | """Makes path relative to base_dir.""" |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 24 | path = os.path.normpath(os.path.abspath(path)) |
| 25 | common_path = os.path.commonprefix((base_dir, path)) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 26 | archive_name = path[len(common_path):] |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 27 | if os.path.isabs(archive_name): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 28 | archive_name = archive_name[1:] |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 29 | return os.path.join(prefix_dir, archive_name) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 30 | def visit(tar, dirname, names): |
| 31 | for name in names: |
| 32 | path = os.path.join(dirname, name) |
| 33 | if os.path.isfile(path): |
| 34 | path_in_tar = archive_name(path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 35 | tar.add(path, path_in_tar) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 36 | compression = TARGZ_DEFAULT_COMPRESSION_LEVEL |
Christopher Dunn | 9cc0bb8 | 2015-01-24 15:44:51 -0600 | [diff] [blame] | 37 | with closing(tarfile.TarFile.open(tarball_path, 'w:gz', |
| 38 | compresslevel=compression)) as tar: |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 39 | for source in sources: |
| 40 | source_path = source |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 41 | if os.path.isdir(source): |
Christopher Dunn | f357688 | 2015-01-24 16:20:25 -0600 | [diff] [blame] | 42 | for dirpath, dirnames, filenames in os.walk(source_path): |
| 43 | visit(tar, dirpath, filenames) |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 44 | else: |
| 45 | path_in_tar = archive_name(source_path) |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 46 | tar.add(source_path, path_in_tar) # filename, arcname |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 47 | |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 48 | def decompress(tarball_path, base_dir): |
Christopher Dunn | dc0f736 | 2011-06-21 21:18:49 +0000 | [diff] [blame] | 49 | """Decompress the gzipped tarball into directory base_dir. |
| 50 | """ |
Christopher Dunn | 9cc0bb8 | 2015-01-24 15:44:51 -0600 | [diff] [blame] | 51 | with closing(tarfile.TarFile.open(tarball_path)) as tar: |
Christopher Dunn | 494950a | 2015-01-24 15:29:52 -0600 | [diff] [blame] | 52 | tar.extractall(base_dir) |