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,56 @@
from __future__ import absolute_import, unicode_literals
from argparse import ArgumentTypeError
from collections import OrderedDict
from .base import ComponentBuilder
class ActivationSelector(ComponentBuilder):
def __init__(self, interpreter, parser):
self.default = None
possible = OrderedDict(
(k, v) for k, v in self.options("virtualenv.activate").items() if v.supports(interpreter)
)
super(ActivationSelector, self).__init__(interpreter, parser, "activators", possible)
self.parser.description = "options for activation scripts"
self.active = None
def add_selector_arg_parse(self, name, choices):
self.default = ",".join(choices)
self.parser.add_argument(
"--{}".format(name),
default=self.default,
metavar="comma_sep_list",
required=False,
help="activators to generate - default is all supported",
type=self._extract_activators,
)
def _extract_activators(self, entered_str):
elements = [e.strip() for e in entered_str.split(",") if e.strip()]
missing = [e for e in elements if e not in self.possible]
if missing:
raise ArgumentTypeError("the following activators are not available {}".format(",".join(missing)))
return elements
def handle_selected_arg_parse(self, options):
selected_activators = (
self._extract_activators(self.default) if options.activators is self.default else options.activators
)
self.active = {k: v for k, v in self.possible.items() if k in selected_activators}
self.parser.add_argument(
"--prompt",
dest="prompt",
metavar="prompt",
help=(
"provides an alternative prompt prefix for this environment "
"(value of . means name of the current working directory)"
),
default=None,
)
for activator in self.active.values():
activator.add_parser_arguments(self.parser, self.interpreter)
def create(self, options):
return [activator_class(options) for activator_class in self.active.values()]

View file

@ -0,0 +1,65 @@
from __future__ import absolute_import, unicode_literals
import sys
from collections import OrderedDict
if sys.version_info >= (3, 8):
from importlib.metadata import entry_points
importlib_metadata_version = ()
else:
from importlib_metadata import entry_points, version
importlib_metadata_version = tuple(int(i) for i in version("importlib_metadata").split(".")[:2])
class PluginLoader(object):
_OPTIONS = None
_ENTRY_POINTS = None
@classmethod
def entry_points_for(cls, key):
if sys.version_info >= (3, 10) or importlib_metadata_version >= (3, 6):
return OrderedDict((e.name, e.load()) for e in cls.entry_points().select(group=key))
else:
return OrderedDict((e.name, e.load()) for e in cls.entry_points().get(key, {}))
@staticmethod
def entry_points():
if PluginLoader._ENTRY_POINTS is None:
PluginLoader._ENTRY_POINTS = entry_points()
return PluginLoader._ENTRY_POINTS
class ComponentBuilder(PluginLoader):
def __init__(self, interpreter, parser, name, possible):
self.interpreter = interpreter
self.name = name
self._impl_class = None
self.possible = possible
self.parser = parser.add_argument_group(title=name)
self.add_selector_arg_parse(name, list(self.possible))
@classmethod
def options(cls, key):
if cls._OPTIONS is None:
cls._OPTIONS = cls.entry_points_for(key)
return cls._OPTIONS
def add_selector_arg_parse(self, name, choices):
raise NotImplementedError
def handle_selected_arg_parse(self, options):
selected = getattr(options, self.name)
if selected not in self.possible:
raise RuntimeError("No implementation for {}".format(self.interpreter))
self._impl_class = self.possible[selected]
self.populate_selected_argparse(selected, options.app_data)
return selected
def populate_selected_argparse(self, selected, app_data):
self.parser.description = "options for {} {}".format(self.name, selected)
self._impl_class.add_parser_arguments(self.parser, self.interpreter, app_data)
def create(self, options):
return self._impl_class(options, self.interpreter)

View file

@ -0,0 +1,77 @@
from __future__ import absolute_import, unicode_literals
from collections import OrderedDict, defaultdict, namedtuple
from virtualenv.create.describe import Describe
from virtualenv.create.via_global_ref.builtin.builtin_way import VirtualenvBuiltin
from .base import ComponentBuilder
CreatorInfo = namedtuple("CreatorInfo", ["key_to_class", "key_to_meta", "describe", "builtin_key"])
class CreatorSelector(ComponentBuilder):
def __init__(self, interpreter, parser):
creators, self.key_to_meta, self.describe, self.builtin_key = self.for_interpreter(interpreter)
super(CreatorSelector, self).__init__(interpreter, parser, "creator", creators)
@classmethod
def for_interpreter(cls, interpreter):
key_to_class, key_to_meta, builtin_key, describe = OrderedDict(), {}, None, None
errors = defaultdict(list)
for key, creator_class in cls.options("virtualenv.create").items():
if key == "builtin":
raise RuntimeError("builtin creator is a reserved name")
meta = creator_class.can_create(interpreter)
if meta:
if meta.error:
errors[meta.error].append(creator_class)
else:
if "builtin" not in key_to_class and issubclass(creator_class, VirtualenvBuiltin):
builtin_key = key
key_to_class["builtin"] = creator_class
key_to_meta["builtin"] = meta
key_to_class[key] = creator_class
key_to_meta[key] = meta
if describe is None and issubclass(creator_class, Describe) and creator_class.can_describe(interpreter):
describe = creator_class
if not key_to_meta:
if errors:
rows = ["{} for creators {}".format(k, ", ".join(i.__name__ for i in v)) for k, v in errors.items()]
raise RuntimeError("\n".join(rows))
else:
raise RuntimeError("No virtualenv implementation for {}".format(interpreter))
return CreatorInfo(
key_to_class=key_to_class,
key_to_meta=key_to_meta,
describe=describe,
builtin_key=builtin_key,
)
def add_selector_arg_parse(self, name, choices):
# prefer the built-in venv if present, otherwise fallback to first defined type
choices = sorted(choices, key=lambda a: 0 if a == "builtin" else 1)
default_value = self._get_default(choices)
self.parser.add_argument(
"--{}".format(name),
choices=choices,
default=default_value,
required=False,
help="create environment via{}".format(
"" if self.builtin_key is None else " (builtin = {})".format(self.builtin_key),
),
)
@staticmethod
def _get_default(choices):
return next(iter(choices))
def populate_selected_argparse(self, selected, app_data):
self.parser.description = "options for {} {}".format(self.name, selected)
self._impl_class.add_parser_arguments(self.parser, self.interpreter, self.key_to_meta[selected], app_data)
def create(self, options):
options.meta = self.key_to_meta[getattr(options, self.name)]
if not issubclass(self._impl_class, Describe):
options.describe = self.describe(options, self.interpreter)
return super(CreatorSelector, self).create(options)

View file

@ -0,0 +1,35 @@
from __future__ import absolute_import, unicode_literals
from .base import PluginLoader
class Discovery(PluginLoader):
""" """
def get_discover(parser, args):
discover_types = Discovery.entry_points_for("virtualenv.discovery")
discovery_parser = parser.add_argument_group(
title="discovery",
description="discover and provide a target interpreter",
)
choices = _get_default_discovery(discover_types)
# prefer the builtin if present, otherwise fallback to first defined type
choices = sorted(choices, key=lambda a: 0 if a == "builtin" else 1)
discovery_parser.add_argument(
"--discovery",
choices=choices,
default=next(iter(choices)),
required=False,
help="interpreter discovery method",
)
options, _ = parser.parse_known_args(args)
discover_class = discover_types[options.discovery]
discover_class.add_parser_arguments(discovery_parser)
options, _ = parser.parse_known_args(args, namespace=options)
discover = discover_class(options)
return discover
def _get_default_discovery(discover_types):
return list(discover_types.keys())

View file

@ -0,0 +1,35 @@
from __future__ import absolute_import, unicode_literals
from .base import ComponentBuilder
class SeederSelector(ComponentBuilder):
def __init__(self, interpreter, parser):
possible = self.options("virtualenv.seed")
super(SeederSelector, self).__init__(interpreter, parser, "seeder", possible)
def add_selector_arg_parse(self, name, choices):
self.parser.add_argument(
"--{}".format(name),
choices=choices,
default=self._get_default(),
required=False,
help="seed packages install method",
)
self.parser.add_argument(
"--no-seed",
"--without-pip",
help="do not install seed packages",
action="store_true",
dest="no_seed",
)
@staticmethod
def _get_default():
return "app-data"
def handle_selected_arg_parse(self, options):
return super(SeederSelector, self).handle_selected_arg_parse(options)
def create(self, options):
return self._impl_class(options)