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 2, 2020
1 parent 0dd42a8 commit a48dc2a
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 17 deletions.
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
2 changes: 1 addition & 1 deletion src/virtualenv/interpreters/create/venv.py
Original file line number Diff line number Diff line change
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 = " [%(asctime)s] %(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
5 changes: 3 additions & 2 deletions src/virtualenv/seed/embed/base_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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",
Expand All @@ -39,6 +40,6 @@ def add_parser_arguments(cls, parser):
"--{}".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",
)
14 changes: 13 additions & 1 deletion src/virtualenv/seed/embed/link_app_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,21 @@ def run(self, creator):
return
cache = get_default_data_dir() / "seed-v1"
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.download,
None if self.pip_version == "latest" else self.pip_version,
None if self.setuptools_version == "latest" else self.setuptools_version,
)
pip_install(name_to_whl, creator, cache)

def __str__(self):
return "LinkFromAppData {} {}".format(
"pip{}".format("={}".format(self.pip_version)),
"setuptools{}".format("={}".format(self.setuptools_version)),
)


def pip_install(wheels, creator, cache):
site_package, bin_dir, env_exe = creator.site_packages[0], creator.bin_dir, creator.exe
Expand Down
5 changes: 4 additions & 1 deletion src/virtualenv/seed/embed/pip_invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def run(self, creator):
cmd.extend(["--find-links", str(folder)])
if not self.download:
cmd.append("--no-index")
for key, version in {"pip": self.pip_version, "setuptools": self.setuptools_version}.items():
for key, version in {
"pip": None if self.pip_version == "latest" else self.pip_version,
"setuptools": None if self.setuptools_version == "latest" else self.setuptools_version,
}.items():
cmd.append("{}{}".format(key, "=={}".format(version) if version is not None else ""))

env = os.environ.copy()
Expand Down
9 changes: 7 additions & 2 deletions src/virtualenv/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ def _create(self):

def _seed(self):
if self.seeder is not None:
logging.info("add seed packages via %s", self.seeder)
self.seeder.run(self.creator)

def _activate(self):
for activator in self.activators:
activator.generate(self.creator)
if self.activators:
logging.info(
"add activators for %s", "|".join(type(i).__name__.replace("Activator", "") for i in self.activators)
)
for activator in self.activators:
activator.generate(self.creator)


_DEBUG_MARKER = "=" * 30 + " target debug " + "=" * 30
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/config/test_env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _exc(of):
def test_value_bad(monkeypatch, caplog, empty_conf):
monkeypatch.setenv(str("VIRTUALENV_VERBOSE"), str("a"))
result = parse_cli([])
assert result.verbosity == 3
assert result.verbosity == 2
msg = "env var VIRTUALENV_VERBOSE failed to convert 'a' as {!r} because {!r}".format(int, _exc("a"))
# one for the core parse, one for the normal one
assert caplog.messages == [msg], "{}{}".format(caplog.text, msg)
11 changes: 10 additions & 1 deletion tests/unit/interpreters/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ def cleanup_sys_path(paths):
)
def test_create_no_seed(python, use_venv, global_access, tmp_path, coverage_env, special_name_dir):
dest = special_name_dir
cmd = ["-v", "-v", "-p", six.ensure_text(python), six.ensure_text(str(dest)), "--without-pip", "--activators", ""]
cmd = [
"-v",
"-v",
"-p",
six.ensure_text(python),
six.ensure_text(str(dest)),
"--without-pip",
"--activators",
"",
]
if global_access:
cmd.append("--system-site-packages")
if use_venv:
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import absolute_import, unicode_literals

import pytest
import six

from virtualenv import __version__
from virtualenv.run import run_via_cli


Expand All @@ -11,3 +15,19 @@ def test_help(capsys):
out, err = capsys.readouterr()
assert not err
assert out


def test_version(capsys):
with pytest.raises(SystemExit) as context:
run_via_cli(args=["--version"])
assert context.value.code == 0

out, err = capsys.readouterr()
extra = out if six.PY2 else err
content = out if six.PY3 else err
assert not extra

assert __version__ in content
import virtualenv

assert virtualenv.__file__ in content

0 comments on commit a48dc2a

Please sign in to comment.