Add a member to the test archive. Call within `with`. Provides many shortcuts: - default `type` is based on symlink_to, hardlink_to, and trailing `/` in name (which is stripped) - size & content defaults are based on each other - content can be str or bytes
(self, name, *, type=None, symlink_to=None, hardlink_to=None,
mode=None, size=None, content=None, **kwargs)
| 3614 | self.bio = None |
| 3615 | |
| 3616 | def add(self, name, *, type=None, symlink_to=None, hardlink_to=None, |
| 3617 | mode=None, size=None, content=None, **kwargs): |
| 3618 | """Add a member to the test archive. Call within `with`. |
| 3619 | |
| 3620 | Provides many shortcuts: |
| 3621 | - default `type` is based on symlink_to, hardlink_to, and trailing `/` |
| 3622 | in name (which is stripped) |
| 3623 | - size & content defaults are based on each other |
| 3624 | - content can be str or bytes |
| 3625 | - mode should be textual ('-rwxrwxrwx') |
| 3626 | |
| 3627 | (add more! this is unstable internal test-only API) |
| 3628 | """ |
| 3629 | name = str(name) |
| 3630 | tarinfo = tarfile.TarInfo(name).replace(**kwargs) |
| 3631 | if content is not None: |
| 3632 | if isinstance(content, str): |
| 3633 | content = content.encode() |
| 3634 | size = len(content) |
| 3635 | if size is not None: |
| 3636 | tarinfo.size = size |
| 3637 | if content is None: |
| 3638 | content = bytes(tarinfo.size) |
| 3639 | if mode: |
| 3640 | tarinfo.mode = _filemode_to_int(mode) |
| 3641 | if symlink_to is not None: |
| 3642 | type = tarfile.SYMTYPE |
| 3643 | tarinfo.linkname = str(symlink_to) |
| 3644 | if hardlink_to is not None: |
| 3645 | type = tarfile.LNKTYPE |
| 3646 | tarinfo.linkname = str(hardlink_to) |
| 3647 | if name.endswith('/') and type is None: |
| 3648 | type = tarfile.DIRTYPE |
| 3649 | if type is not None: |
| 3650 | tarinfo.type = type |
| 3651 | if tarinfo.isreg(): |
| 3652 | fileobj = io.BytesIO(content) |
| 3653 | else: |
| 3654 | fileobj = None |
| 3655 | self.tar_w.addfile(tarinfo, fileobj) |
| 3656 | |
| 3657 | def open(self, **kwargs): |
| 3658 | """Open the resulting archive as TarFile. Call after `with`.""" |
no test coverage detected