(path, name, archive_format, files)
| 29 | |
| 30 | |
| 31 | def build_archive(path, name, archive_format, files): |
| 32 | filepath = path / f"{name}.{archive_format}" |
| 33 | |
| 34 | if archive_format == "tar.gz": |
| 35 | with tarfile.open(filepath, "x:gz") as archive: |
| 36 | for mname, content in files.items(): |
| 37 | if isinstance(content, tarfile.TarInfo): |
| 38 | content.name = mname |
| 39 | archive.addfile(content) |
| 40 | else: |
| 41 | data = textwrap.dedent(content).encode("utf8") |
| 42 | member = tarfile.TarInfo(mname) |
| 43 | member.size = len(data) |
| 44 | archive.addfile(member, io.BytesIO(data)) |
| 45 | return filepath |
| 46 | |
| 47 | if archive_format == "zip": |
| 48 | with zipfile.ZipFile(filepath, mode="w") as archive: |
| 49 | for mname, content in files.items(): |
| 50 | archive.writestr(mname, textwrap.dedent(content)) |
| 51 | return filepath |
| 52 | |
| 53 | raise ValueError(format) |
no outgoing calls