Skip to content

Commit

Permalink
interface compatibility with before rewrite
Browse files Browse the repository at this point in the history
Ensure that what ran with virtualenv 17 will continue running in a post
rewrite world minus the deprecated flags, plus the relocatable feature.

Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Jan 3, 2020
1 parent 0dd42a8 commit 24ffd0c
Show file tree
Hide file tree
Showing 39 changed files with 527 additions and 346 deletions.
2 changes: 1 addition & 1 deletion src/virtualenv/activation/bash/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.util import Path
from virtualenv.util.path import Path

from ..via_template import ViaTemplateActivator

Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/batch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.util import Path
from virtualenv.util.path import Path

from ..via_template import ViaTemplateActivator

Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/cshell/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.util import Path
from virtualenv.util.path import Path

from ..via_template import ViaTemplateActivator

Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/fish/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.util import Path
from virtualenv.util.path import Path

from ..via_template import ViaTemplateActivator

Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/powershell/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.util import Path
from virtualenv.util.path import Path

from ..via_template import ViaTemplateActivator

Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import os

from virtualenv.util import Path
from virtualenv.util.path import Path

from ..via_template import ViaTemplateActivator

Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/activation/xonosh/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import absolute_import, unicode_literals

from virtualenv.util import Path
from virtualenv.util.path import Path

from ..via_template import ViaTemplateActivator

Expand Down
10 changes: 2 additions & 8 deletions src/virtualenv/config/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@
import os

from virtualenv.info import PY3, get_default_config_dir
from virtualenv.util import Path
from virtualenv.util import ConfigParser
from virtualenv.util.path import Path

from .convert import convert

try:
import ConfigParser
except ImportError:
# noinspection PyPep8Naming
import configparser as ConfigParser


DEFAULT_CONFIG_FILE = get_default_config_dir() / "virtualenv.ini"


Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from appdirs import user_config_dir, user_data_dir

from virtualenv.util import Path
from virtualenv.util.path import Path

IS_PYPY = hasattr(sys, "pypy_version_info")
PY3 = sys.version_info[0] == 3
Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/interpreters/create/cpython/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import six

from virtualenv.interpreters.create.via_global_ref import ViaGlobalRef
from virtualenv.util import Path, copy, ensure_dir, symlink
from virtualenv.util.path import Path, copy, ensure_dir, symlink


@six.add_metaclass(abc.ABCMeta)
Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/interpreters/create/cpython/cpython2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import six

from virtualenv.util import Path, copy
from virtualenv.util.path import Path, copy

from .common import CPython, CPythonPosix, CPythonWindows

Expand Down
2 changes: 1 addition & 1 deletion src/virtualenv/interpreters/create/cpython/cpython3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import six

from virtualenv.util import Path, copy
from virtualenv.util.path import Path, copy

from .common import CPython, CPythonPosix, CPythonWindows

Expand Down
4 changes: 3 additions & 1 deletion src/virtualenv/interpreters/create/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

from virtualenv.info import IS_WIN
from virtualenv.pyenv_cfg import PyEnvCfg
from virtualenv.util import Path, run_cmd
from virtualenv.util.path import Path
from virtualenv.util.subprocess import run_cmd
from virtualenv.version import __version__

HERE = Path(__file__).absolute().parent
Expand Down Expand Up @@ -104,6 +105,7 @@ def non_write_able(dest, value):

def run(self):
if self.dest_dir.exists() and self.clear:
logging.debug("delete %s", self.dest_dir)
shutil.rmtree(str(self.dest_dir), ignore_errors=True)
self.create()
self.set_pyenv_cfg()
Expand Down
4 changes: 2 additions & 2 deletions src/virtualenv/interpreters/create/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def encode_path(value):
return None
if isinstance(value, bytes):
return value.decode(sys.getfilesystemencoding())
if isinstance(value, type):
return repr(value)
elif not isinstance(value, str):
return repr(value if isinstance(value, type) else type(value))
return value


Expand Down
4 changes: 2 additions & 2 deletions src/virtualenv/interpreters/create/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from virtualenv.error import ProcessCallFailed
from virtualenv.interpreters.discovery.py_info import CURRENT
from virtualenv.util import run_cmd
from virtualenv.util.subprocess import run_cmd

from .via_global_ref import ViaGlobalRef

Expand Down Expand Up @@ -41,7 +41,7 @@ def create_inline(self):

def create_via_sub_process(self):
cmd = self.get_host_create_cmd()
logging.info("create with venv %s", " ".join(cmd))
logging.info("using host built-in venv to create via %s", " ".join(cmd))
code, out, err = run_cmd(cmd)
if code != 0:
raise ProcessCallFailed(code, out, err, cmd)
Expand Down
1 change: 1 addition & 0 deletions src/virtualenv/interpreters/create/via_global_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def add_parser_arguments(cls, parser, interpreter):
)
group.add_argument(
"--copies",
"--always-copy",
default=not symlink,
action="store_false",
dest="symlinks",
Expand Down
3 changes: 2 additions & 1 deletion src/virtualenv/interpreters/discovery/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def __str__(self):

def get_interpreter(key):
spec = PythonSpec.from_string_spec(key)
logging.debug("find interpreter for spec %r", spec)
logging.info("find interpreter for spec %r", spec)
proposed_paths = set()
for interpreter, impl_must_match in propose_interpreters(spec):
if interpreter.executable not in proposed_paths:
logging.debug("proposed %s", interpreter)
if interpreter.satisfies(spec, impl_must_match):
logging.info("accepted target interpreter %s", interpreter)
return interpreter
proposed_paths.add(interpreter.executable)

Expand Down
10 changes: 6 additions & 4 deletions src/virtualenv/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
import sys

import six

LEVELS = {
0: logging.CRITICAL,
1: logging.ERROR,
Expand All @@ -23,11 +25,11 @@ def setup_report(verbose, quiet):
verbosity = MAX_LEVEL # pragma: no cover
level = LEVELS[verbosity]
msg_format = "%(message)s"
if level >= logging.DEBUG:
locate = "pathname" if level > logging.DEBUG else "module"
msg_format += "[%(asctime)s] %(levelname)s [%({})s:%(lineno)d]".format(locate)
if level <= logging.DEBUG:
locate = "pathname" if level < logging.DEBUG else "module"
msg_format = "%(relativeCreated)d {} [%(levelname)s %({})s:%(lineno)d]".format(msg_format, locate)

formatter = logging.Formatter(str(msg_format))
formatter = logging.Formatter(six.ensure_str(msg_format))
stream_handler = logging.StreamHandler(stream=sys.stdout)
stream_handler.setLevel(level)
LOGGER.setLevel(logging.NOTSET)
Expand Down
12 changes: 11 additions & 1 deletion src/virtualenv/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .config.cli.parser import VirtualEnvConfigParser
from .report import LEVELS, setup_report
from .session import Session
from .version import __version__


def run_via_cli(args):
Expand All @@ -23,6 +24,7 @@ def run_via_cli(args):

def session_via_cli(args):
parser = VirtualEnvConfigParser()
add_version_flag(parser)
options, verbosity = _do_report_setup(parser, args)
discover = _get_discover(parser, args, options)
interpreter = discover.interpreter
Expand All @@ -44,12 +46,20 @@ def session_via_cli(args):
return session


def add_version_flag(parser):
import virtualenv

parser.add_argument(
"--version", action="version", version="%(prog)s {} from {}".format(__version__, virtualenv.__file__)
)


def _do_report_setup(parser, args):
level_map = ", ".join("{}:{}".format(c, logging.getLevelName(l)) for c, l in sorted(list(LEVELS.items())))
msg = "verbosity = verbose - quiet, default {}, count mapping = {{{}}}"
verbosity_group = parser.add_argument_group(msg.format(logging.getLevelName(LEVELS[3]), level_map))
verbosity = verbosity_group.add_mutually_exclusive_group()
verbosity.add_argument("-v", "--verbose", action="count", dest="verbose", help="increase verbosity", default=3)
verbosity.add_argument("-v", "--verbose", action="count", dest="verbose", help="increase verbosity", default=2)
verbosity.add_argument("-q", "--quiet", action="count", dest="quiet", help="decrease verbosity", default=0)
options, _ = parser.parse_known_args(args)
verbosity_value = setup_report(options.verbose, options.quiet)
Expand Down
47 changes: 42 additions & 5 deletions src/virtualenv/seed/embed/base_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import six

from virtualenv.util.path import Path

from ..seeder import Seeder


Expand All @@ -13,8 +15,11 @@ def __init__(self, options):
super(Seeder, self).__init__()
self.enabled = options.without_pip is False
self.download = options.download
self.pip_version = options.pip
self.setuptools_version = options.setuptools
self.extra_search_dir = [i.resolve() for i in options.extra_search_dir if i.exists()]
self.pip_version = None if options.pip == "latest" else options.pip
self.setuptools_version = None if options.setuptools == "latest" else options.setuptools
self.no_pip = options.no_pip
self.no_setuptools = options.no_setuptools

@classmethod
def add_parser_arguments(cls, parser):
Expand All @@ -28,17 +33,49 @@ def add_parser_arguments(cls, parser):
)
group.add_argument(
"--no-download",
"--never-download",
dest="download",
action="store_false",
help="do not download latest pip/setuptools from PyPi",
default=True,
)

parser.add_argument(
"--extra-search-dir",
metavar="d",
type=Path,
nargs="+",
help="a location containing wheels candidates to install from",
default=[],
)
for package in ["pip", "setuptools"]:
parser.add_argument(
"--{}".format(package),
dest=package,
metavar="version",
help="{} version to install, default: latest from cache, bundle for bundled".format(package),
default=None,
help="{} version to install, bundle for bundled".format(package),
default="latest",
)
for extra, package in [
("", "pip"),
("", "setuptools"),
("N/A - kept only for backwards compatibility; ", "wheel"),
]:
parser.add_argument(
"--no-{}".format(package),
dest="no_{}".format(package),
action="store_true",
help="{}do not install {}".format(extra, package),
default=False,
)

def __str__(self):
result = self.__class__.__name__
if self.extra_search_dir:
result += " extra search dirs = {}".format(
", ".join(six.ensure_text(str(i)) for i in self.extra_search_dir)
)
if self.no_pip is False:
result += " pip{}".format("={}".format(self.pip_version or "latest"))
if self.no_setuptools is False:
result += " setuptools{}".format("={}".format(self.setuptools_version or "latest"))
return result
32 changes: 24 additions & 8 deletions src/virtualenv/seed/embed/link_app_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,46 @@
from shutil import copytree
from textwrap import dedent

import six
from six import PY3

from virtualenv.info import get_default_data_dir
from virtualenv.util import Path
from virtualenv.util import ConfigParser
from virtualenv.util.path import Path

from .base_embed import BaseEmbed
from .wheels.acquire import get_wheel

try:
import ConfigParser
except ImportError:
# noinspection PyPep8Naming
import configparser as ConfigParser


class LinkFromAppData(BaseEmbed):
def __init__(self, options):
super(LinkFromAppData, self).__init__(options)
self.clear_app_data = options.clear_app_data

def run(self, creator):
if not self.enabled:
return
cache = get_default_data_dir() / "seed-v1"
if self.clear_app_data:
logging.debug("delete %s", cache)
shutil.rmtree(six.ensure_text(str(cache)))
version = creator.interpreter.version_release_str
name_to_whl = get_wheel(version, cache, self.download, self.pip_version, self.setuptools_version)
name_to_whl = get_wheel(
version, cache, self.extra_search_dir, self.download, self.pip_version, self.setuptools_version,
)
pip_install(name_to_whl, creator, cache)

@classmethod
def add_parser_arguments(cls, parser):
super(LinkFromAppData, cls).add_parser_arguments(parser)
parser.add_argument(
"--clear-app-data",
dest="clear_app_data",
action="store_true",
help="clear the app data folder",
default=False,
)


def pip_install(wheels, creator, cache):
site_package, bin_dir, env_exe = creator.site_packages[0], creator.bin_dir, creator.exe
Expand Down
Loading

0 comments on commit 24ffd0c

Please sign in to comment.