This commit is contained in:
Waylon Walker 2022-03-31 20:20:07 -05:00
commit 38355d2442
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
9083 changed files with 1225834 additions and 0 deletions

View file

@ -0,0 +1,58 @@
"""
Application data stored by virtualenv.
"""
from __future__ import absolute_import, unicode_literals
import logging
import os
from platformdirs import user_data_dir
from .na import AppDataDisabled
from .read_only import ReadOnlyAppData
from .via_disk_folder import AppDataDiskFolder
from .via_tempdir import TempAppData
def _default_app_data_dir(env):
key = str("VIRTUALENV_OVERRIDE_APP_DATA")
if key in env:
return env[key]
else:
return user_data_dir(appname="virtualenv", appauthor="pypa")
def make_app_data(folder, **kwargs):
read_only = kwargs.pop("read_only")
env = kwargs.pop("env")
if kwargs: # py3+ kwonly
raise TypeError("unexpected keywords: {}")
if folder is None:
folder = _default_app_data_dir(env)
folder = os.path.abspath(folder)
if read_only:
return ReadOnlyAppData(folder)
if not os.path.isdir(folder):
try:
os.makedirs(folder)
logging.debug("created app data folder %s", folder)
except OSError as exception:
logging.info("could not create app data folder %s due to %r", folder, exception)
if os.access(folder, os.W_OK):
return AppDataDiskFolder(folder)
else:
logging.debug("app data folder %s has no write access", folder)
return TempAppData()
__all__ = (
"AppDataDisabled",
"AppDataDiskFolder",
"ReadOnlyAppData",
"TempAppData",
"make_app_data",
)

View file

@ -0,0 +1,95 @@
"""
Application data stored by virtualenv.
"""
from __future__ import absolute_import, unicode_literals
from abc import ABCMeta, abstractmethod
from contextlib import contextmanager
import six
from virtualenv.info import IS_ZIPAPP
@six.add_metaclass(ABCMeta)
class AppData(object):
"""Abstract storage interface for the virtualenv application"""
@abstractmethod
def close(self):
"""called before virtualenv exits"""
@abstractmethod
def reset(self):
"""called when the user passes in the reset app data"""
@abstractmethod
def py_info(self, path):
raise NotImplementedError
@abstractmethod
def py_info_clear(self):
raise NotImplementedError
@property
def can_update(self):
raise NotImplementedError
@abstractmethod
def embed_update_log(self, distribution, for_py_version):
raise NotImplementedError
@property
def house(self):
raise NotImplementedError
@property
def transient(self):
raise NotImplementedError
@abstractmethod
def wheel_image(self, for_py_version, name):
raise NotImplementedError
@contextmanager
def ensure_extracted(self, path, to_folder=None):
"""Some paths might be within the zipapp, unzip these to a path on the disk"""
if IS_ZIPAPP:
with self.extract(path, to_folder) as result:
yield result
else:
yield path
@abstractmethod
@contextmanager
def extract(self, path, to_folder):
raise NotImplementedError
@abstractmethod
@contextmanager
def locked(self, path):
raise NotImplementedError
@six.add_metaclass(ABCMeta)
class ContentStore(object):
@abstractmethod
def exists(self):
raise NotImplementedError
@abstractmethod
def read(self):
raise NotImplementedError
@abstractmethod
def write(self, content):
raise NotImplementedError
@abstractmethod
def remove(self):
raise NotImplementedError
@abstractmethod
@contextmanager
def locked(self):
pass

View file

@ -0,0 +1,66 @@
from __future__ import absolute_import, unicode_literals
from contextlib import contextmanager
from .base import AppData, ContentStore
class AppDataDisabled(AppData):
"""No application cache available (most likely as we don't have write permissions)"""
transient = True
can_update = False
def __init__(self):
pass
error = RuntimeError("no app data folder available, probably no write access to the folder")
def close(self):
"""do nothing"""
def reset(self):
"""do nothing"""
def py_info(self, path):
return ContentStoreNA()
def embed_update_log(self, distribution, for_py_version):
return ContentStoreNA()
def extract(self, path, to_folder):
raise self.error
@contextmanager
def locked(self, path):
"""do nothing"""
yield
@property
def house(self):
raise self.error
def wheel_image(self, for_py_version, name):
raise self.error
def py_info_clear(self):
""" """
class ContentStoreNA(ContentStore):
def exists(self):
return False
def read(self):
""" """
return None
def write(self, content):
""" """
def remove(self):
""" """
@contextmanager
def locked(self):
yield

View file

@ -0,0 +1,34 @@
import os.path
from virtualenv.util.lock import NoOpFileLock
from .via_disk_folder import AppDataDiskFolder, PyInfoStoreDisk
class ReadOnlyAppData(AppDataDiskFolder):
can_update = False
def __init__(self, folder): # type: (str) -> None
if not os.path.isdir(folder):
raise RuntimeError("read-only app data directory {} does not exist".format(folder))
self.lock = NoOpFileLock(folder)
def reset(self): # type: () -> None
raise RuntimeError("read-only app data does not support reset")
def py_info_clear(self): # type: () -> None
raise NotImplementedError
def py_info(self, path):
return _PyInfoStoreDiskReadOnly(self.py_info_at, path)
def embed_update_log(self, distribution, for_py_version):
raise NotImplementedError
class _PyInfoStoreDiskReadOnly(PyInfoStoreDisk):
def write(self, content):
raise RuntimeError("read-only app data python info cannot be updated")
__all__ = ("ReadOnlyAppData",)

View file

@ -0,0 +1,177 @@
# -*- coding: utf-8 -*-
"""
A rough layout of the current storage goes as:
virtualenv-app-data
py - <version> <cache information about python interpreters>
   *.json/lock
wheel <cache wheels used for seeding>
  house
*.whl <wheels downloaded go here>
<python major.minor> -> 3.9
img-<version>
image
<install class> -> CopyPipInstall / SymlinkPipInstall
<wheel name> -> pip-20.1.1-py2.py3-none-any
embed
3 -> json format versioning
*.json -> for every distribution contains data about newer embed versions and releases
unzip <in zip app we cannot refer to some internal files, so first extract them>
<virtualenv version>
py_info.py
debug.py
_virtualenv.py
"""
from __future__ import absolute_import, unicode_literals
import json
import logging
from abc import ABCMeta
from contextlib import contextmanager
from hashlib import sha256
import six
from virtualenv.util.lock import ReentrantFileLock
from virtualenv.util.path import safe_delete
from virtualenv.util.six import ensure_text
from virtualenv.util.zipapp import extract
from virtualenv.version import __version__
from .base import AppData, ContentStore
class AppDataDiskFolder(AppData):
"""
Store the application data on the disk within a folder layout.
"""
transient = False
can_update = True
def __init__(self, folder):
self.lock = ReentrantFileLock(folder)
def __repr__(self):
return "{}({})".format(type(self).__name__, self.lock.path)
def __str__(self):
return str(self.lock.path)
def reset(self):
logging.debug("reset app data folder %s", self.lock.path)
safe_delete(self.lock.path)
def close(self):
"""do nothing"""
@contextmanager
def locked(self, path):
path_lock = self.lock / path
with path_lock:
yield path_lock.path
@contextmanager
def extract(self, path, to_folder):
if to_folder is not None:
root = ReentrantFileLock(to_folder())
else:
root = self.lock / "unzip" / __version__
with root.lock_for_key(path.name):
dest = root.path / path.name
if not dest.exists():
extract(path, dest)
yield dest
@property
def py_info_at(self):
return self.lock / "py_info" / "1"
def py_info(self, path):
return PyInfoStoreDisk(self.py_info_at, path)
def py_info_clear(self):
""" """
py_info_folder = self.py_info_at
with py_info_folder:
for filename in py_info_folder.path.iterdir():
if filename.suffix == ".json":
with py_info_folder.lock_for_key(filename.stem):
if filename.exists():
filename.unlink()
def embed_update_log(self, distribution, for_py_version):
return EmbedDistributionUpdateStoreDisk(self.lock / "wheel" / for_py_version / "embed" / "3", distribution)
@property
def house(self):
path = self.lock.path / "wheel" / "house"
path.mkdir(parents=True, exist_ok=True)
return path
def wheel_image(self, for_py_version, name):
return self.lock.path / "wheel" / for_py_version / "image" / "1" / name
@six.add_metaclass(ABCMeta)
class JSONStoreDisk(ContentStore):
def __init__(self, in_folder, key, msg, msg_args):
self.in_folder = in_folder
self.key = key
self.msg = msg
self.msg_args = msg_args + (self.file,)
@property
def file(self):
return self.in_folder.path / "{}.json".format(self.key)
def exists(self):
return self.file.exists()
def read(self):
data, bad_format = None, False
try:
data = json.loads(self.file.read_text())
logging.debug("got {} from %s".format(self.msg), *self.msg_args)
return data
except ValueError:
bad_format = True
except Exception: # noqa
pass
if bad_format:
try:
self.remove()
except OSError: # reading and writing on the same file may cause race on multiple processes
pass
return None
def remove(self):
self.file.unlink()
logging.debug("removed {} at %s".format(self.msg), *self.msg_args)
@contextmanager
def locked(self):
with self.in_folder.lock_for_key(self.key):
yield
def write(self, content):
folder = self.file.parent
folder.mkdir(parents=True, exist_ok=True)
self.file.write_text(ensure_text(json.dumps(content, sort_keys=True, indent=2)))
logging.debug("wrote {} at %s".format(self.msg), *self.msg_args)
class PyInfoStoreDisk(JSONStoreDisk):
def __init__(self, in_folder, path):
key = sha256(str(path).encode("utf-8") if six.PY3 else str(path)).hexdigest()
super(PyInfoStoreDisk, self).__init__(in_folder, key, "python info of %s", (path,))
class EmbedDistributionUpdateStoreDisk(JSONStoreDisk):
def __init__(self, in_folder, distribution):
super(EmbedDistributionUpdateStoreDisk, self).__init__(
in_folder,
distribution,
"embed update of distribution %s",
(distribution,),
)

View file

@ -0,0 +1,27 @@
from __future__ import absolute_import, unicode_literals
import logging
from tempfile import mkdtemp
from virtualenv.util.path import safe_delete
from .via_disk_folder import AppDataDiskFolder
class TempAppData(AppDataDiskFolder):
transient = True
can_update = False
def __init__(self):
super(TempAppData, self).__init__(folder=mkdtemp())
logging.debug("created temporary app data folder %s", self.lock.path)
def reset(self):
"""this is a temporary folder, is already empty to start with"""
def close(self):
logging.debug("remove temporary app data folder %s", self.lock.path)
safe_delete(self.lock.path)
def embed_update_log(self, distribution, for_py_version):
raise NotImplementedError