Skip to content

Commit

Permalink
Add support for prelink_message. (conda#11123)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jannis Leidel <[email protected]>
  • Loading branch information
3 people authored Mar 8, 2022
1 parent ca479f2 commit f0677d5
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 44 deletions.
1 change: 0 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ tasks:
git tag "$(git tag --sort=committerdate | tail -1).dev"
eval "$(sudo /opt/conda/bin/python -m conda init --dev bash)"
sudo su root -c "/opt/conda/bin/conda install -yq conda-build"
conda-build tests/test-recipes/activate_deactivate_package
command: |
eval "$(sudo /opt/conda/bin/python -m conda init --dev bash)"
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ to do it manually in other modes:

```bash
$ conda install conda-build
$ conda-build tests/test-recipes/activate_deactivate_package
$ conda-build tests/test-recipes/activate_deactivate_package tests/test-recipes/pre_link_messages_package

```

Check `dev/linux/integration.sh` and `dev\windows\integration.bat` for more details.
Expand Down
43 changes: 37 additions & 6 deletions conda/core/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import absolute_import, division, print_function, unicode_literals

import itertools
from collections import defaultdict, namedtuple
from logging import getLogger
import os
from os.path import basename, dirname, isdir, join
import sys
from pathlib import Path
from traceback import format_exception_only
from textwrap import indent
import warnings

from .package_cache_data import PackageCacheData
Expand All @@ -24,6 +27,7 @@
from .._vendor.toolz import concat, concatv, interleave
from ..base.constants import DEFAULTS_CHANNEL_NAME, PREFIX_MAGIC_FILE, SafetyChecks
from ..base.context import context
from ..cli.common import confirm_yn
from ..common.compat import ensure_text_type, iteritems, itervalues, odict, on_win, text_type
from ..common.io import Spinner, dashlist, time_recorder
from ..common.io import DummyExecutor, ThreadLimitedThreadPoolExecutor
Expand All @@ -32,7 +36,8 @@
from ..common.signals import signal_handler
from ..exceptions import (DisallowedPackageError, EnvironmentNotWritableError,
KnownPackageClobberError, LinkError, RemoveError,
SharedLinkPathClobberError, UnknownPackageClobberError, maybe_raise)
SharedLinkPathClobberError, UnknownPackageClobberError, maybe_raise,
CondaSystemExit)
from ..gateways.disk import mkdir_p
from ..gateways.disk.delete import rm_rf
from ..gateways.disk.read import isfile, lexists, read_package_info
Expand Down Expand Up @@ -227,25 +232,51 @@ def verify(self):
self._verified = True
return

with Spinner("Verifying transaction", not context.verbosity and not context.quiet,
context.json):
with Spinner(
"Verifying transaction", not context.verbosity and not context.quiet, context.json
):
exceptions = self._verify(self.prefix_setups, self.prefix_action_groups)
if exceptions:
try:
maybe_raise(CondaMultiError(exceptions), context)
except:
rm_rf(self.transaction_context['temp_dir'])
rm_rf(self.transaction_context["temp_dir"])
raise
log.info(exceptions)

try:
self._verify_pre_link_message(
itertools.chain(
*(act.link_action_groups for act in self.prefix_action_groups.values())
)
)
except CondaSystemExit:
rm_rf(self.transaction_context["temp_dir"])
raise
self._verified = True

def _verify_pre_link_message(self, all_link_groups):
flag_pre_link = False
for act in all_link_groups:
prelink_msg_dir = (
Path(act.pkg_data.extracted_package_dir) / "info" / "prelink_messages"
)
all_msg_subdir = list(item for item in prelink_msg_dir.glob("**/*") if item.is_file())
if prelink_msg_dir.is_dir() and all_msg_subdir:
print("\n\nThe following PRELINK MESSAGES are INCLUDED:\n\n")
flag_pre_link = True

for msg_file in all_msg_subdir:
print(f" File {msg_file.name}:\n")
print(indent(msg_file.read_text(), " "))
print("")
if flag_pre_link:
confirm_yn()

def execute(self):
if not self._verified:
self.verify()

assert not context.dry_run

try:
self._execute(tuple(concat(interleave(itervalues(self.prefix_action_groups)))))
finally:
Expand Down
9 changes: 3 additions & 6 deletions conda/testing/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

import warnings
import py
import pytest

from conda.common.compat import PY3
from conda.gateways.disk.create import TemporaryDirectory
from conda.core.subdir_data import SubdirData


@pytest.fixture(autouse=True)
def suppress_resource_warning():
'''
"""
Suppress `Unclosed Socket Warning`
It seems urllib3 keeps a socket open to avoid costly recreation costs.
xref: https://github.com/kennethreitz/requests/issues/1882
'''
if PY3:
warnings.filterwarnings("ignore", category=ResourceWarning)
"""
warnings.filterwarnings("ignore", category=ResourceWarning)


@pytest.fixture(scope='function')
Expand Down
2 changes: 1 addition & 1 deletion dev/linux/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TEST_GROUP="${TEST_GROUP:-1}"

eval "$(sudo /opt/conda/bin/python -m conda init --dev bash)"
sudo su root -c "/opt/conda/bin/conda install -yq conda-build"
conda-build tests/test-recipes/activate_deactivate_package
conda-build tests/test-recipes/activate_deactivate_package tests/test-recipes/pre_link_messages_package
conda info
pytest -m "integration" -v --splits ${TEST_SPLITS} --group=${TEST_GROUP}
python -m conda.common.io
2 changes: 1 addition & 1 deletion dev/windows/integration.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pushd %TEMP% || goto :error
cd \conda_src || goto :error
CALL dev-init.bat || goto :error
CALL conda info || goto :error
CALL conda-build tests\test-recipes\activate_deactivate_package || goto :error
CALL conda-build tests\test-recipes\activate_deactivate_package tests\test-recipes\pre_link_messages_package || goto :error
CALL pytest -m "integration" --basetemp=C:\tmp -v --splits=%TEST_SPLITS% --group=%TEST_GROUP% || goto :error
goto :EOF

Expand Down
69 changes: 42 additions & 27 deletions tests/cli/test_cli_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,46 @@
from mock import patch


class TestCliInstall(TestCase):
def setUp(self):
self.prefix = tempfile.mkdtemp()
self.testenv = tempfile.mkdtemp()
run_command(Commands.CREATE, self.prefix, 'python=3.7')

def tearDown(self):
rm_rf(self.prefix)
rm_rf(self.testenv)

@pytest.mark.integration
def test_find_conflicts_called_once(self):
bad_deps = {'python': {((MatchSpec("statistics"), MatchSpec("python[version='>=2.7,<2.8.0a0']")), 'python=3')}}

with patch('conda.resolve.Resolve.find_conflicts') as monkey:
monkey.side_effect = UnsatisfiableError(bad_deps, strict=True)
with self.assertRaises(UnsatisfiableError):
@pytest.fixture
def fix_cli_install(tmpdir):
prefix = tmpdir.mkdir("cli_install_prefix")
test_env = tmpdir.mkdir("cli_install_test_env")
run_command(Commands.CREATE, str(prefix), 'python=3.7')
yield str(prefix), str(test_env)
rm_rf(prefix)
rm_rf(test_env)


@pytest.mark.integration
def test_pre_link_message(fix_cli_install, conda_build_recipes):
prefix = fix_cli_install[0]
with patch("conda.cli.common.confirm_yn", return_value=True):
stdout, _, _ = run_command(
Commands.INSTALL, prefix, "pre_link_messages_package", "--use-local"
)
assert "Lorem ipsum dolor sit amet" in stdout


@pytest.mark.integration
def test_find_conflicts_called_once(fix_cli_install):
prefix, test_env = fix_cli_install
bad_deps = {'python': {((MatchSpec("statistics"), MatchSpec("python[version='>=2.7,<2.8.0a0']")), 'python=3')}}

with patch(
"conda.resolve.Resolve.find_conflicts",
side_effect=UnsatisfiableError(bad_deps, strict=True),
) as mocked_find_conflicts:
with pytest.raises(UnsatisfiableError):
# Statistics is a py27 only package allowing us a simple unsatisfiable case
stdout, stderr, _ = run_command(Commands.INSTALL, self.prefix, 'statistics')
self.assertEqual(monkey.call_count, 1)
monkey.reset_mock()
with self.assertRaises(UnsatisfiableError):
stdout, stderr, _ = run_command(Commands.INSTALL, self.prefix, 'statistics', '--freeze-installed')
self.assertEqual(monkey.call_count, 1)
monkey.reset_mock()
with self.assertRaises(UnsatisfiableError):
stdout, stderr, _ = run_command(Commands.CREATE, self.testenv, 'statistics', 'python=3.7')
self.assertEqual(monkey.call_count, 1)
run_command(Commands.INSTALL, prefix, "statistics")
assert mocked_find_conflicts.call_count == 1

mocked_find_conflicts.reset_mock()
with pytest.raises(UnsatisfiableError):
run_command(Commands.INSTALL, prefix, "statistics", "--freeze-installed")
assert mocked_find_conflicts.call_count == 1

mocked_find_conflicts.reset_mock()
with pytest.raises(UnsatisfiableError):
run_command(Commands.CREATE, test_env, "statistics", "python=3.7")
assert mocked_find_conflicts.call_count == 1
15 changes: 14 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause

from pathlib import Path
import subprocess
import sys

import pytest

from conda.testing.fixtures import (
suppress_resource_warning,
tmpdir,
Expand All @@ -24,3 +27,13 @@ def pytest_addoption(parser):
def pytest_generate_tests(metafunc):
if 'shell' in metafunc.fixturenames:
metafunc.parametrize("shell", metafunc.config.option.shell)


@pytest.fixture(scope="session", autouse=True)
def conda_build_recipes():
test_recipes = Path(__file__).resolve().parent / "test-recipes"
recipes_to_build = ["activate_deactivate_package", "pre_link_messages_package"]
packages = [str(test_recipes / pkg) for pkg in recipes_to_build]
cmd = ["conda-build"]
cmd.extend(packages)
subprocess.run(cmd, check=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Spam spam spam,
SPAM!
12 changes: 12 additions & 0 deletions tests/test-recipes/pre_link_messages_package/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package:
name: pre_link_messages_package
version: 1.0

build:
number: 0

about:
summary: Package to test prelink_message
prelink_message:
- message-from-pkg.txt
- message-from-pkg2.txt

0 comments on commit f0677d5

Please sign in to comment.