blob: 3c0ba65e711c07172f562eac0960af9abf289f3e [file] [log] [blame]
Haibo Huangb0bee822021-02-24 15:40:15 -08001# Copyright 2010 Baptiste Lepilleur and The JsonCpp Authors
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
6from contextlib import closing
7import os
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -05008import 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 """
Haibo Huangb0bee822021-02-24 15:40:15 -080021 base_dir = os.path.normpath(os.path.abspath(base_dir))
22 def archive_name(path):
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050023 """Makes path relative to base_dir."""
Haibo Huangb0bee822021-02-24 15:40:15 -080024 path = os.path.normpath(os.path.abspath(path))
25 common_path = os.path.commonprefix((base_dir, path))
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050026 archive_name = path[len(common_path):]
Haibo Huangb0bee822021-02-24 15:40:15 -080027 if os.path.isabs(archive_name):
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050028 archive_name = archive_name[1:]
Haibo Huangb0bee822021-02-24 15:40:15 -080029 return os.path.join(prefix_dir, archive_name)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050030 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)
Haibo Huangb0bee822021-02-24 15:40:15 -080035 tar.add(path, path_in_tar)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050036 compression = TARGZ_DEFAULT_COMPRESSION_LEVEL
Haibo Huangb0bee822021-02-24 15:40:15 -080037 with closing(tarfile.TarFile.open(tarball_path, 'w:gz',
38 compresslevel=compression)) as tar:
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050039 for source in sources:
40 source_path = source
Haibo Huangb0bee822021-02-24 15:40:15 -080041 if os.path.isdir(source):
42 for dirpath, dirnames, filenames in os.walk(source_path):
43 visit(tar, dirpath, filenames)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050044 else:
45 path_in_tar = archive_name(source_path)
Haibo Huangb0bee822021-02-24 15:40:15 -080046 tar.add(source_path, path_in_tar) # filename, arcname
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050047
Haibo Huangb0bee822021-02-24 15:40:15 -080048def decompress(tarball_path, base_dir):
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050049 """Decompress the gzipped tarball into directory base_dir.
50 """
Haibo Huangb0bee822021-02-24 15:40:15 -080051 with closing(tarfile.TarFile.open(tarball_path)) as tar:
52 tar.extractall(base_dir)