mirror of
https://gitlab.com/kupfer/kupferbootstrap.git
synced 2025-02-23 05:35:44 -05:00
utils: add read_files_from_tar()
This commit is contained in:
parent
81f88dd636
commit
2c70ad6c12
1 changed files with 13 additions and 1 deletions
14
utils.py
14
utils.py
|
@ -1,10 +1,13 @@
|
|||
import atexit
|
||||
import grp
|
||||
import logging
|
||||
import os
|
||||
import pwd
|
||||
import subprocess
|
||||
import tarfile
|
||||
|
||||
from shutil import which
|
||||
from typing import Optional, Union, Sequence
|
||||
from typing import Generator, IO, Optional, Union, Sequence
|
||||
|
||||
from exec.cmd import run_cmd, run_root_cmd
|
||||
|
||||
|
@ -104,3 +107,12 @@ def get_gid(group: Union[int, str]) -> int:
|
|||
if isinstance(group, int) or group.isnumeric():
|
||||
return int(group)
|
||||
return grp.getgrnam(group).gr_gid
|
||||
|
||||
|
||||
def read_files_from_tar(tar_file: str, files: Sequence[str]) -> Generator[tuple[str, IO], None, None]:
|
||||
assert os.path.exists(tar_file)
|
||||
with tarfile.open(tar_file) as index:
|
||||
for path in files:
|
||||
fd = index.extractfile(index.getmember(path))
|
||||
assert fd
|
||||
yield path, fd
|
||||
|
|
Loading…
Add table
Reference in a new issue