comparison env/lib/python3.7/site-packages/virtualenv/util/zipapp.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400
parents 26e78fe6e8c4
children
comparison
equal deleted inserted replaced
1:75ca89e9b81c 2:6af9afd405e9
1 from __future__ import absolute_import, unicode_literals
2
3 import logging
4 import os
5 import zipfile
6 from contextlib import contextmanager
7 from tempfile import TemporaryFile
8
9 from virtualenv.info import IS_WIN, IS_ZIPAPP, ROOT
10 from virtualenv.util.path import Path
11 from virtualenv.util.six import ensure_text
12 from virtualenv.version import __version__
13
14
15 def read(full_path):
16 sub_file = _get_path_within_zip(full_path)
17 with zipfile.ZipFile(ROOT, "r") as zip_file:
18 with zip_file.open(sub_file) as file_handler:
19 return file_handler.read().decode("utf-8")
20
21
22 def extract(full_path, dest):
23 logging.debug("extract %s to %s", full_path, dest)
24 sub_file = _get_path_within_zip(full_path)
25 with zipfile.ZipFile(ROOT, "r") as zip_file:
26 info = zip_file.getinfo(sub_file)
27 info.filename = dest.name
28 zip_file.extract(info, ensure_text(str(dest.parent)))
29
30
31 def _get_path_within_zip(full_path):
32 full_path = os.path.abspath(str(full_path))
33 sub_file = full_path[len(ROOT) + 1 :]
34 if IS_WIN:
35 # paths are always UNIX separators, even on Windows, though __file__ still follows platform default
36 sub_file = sub_file.replace(os.sep, "/")
37 return sub_file
38
39
40 @contextmanager
41 def ensure_file_on_disk(path, app_data):
42 if IS_ZIPAPP:
43 if app_data is None:
44 with TemporaryFile() as temp_file:
45 dest = Path(temp_file.name)
46 extract(path, dest)
47 yield Path(dest)
48 else:
49 base = app_data / "zipapp" / "extract" / __version__
50 with base.lock_for_key(path.name):
51 dest = base.path / path.name
52 if not dest.exists():
53 extract(path, dest)
54 yield dest
55 else:
56 yield path