def make_archive(self, base_name, format, root_dir=None, base_dir=None, owner=None, group=None): """Don't inlcude the version number for MSDeploy packages.""" dry_run = self.dry_run dist_name = self.distribution.get_name() # Copied from distutils.command.sdist.sdist.make_archive zip_filename = base_name + ".zip" base_len = len(base_dir) archive_util.mkpath(os.path.dirname(zip_filename), dry_run=dry_run) log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir) if not dry_run: zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_DEFLATED) for filename in self.msdeploy_files: log.info("adding '%s'" % filename) zip.write(filename, filename) for dirpath, dirnames, filenames in os.walk(base_dir): for name in filenames: src_path = os.path.normpath(os.path.join(dirpath, name)) dst_path = dist_name + src_path[base_len:] if os.path.isfile(src_path): zip.write(src_path, dst_path) log.info("adding '%s'" % dst_path) zip.close() return zip_filename
def make_zipfile(base_name, base_dir, verbose=0, dry_run=0): """Create a zip file from all the files under 'base_dir'. The output zip file will be named 'base_name' + ".zip". Uses either the "zipfile" Python module (if available) or the InfoZIP "zip" utility (if installed and found on the default search path). If neither tool is available, raises DistutilsExecError. Returns the name of the output zip file. """ import zipfile zip_filename = base_name + ".zip" archive_util.mkpath(os.path.dirname(zip_filename), dry_run=dry_run) archive_util.log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir) if not dry_run: zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_DEFLATED) for dirpath, dirnames, filenames in os.walk(base_dir): for name in filenames: src_path = os.path.normpath(os.path.join(dirpath, name)) dirpath_split = os.path.split(dirpath) dst_dirpath = dirpath_split[1:] if dirpath_split[0] == '': dst_dirpath = dirpath_split[2:] dst_path = os.path.normpath( os.path.join(*(dst_dirpath + (name, )))) if os.path.isfile(src_path): zip.write(src_path, dst_path) archive_util.log.info("adding '%s'" % dst_path) zip.close() return zip_filename
def make_zipfile(base_name, base_dir, verbose=0, dry_run=0): """Create a zip file from all the files under 'base_dir'. The output zip file will be named 'base_name' + ".zip". Uses either the "zipfile" Python module (if available) or the InfoZIP "zip" utility (if installed and found on the default search path). If neither tool is available, raises DistutilsExecError. Returns the name of the output zip file. """ import zipfile zip_filename = base_name + ".zip" archive_util.mkpath(os.path.dirname(zip_filename), dry_run=dry_run) archive_util.log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir) if not dry_run: zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_DEFLATED) for dirpath, dirnames, filenames in os.walk(base_dir): for name in filenames: src_path = os.path.normpath(os.path.join(dirpath, name)) dirpath_split = os.path.split(dirpath) dst_dirpath = dirpath_split[1:] if dirpath_split[0] == '': dst_dirpath = dirpath_split[2:] dst_path = os.path.normpath(os.path.join(*( dst_dirpath + (name,)))) if os.path.isfile(src_path): zip.write(src_path, dst_path) archive_util.log.info("adding '%s'" % dst_path) zip.close() return zip_filename
def patched_make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, owner=None, group=None): """Modified version of distutils.archive_util.make_tarball which supports SOURCE_DATE_EPOCH. Create a (possibly compressed) tar file from all the files under 'base_dir'. 'compress' must be "gzip" (the default), "bzip2", "xz", "compress", or None. ("compress" will be deprecated in Python 3.2) '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 and group will be used. If the environment variable SOURCE_DATE_EPOCH is set, each file's timestamp will be adjusted to be no later than that value. The output tar file will be named 'base_dir' + ".tar", possibly plus the appropriate compression extension (".gz", ".bz2", ".xz" or ".Z"). Returns the output filename. """ # late imports to avoid circular dependencies import os import sys import tarfile from distutils.archive_util import ( _get_gid, _get_uid, log, mkpath, spawn, warn, ) tar_compression = { 'gzip': 'gz', 'bzip2': 'bz2', 'xz': 'xz', None: '', 'compress': '' } compress_ext = { 'gzip': '.gz', 'bzip2': '.bz2', 'xz': '.xz', 'compress': '.Z' } # flags for compression program, each element of list will be an argument if compress is not None and compress not in compress_ext.keys(): raise ValueError( "bad value for 'compress': must be None, 'gzip', 'bzip2', " "'xz' or 'compress'") archive_name = base_name + '.tar' if compress != 'compress': archive_name += compress_ext.get(compress, '') mkpath(os.path.dirname(archive_name), dry_run=dry_run) log.info('Creating tar archive') uid = _get_uid(owner) gid = _get_gid(group) epoch = os.environ.get("SOURCE_DATE_EPOCH") if epoch is not None: epoch = int(epoch) def _adjust_tarinfo(tarinfo): if gid is not None: tarinfo.gid = gid tarinfo.gname = group if uid is not None: tarinfo.uid = uid tarinfo.uname = owner if epoch is not None: if tarinfo.mtime > epoch: tarinfo.mtime = epoch return tarinfo if not dry_run: tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress]) try: tar.add(base_dir, filter=_adjust_tarinfo) finally: tar.close() # compression using `compress` if compress == 'compress': warn("'compress' will be deprecated.", PendingDeprecationWarning) # the option varies depending on the platform compressed_name = archive_name + compress_ext[compress] if sys.platform == 'win32': cmd = [compress, archive_name, compressed_name] else: cmd = [compress, '-f', archive_name] spawn(cmd, dry_run=dry_run) return compressed_name return archive_name