blob: b2030fbdba52f4720365618ad748c3f696d83554 [file] [log] [blame]
Christopher Dunn9cc0bb82015-01-24 15:44:51 -06001from contextlib import closing
Christopher Dunnf3576882015-01-24 16:20:25 -06002import os
Christopher Dunndc0f7362011-06-21 21:18:49 +00003import tarfile
4
5TARGZ_DEFAULT_COMPRESSION_LEVEL = 9
6
7def make_tarball(tarball_path, sources, base_dir, prefix_dir=''):
8 """Parameters:
9 tarball_path: output path of the .tar.gz file
10 sources: list of sources to include in the tarball, relative to the current directory
11 base_dir: if a source file is in a sub-directory of base_dir, then base_dir is stripped
12 from path in the tarball.
13 prefix_dir: all files stored in the tarball be sub-directory of prefix_dir. Set to ''
14 to make them child of root.
15 """
Christopher Dunn494950a2015-01-24 15:29:52 -060016 base_dir = os.path.normpath(os.path.abspath(base_dir))
17 def archive_name(path):
Christopher Dunndc0f7362011-06-21 21:18:49 +000018 """Makes path relative to base_dir."""
Christopher Dunn494950a2015-01-24 15:29:52 -060019 path = os.path.normpath(os.path.abspath(path))
20 common_path = os.path.commonprefix((base_dir, path))
Christopher Dunndc0f7362011-06-21 21:18:49 +000021 archive_name = path[len(common_path):]
Christopher Dunn494950a2015-01-24 15:29:52 -060022 if os.path.isabs(archive_name):
Christopher Dunndc0f7362011-06-21 21:18:49 +000023 archive_name = archive_name[1:]
Christopher Dunn494950a2015-01-24 15:29:52 -060024 return os.path.join(prefix_dir, archive_name)
Christopher Dunndc0f7362011-06-21 21:18:49 +000025 def visit(tar, dirname, names):
26 for name in names:
27 path = os.path.join(dirname, name)
28 if os.path.isfile(path):
29 path_in_tar = archive_name(path)
Christopher Dunn494950a2015-01-24 15:29:52 -060030 tar.add(path, path_in_tar)
Christopher Dunndc0f7362011-06-21 21:18:49 +000031 compression = TARGZ_DEFAULT_COMPRESSION_LEVEL
Christopher Dunn9cc0bb82015-01-24 15:44:51 -060032 with closing(tarfile.TarFile.open(tarball_path, 'w:gz',
33 compresslevel=compression)) as tar:
Christopher Dunndc0f7362011-06-21 21:18:49 +000034 for source in sources:
35 source_path = source
Christopher Dunn494950a2015-01-24 15:29:52 -060036 if os.path.isdir(source):
Christopher Dunnf3576882015-01-24 16:20:25 -060037 for dirpath, dirnames, filenames in os.walk(source_path):
38 visit(tar, dirpath, filenames)
Christopher Dunndc0f7362011-06-21 21:18:49 +000039 else:
40 path_in_tar = archive_name(source_path)
Christopher Dunn494950a2015-01-24 15:29:52 -060041 tar.add(source_path, path_in_tar) # filename, arcname
Christopher Dunndc0f7362011-06-21 21:18:49 +000042
Christopher Dunn494950a2015-01-24 15:29:52 -060043def decompress(tarball_path, base_dir):
Christopher Dunndc0f7362011-06-21 21:18:49 +000044 """Decompress the gzipped tarball into directory base_dir.
45 """
Christopher Dunn9cc0bb82015-01-24 15:44:51 -060046 with closing(tarfile.TarFile.open(tarball_path)) as tar:
Christopher Dunn494950a2015-01-24 15:29:52 -060047 tar.extractall(base_dir)