1*44844408SAndroid Build Coastguard Worker# Copyright 2010 Baptiste Lepilleur and The JsonCpp Authors 2*44844408SAndroid Build Coastguard Worker# Distributed under MIT license, or public domain if desired and 3*44844408SAndroid Build Coastguard Worker# recognized in your jurisdiction. 4*44844408SAndroid Build Coastguard Worker# See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE 5*44844408SAndroid Build Coastguard Worker 6*44844408SAndroid Build Coastguard Workerfrom contextlib import closing 7*44844408SAndroid Build Coastguard Workerimport os 8*44844408SAndroid Build Coastguard Workerimport tarfile 9*44844408SAndroid Build Coastguard Worker 10*44844408SAndroid Build Coastguard WorkerTARGZ_DEFAULT_COMPRESSION_LEVEL = 9 11*44844408SAndroid Build Coastguard Worker 12*44844408SAndroid Build Coastguard Workerdef make_tarball(tarball_path, sources, base_dir, prefix_dir=''): 13*44844408SAndroid Build Coastguard Worker """Parameters: 14*44844408SAndroid Build Coastguard Worker tarball_path: output path of the .tar.gz file 15*44844408SAndroid Build Coastguard Worker sources: list of sources to include in the tarball, relative to the current directory 16*44844408SAndroid Build Coastguard Worker base_dir: if a source file is in a sub-directory of base_dir, then base_dir is stripped 17*44844408SAndroid Build Coastguard Worker from path in the tarball. 18*44844408SAndroid Build Coastguard Worker prefix_dir: all files stored in the tarball be sub-directory of prefix_dir. Set to '' 19*44844408SAndroid Build Coastguard Worker to make them child of root. 20*44844408SAndroid Build Coastguard Worker """ 21*44844408SAndroid Build Coastguard Worker base_dir = os.path.normpath(os.path.abspath(base_dir)) 22*44844408SAndroid Build Coastguard Worker def archive_name(path): 23*44844408SAndroid Build Coastguard Worker """Makes path relative to base_dir.""" 24*44844408SAndroid Build Coastguard Worker path = os.path.normpath(os.path.abspath(path)) 25*44844408SAndroid Build Coastguard Worker common_path = os.path.commonprefix((base_dir, path)) 26*44844408SAndroid Build Coastguard Worker archive_name = path[len(common_path):] 27*44844408SAndroid Build Coastguard Worker if os.path.isabs(archive_name): 28*44844408SAndroid Build Coastguard Worker archive_name = archive_name[1:] 29*44844408SAndroid Build Coastguard Worker return os.path.join(prefix_dir, archive_name) 30*44844408SAndroid Build Coastguard Worker def visit(tar, dirname, names): 31*44844408SAndroid Build Coastguard Worker for name in names: 32*44844408SAndroid Build Coastguard Worker path = os.path.join(dirname, name) 33*44844408SAndroid Build Coastguard Worker if os.path.isfile(path): 34*44844408SAndroid Build Coastguard Worker path_in_tar = archive_name(path) 35*44844408SAndroid Build Coastguard Worker tar.add(path, path_in_tar) 36*44844408SAndroid Build Coastguard Worker compression = TARGZ_DEFAULT_COMPRESSION_LEVEL 37*44844408SAndroid Build Coastguard Worker with closing(tarfile.TarFile.open(tarball_path, 'w:gz', 38*44844408SAndroid Build Coastguard Worker compresslevel=compression)) as tar: 39*44844408SAndroid Build Coastguard Worker for source in sources: 40*44844408SAndroid Build Coastguard Worker source_path = source 41*44844408SAndroid Build Coastguard Worker if os.path.isdir(source): 42*44844408SAndroid Build Coastguard Worker for dirpath, dirnames, filenames in os.walk(source_path): 43*44844408SAndroid Build Coastguard Worker visit(tar, dirpath, filenames) 44*44844408SAndroid Build Coastguard Worker else: 45*44844408SAndroid Build Coastguard Worker path_in_tar = archive_name(source_path) 46*44844408SAndroid Build Coastguard Worker tar.add(source_path, path_in_tar) # filename, arcname 47*44844408SAndroid Build Coastguard Worker 48*44844408SAndroid Build Coastguard Workerdef decompress(tarball_path, base_dir): 49*44844408SAndroid Build Coastguard Worker """Decompress the gzipped tarball into directory base_dir. 50*44844408SAndroid Build Coastguard Worker """ 51*44844408SAndroid Build Coastguard Worker with closing(tarfile.TarFile.open(tarball_path)) as tar: 52*44844408SAndroid Build Coastguard Worker tar.extractall(base_dir) 53