| 854 | shutil.rmtree(self.chroot) |
| 855 | |
| 856 | def zip( |
| 857 | self, |
| 858 | filename, # type: str |
| 859 | mode="w", # type: str |
| 860 | deterministic=False, # type: bool |
| 861 | exclude_file=lambda _: False, # type: Callable[[str], bool] |
| 862 | strip_prefix=None, # type: Optional[str] |
| 863 | labels=None, # type: Optional[Iterable[str]] |
| 864 | compress=True, # type: bool |
| 865 | ): |
| 866 | # type: (...) -> None |
| 867 | |
| 868 | if labels: |
| 869 | selected_files = set( |
| 870 | itertools.chain.from_iterable(self.filesets.get(label, ()) for label in labels) |
| 871 | ) |
| 872 | else: |
| 873 | selected_files = self.files() |
| 874 | |
| 875 | with open_zip( |
| 876 | filename, mode, zipfile.ZIP_DEFLATED if compress else zipfile.ZIP_STORED |
| 877 | ) as zf: |
| 878 | |
| 879 | def write_entry( |
| 880 | filename, # type: str |
| 881 | arcname, # type: str |
| 882 | ): |
| 883 | # type: (...) -> None |
| 884 | zf.write_deterministic( |
| 885 | filename=filename, |
| 886 | arcname=os.path.relpath(arcname, strip_prefix) if strip_prefix else arcname, |
| 887 | deterministic=deterministic, |
| 888 | compress=compress and self._compress_by_file.get(arcname, True), |
| 889 | ) |
| 890 | |
| 891 | def get_parent_dir(path): |
| 892 | # type: (str) -> Optional[str] |
| 893 | parent_dir = os.path.normpath(os.path.dirname(path)) |
| 894 | if parent_dir and parent_dir != os.curdir: |
| 895 | return parent_dir |
| 896 | return None |
| 897 | |
| 898 | written_dirs = set() |
| 899 | |
| 900 | def maybe_write_parent_dirs(path): |
| 901 | # type: (str) -> None |
| 902 | if path == strip_prefix: |
| 903 | return |
| 904 | parent_dir = get_parent_dir(path) |
| 905 | if parent_dir is None or parent_dir in written_dirs: |
| 906 | return |
| 907 | maybe_write_parent_dirs(parent_dir) |
| 908 | if parent_dir != strip_prefix: |
| 909 | write_entry(filename=os.path.join(self.chroot, parent_dir), arcname=parent_dir) |
| 910 | written_dirs.add(parent_dir) |
| 911 | |
| 912 | def iter_files(): |
| 913 | # type: () -> Iterator[Tuple[str, str]] |