move casync release creation to use a tarball of files (#32089)
* tar archive instead * fix * move this here * migrate these * fix this * update readme * fix that * try to build nightly * Revert "try to build nightly" This reverts commitpull/32199/head4ea680cb6a
. * caexclude is no longer required * finish up * sorted * need this * and that * context mnager * path based old-commit-hash:69982d43cd
parent
6bcc026ca5
commit
b33aef01b8
8 changed files with 221 additions and 31 deletions
@ -0,0 +1,38 @@ |
||||
import pathlib |
||||
import tarfile |
||||
from typing import IO, Callable |
||||
|
||||
|
||||
def include_default(_) -> bool: |
||||
return True |
||||
|
||||
|
||||
def create_tar_archive(filename: pathlib.Path, directory: pathlib.Path, include: Callable[[pathlib.Path], bool] = include_default): |
||||
"""Creates a tar archive of a directory""" |
||||
|
||||
with tarfile.open(filename, 'w') as tar: |
||||
for file in sorted(directory.rglob("*"), key=lambda f: f.stat().st_size if f.is_file() else 0, reverse=True): |
||||
if not include(file): |
||||
continue |
||||
relative_path = str(file.relative_to(directory)) |
||||
if file.is_symlink(): |
||||
info = tarfile.TarInfo(relative_path) |
||||
info.type = tarfile.SYMTYPE |
||||
info.linkpath = str(file.readlink()) |
||||
tar.addfile(info) |
||||
|
||||
elif file.is_file(): |
||||
info = tarfile.TarInfo(relative_path) |
||||
info.size = file.stat().st_size |
||||
info.type = tarfile.REGTYPE |
||||
with file.open('rb') as f: |
||||
tar.addfile(info, f) |
||||
|
||||
|
||||
|
||||
def extract_tar_archive(fh: IO[bytes], directory: pathlib.Path): |
||||
"""Extracts a tar archive to a directory""" |
||||
|
||||
tar = tarfile.open(fileobj=fh, mode='r') |
||||
tar.extractall(str(directory), filter=lambda info, path: info) |
||||
tar.close() |
Loading…
Reference in new issue