Create a (possibly compressed) tar file from all the files under 'base_dir'. 'compress' must be "gzip" (the default), "bzip2", "xz", or None. 'owner' and 'group' can be used to define an owner and a group for the archive that is being built. If not provided, the current owner
(base_name, base_dir, compress="gzip", verbose=0, dry_run=0,
owner=None, group=None, logger=None, root_dir=None)
| 925 | return None |
| 926 | |
| 927 | def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, |
| 928 | owner=None, group=None, logger=None, root_dir=None): |
| 929 | """Create a (possibly compressed) tar file from all the files under |
| 930 | 'base_dir'. |
| 931 | |
| 932 | 'compress' must be "gzip" (the default), "bzip2", "xz", or None. |
| 933 | |
| 934 | 'owner' and 'group' can be used to define an owner and a group for the |
| 935 | archive that is being built. If not provided, the current owner and group |
| 936 | will be used. |
| 937 | |
| 938 | The output tar file will be named 'base_name' + ".tar", possibly plus |
| 939 | the appropriate compression extension (".gz", ".bz2", or ".xz"). |
| 940 | |
| 941 | Returns the output filename. |
| 942 | """ |
| 943 | if compress is None: |
| 944 | tar_compression = '' |
| 945 | elif _ZLIB_SUPPORTED and compress == 'gzip': |
| 946 | tar_compression = 'gz' |
| 947 | elif _BZ2_SUPPORTED and compress == 'bzip2': |
| 948 | tar_compression = 'bz2' |
| 949 | elif _LZMA_SUPPORTED and compress == 'xz': |
| 950 | tar_compression = 'xz' |
| 951 | else: |
| 952 | raise ValueError("bad value for 'compress', or compression format not " |
| 953 | "supported : {0}".format(compress)) |
| 954 | |
| 955 | import tarfile # late import for breaking circular dependency |
| 956 | |
| 957 | compress_ext = '.' + tar_compression if compress else '' |
| 958 | archive_name = base_name + '.tar' + compress_ext |
| 959 | archive_dir = os.path.dirname(archive_name) |
| 960 | |
| 961 | if archive_dir and not os.path.exists(archive_dir): |
| 962 | if logger is not None: |
| 963 | logger.info("creating %s", archive_dir) |
| 964 | if not dry_run: |
| 965 | os.makedirs(archive_dir) |
| 966 | |
| 967 | # creating the tarball |
| 968 | if logger is not None: |
| 969 | logger.info('Creating tar archive') |
| 970 | |
| 971 | uid = _get_uid(owner) |
| 972 | gid = _get_gid(group) |
| 973 | |
| 974 | def _set_uid_gid(tarinfo): |
| 975 | if gid is not None: |
| 976 | tarinfo.gid = gid |
| 977 | tarinfo.gname = group |
| 978 | if uid is not None: |
| 979 | tarinfo.uid = uid |
| 980 | tarinfo.uname = owner |
| 981 | return tarinfo |
| 982 | |
| 983 | if not dry_run: |
| 984 | tar = tarfile.open(archive_name, 'w|%s' % tar_compression) |