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)
| 3597 | self.bio = None |
| 3598 | |
| 3599 | def add(self, name, *, type=None, symlink_to=None, hardlink_to=None, |
| 3600 | mode=None, size=None, content=None, **kwargs): |
| 3601 | """Add a member to the test archive. Call within `with`. |
| 3602 | |
| 3603 | Provides many shortcuts: |
| 3604 | - default `type` is based on symlink_to, hardlink_to, and trailing `/` |
| 3605 | in name (which is stripped) |
| 3606 | - size & content defaults are based on each other |
| 3607 | - content can be str or bytes |
| 3608 | - mode should be textual ('-rwxrwxrwx') |
| 3609 | |
| 3610 | (add more! this is unstable internal test-only API) |
| 3611 | """ |
| 3612 | name = str(name) |
| 3613 | tarinfo = tarfile.TarInfo(name).replace(**kwargs) |
| 3614 | if content is not None: |
| 3615 | if isinstance(content, str): |
| 3616 | content = content.encode() |
| 3617 | size = len(content) |
| 3618 | if size is not None: |
| 3619 | tarinfo.size = size |
| 3620 | if content is None: |
| 3621 | content = bytes(tarinfo.size) |
| 3622 | if mode: |
| 3623 | tarinfo.mode = _filemode_to_int(mode) |
| 3624 | if symlink_to is not None: |
| 3625 | type = tarfile.SYMTYPE |
| 3626 | tarinfo.linkname = str(symlink_to) |
| 3627 | if hardlink_to is not None: |
| 3628 | type = tarfile.LNKTYPE |
| 3629 | tarinfo.linkname = str(hardlink_to) |
| 3630 | if name.endswith('/') and type is None: |
| 3631 | type = tarfile.DIRTYPE |
| 3632 | if type is not None: |
| 3633 | tarinfo.type = type |
| 3634 | if tarinfo.isreg(): |
| 3635 | fileobj = io.BytesIO(content) |
| 3636 | else: |
| 3637 | fileobj = None |
| 3638 | self.tar_w.addfile(tarinfo, fileobj) |
| 3639 | |
| 3640 | def open(self, **kwargs): |
| 3641 | """Open the resulting archive as TarFile. Call after `with`.""" |
no test coverage detected