blob: 3c0ba65e711c07172f562eac0960af9abf289f3e [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001# Copyright 2010 Baptiste Lepilleur and The JsonCpp Authors
Sam Clegg63860612015-04-09 18:01:33 -07002# 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 Dunn9cc0bb82015-01-24 15:44:51 -06006from contextlib import closing
Christopher Dunnf3576882015-01-24 16:20:25 -06007import os
Christopher Dunndc0f7362011-06-21 21:18:49 +00008import tarfile
9
10TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
11
12def 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 Dunn494950a2015-01-24 15:29:52 -060021 base_dir = os.path.normpath(os.path.abspath(base_dir))
22 def archive_name(path):
Christopher Dunndc0f7362011-06-21 21:18:49 +000023 """Makes path relative to base_dir."""
Christopher Dunn494950a2015-01-24 15:29:52 -060024 path = os.path.normpath(os.path.abspath(path))
25 common_path = os.path.commonprefix((base_dir, path))
Christopher Dunndc0f7362011-06-21 21:18:49 +000026 archive_name = path[len(common_path):]
Christopher Dunn494950a2015-01-24 15:29:52 -060027 if os.path.isabs(archive_name):
Christopher Dunndc0f7362011-06-21 21:18:49 +000028 archive_name = archive_name[1:]
Christopher Dunn494950a2015-01-24 15:29:52 -060029 return os.path.join(prefix_dir, archive_name)
Christopher Dunndc0f7362011-06-21 21:18:49 +000030 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 Dunn494950a2015-01-24 15:29:52 -060035 tar.add(path, path_in_tar)
Christopher Dunndc0f7362011-06-21 21:18:49 +000036 compression = TARGZ_DEFAULT_COMPRESSION_LEVEL
Christopher Dunn9cc0bb82015-01-24 15:44:51 -060037 with closing(tarfile.TarFile.open(tarball_path, 'w:gz',
38 compresslevel=compression)) as tar:
Christopher Dunndc0f7362011-06-21 21:18:49 +000039 for source in sources:
40 source_path = source
Christopher Dunn494950a2015-01-24 15:29:52 -060041 if os.path.isdir(source):
Christopher Dunnf3576882015-01-24 16:20:25 -060042 for dirpath, dirnames, filenames in os.walk(source_path):
43 visit(tar, dirpath, filenames)
Christopher Dunndc0f7362011-06-21 21:18:49 +000044 else:
45 path_in_tar = archive_name(source_path)
Christopher Dunn494950a2015-01-24 15:29:52 -060046 tar.add(source_path, path_in_tar) # filename, arcname
Christopher Dunndc0f7362011-06-21 21:18:49 +000047
Christopher Dunn494950a2015-01-24 15:29:52 -060048def decompress(tarball_path, base_dir):
Christopher Dunndc0f7362011-06-21 21:18:49 +000049 """Decompress the gzipped tarball into directory base_dir.
50 """
Christopher Dunn9cc0bb82015-01-24 15:44:51 -060051 with closing(tarfile.TarFile.open(tarball_path)) as tar:
Christopher Dunn494950a2015-01-24 15:29:52 -060052 tar.extractall(base_dir)