示例#1
0
文件: tar.py 项目: ipfire/pakfire
	def add(self, name, arcname=None, recursive=None, exclude=None, filter=None):
		"""
			Emulate the add function with capability support.
		"""
		tarinfo = self.gettarinfo(name, arcname)

		if tarinfo.isreg():
			attrs = []

			# Save capabilities.
			caps = util.get_capabilities(name)
			if caps:
				log.debug("Saving capabilities for %s: %s" % (name, caps))
				tarinfo.pax_headers["PAKFIRE.capabilities"] = caps

			# Append the tar header and data to the archive.
			f = tarfile.bltn_open(name, "rb")
			self.addfile(tarinfo, f)
			f.close()

		elif tarinfo.isdir():
			self.addfile(tarinfo)
			if recursive:
				for f in os.listdir(name):
					self.add(os.path.join(name, f), os.path.join(arcname, f),
							recursive, exclude, filter)

		else:
			self.addfile(tarinfo)

		# Return the tar information about the file
		return tarinfo
示例#2
0
    def add(self,
            name,
            arcname=None,
            recursive=None,
            exclude=None,
            filter=None):
        """
			Emulate the add function with capability support.
		"""
        tarinfo = self.gettarinfo(name, arcname)

        if tarinfo.isreg():
            attrs = []

            # Save capabilities.
            caps = util.get_capabilities(name)
            if caps:
                log.debug("Saving capabilities for %s: %s" % (name, caps))
                tarinfo.pax_headers["PAKFIRE.capabilities"] = caps

            # Append the tar header and data to the archive.
            f = tarfile.bltn_open(name, "rb")
            self.addfile(tarinfo, f)
            f.close()

        elif tarinfo.isdir():
            self.addfile(tarinfo)
            if recursive:
                for f in os.listdir(name):
                    self.add(os.path.join(name, f), os.path.join(arcname, f),
                             recursive, exclude, filter)

        else:
            self.addfile(tarinfo)

        # Return the tar information about the file
        return tarinfo
示例#3
0
文件: archive.py 项目: ebeuerle/sos
    def add(self, name, arcname=None, recursive=True,
            exclude=None, filter=None):
        """Add the file `name' to the archive. `name' may be any type of file
           (directory, fifo, symbolic link, etc.). If given, `arcname'
           specifies an alternative name for the file in the archive.
           Directories are added recursively by default. This can be avoided by
           setting `recursive' to False. `exclude' is a function that should
           return True for each filename to be excluded. `filter' is a function
           that expects a TarInfo object argument and returns the changed
           TarInfo object, if it returns None the TarInfo object will be
           excluded from the archive.
        """
        self._check("aw")

        if arcname is None:
            arcname = name

        # Exclude pathnames.
        if exclude is not None:
            import warnings
            warnings.warn("use the filter argument instead",
                          DeprecationWarning, 2)
            if exclude(name):
                self._dbg(2, "tarfile: Excluded %r" % name)
                return

        # Skip if somebody tries to archive the archive...
        if self.name is not None and os.path.abspath(name) == self.name:
            self._dbg(2, "tarfile: Skipped %r" % name)
            return

        self._dbg(1, name)

        # Create a TarInfo object from the file.
        tarinfo = self.gettarinfo(name, arcname)

        if tarinfo is None:
            self._dbg(1, "tarfile: Unsupported type %r" % name)
            return

        # Change or exclude the TarInfo object.
        if filter is not None:
            tarinfo = filter(tarinfo)
            if tarinfo is None:
                self._dbg(2, "tarfile: Excluded %r" % name)
                return

        # Append the tar header and data to the archive.
        if tarinfo.isreg():
            with tarfile.bltn_open(name, "rb") as f:
                self.addfile(tarinfo, f)

        elif tarinfo.isdir():
            self.addfile(tarinfo)
            if recursive:
                for f in os.listdir(name):
                    self.add(os.path.join(name, f), os.path.join(arcname, f),
                             recursive, exclude, filter)

        else:
            self.addfile(tarinfo)
示例#4
0
    def add(self,
            name,
            arcname=None,
            recursive=True,
            exclude=None,
            filter=None):
        """Add the file `name' to the archive. `name' may be any type of file
           (directory, fifo, symbolic link, etc.). If given, `arcname'
           specifies an alternative name for the file in the archive.
           Directories are added recursively by default. This can be avoided by
           setting `recursive' to False. `exclude' is a function that should
           return True for each filename to be excluded. `filter' is a function
           that expects a TarInfo object argument and returns the changed
           TarInfo object, if it returns None the TarInfo object will be
           excluded from the archive.
        """
        self._check("aw")

        if arcname is None:
            arcname = name

        # Exclude pathnames.
        if exclude is not None:
            import warnings
            warnings.warn("use the filter argument instead",
                          DeprecationWarning, 2)
            if exclude(name):
                self._dbg(2, "tarfile: Excluded %r" % name)
                return

        # Skip if somebody tries to archive the archive...
        if self.name is not None and os.path.abspath(name) == self.name:
            self._dbg(2, "tarfile: Skipped %r" % name)
            return

        self._dbg(1, name)

        # Create a TarInfo object from the file.
        tarinfo = self.gettarinfo(name, arcname)

        if tarinfo is None:
            self._dbg(1, "tarfile: Unsupported type %r" % name)
            return

        # Change or exclude the TarInfo object.
        if filter is not None:
            tarinfo = filter(tarinfo)
            if tarinfo is None:
                self._dbg(2, "tarfile: Excluded %r" % name)
                return

        # Append the tar header and data to the archive.
        if tarinfo.isreg():
            with tarfile.bltn_open(name, "rb") as f:
                self.addfile(tarinfo, f)

        elif tarinfo.isdir():
            self.addfile(tarinfo)
            if recursive:
                for f in os.listdir(name):
                    self.add(os.path.join(name, f), os.path.join(arcname, f),
                             recursive, exclude, filter)

        else:
            self.addfile(tarinfo)
示例#5
0
文件: nodes.py 项目: xxoolm/Ryven
 def update_event(self, inp=-1):
     self.set_output_val(0, tarfile.bltn_open(self.input(0), self.input(1), self.input(2), self.input(3), self.input(4), self.input(5), self.input(6), self.input(7)))
示例#6
0
    def __init__(
        self,
        name=None,
        mode="r",
        fileobj=None,
        format=None,
        tarinfo=None,
        dereference=None,
        ignore_zeros=None,
        encoding=None,
        errors=None,
        pax_headers=None,
        debug=None,
        errorlevel=None,
        end_offset=0,
    ):
        # open in 'r' mode, don't read through
        super(BigTarFile2, self).__init__(
            name=name,
            mode="r" if os.path.exists(name) else "w",
            fileobj=fileobj,
            format=format,
            tarinfo=tarinfo,
            dereference=dereference,
            ignore_zeros=ignore_zeros,
            encoding=encoding,
            errors=errors,
            pax_headers=pax_headers,
            debug=debug,
            errorlevel=errorlevel,
        )
        self._loaded_real = False
        if "a" == mode:
            self.mode = "a"
            self._mode = "r+b"
            if not fileobj:
                if self.mode == "a" and not os.path.exists(name):
                    # Create nonexistent files in append mode.
                    self.mode = "w"
                    self._mode = "wb"
                fileobj = tarfile.bltn_open(name, self._mode)
                self._extfileobj = False
            else:
                if name is None and hasattr(fileobj, "name"):
                    name = fileobj.name
                if hasattr(fileobj, "mode"):
                    self._mode = fileobj.mode
                self._extfileobj = True
            self.name = os.path.abspath(name) if name else None
            self.fileobj = fileobj

            # Move to the end of the archive,
            # before the first empty block.
            self.firstmember = None
            if end_offset > self.offset:
                self.fileobj.seek(0, 2)
                p = self.fileobj.tell()
                self.offset = min(p - tarfile.BLOCKSIZE, end_offset)
                self.fileobj.seek(self.offset)

            while True:
                if self.next() is None:
                    if self.offset > 0:
                        self.fileobj.seek(-tarfile.BLOCKSIZE, 1)
                    break
        self.append_offset = self.offset