codekingpro/portable-devtools
114k
1from __future__ import annotations2 3import logging4import os5import zipfile6 7from virtualenv.info import IS_WIN, ROOT8 9 10def read(full_path):11 sub_file = _get_path_within_zip(full_path)12 with zipfile.ZipFile(ROOT, "r") as zip_file, zip_file.open(sub_file) as file_handler:13 return file_handler.read().decode("utf-8")14 15 16def extract(full_path, dest):17 logging.debug("extract %s to %s", full_path, dest)18 sub_file = _get_path_within_zip(full_path)19 with zipfile.ZipFile(ROOT, "r") as zip_file:20 info = zip_file.getinfo(sub_file)21 info.filename = dest.name22 zip_file.extract(info, str(dest.parent))23 24 25def _get_path_within_zip(full_path):26 full_path = os.path.abspath(str(full_path))27 sub_file = full_path[len(ROOT) + 1 :]28 if IS_WIN:29 # paths are always UNIX separators, even on Windows, though __file__ still follows platform default30 sub_file = sub_file.replace(os.sep, "/")31 return sub_file32 33 34__all__ = [35 "extract",36 "read",37]38 