From 10834bc64ee581a7bf295691aaa97458de00ca00 Mon Sep 17 00:00:00 2001 From: jgsogo Date: Wed, 25 Mar 2020 17:49:13 +0100 Subject: [PATCH 1/7] do not add the /python path to sys.path --- conans/client/conan_api.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/conans/client/conan_api.py b/conans/client/conan_api.py index a8e16e1337b..4b5d85e025b 100644 --- a/conans/client/conan_api.py +++ b/conans/client/conan_api.py @@ -60,6 +60,8 @@ from conans.search.search import search_recipes from conans.tools import set_global_instances from conans.unicode import get_cwd +from conans.util.conan_v2_mode import CONAN_V2_MODE_ENVVAR +from conans.util.env_reader import get_env from conans.util.files import exception_message_safe, mkdir, save_files from conans.util.log import configure_logger from conans.util.tracer import log_command, log_exception @@ -220,8 +222,9 @@ def __init__(self, cache_folder=None, output=None, user_io=None, http_requester= # Migration system migrator = ClientMigrator(self.cache_folder, Version(client_version), self.out) migrator.migrate() - # FIXME Remove in Conan 2.0 - sys.path.append(os.path.join(self.cache_folder, "python")) + if not get_env(CONAN_V2_MODE_ENVVAR, False): + # FIXME Remove in Conan 2.0 + sys.path.append(os.path.join(self.cache_folder, "python")) def create_app(self, quiet_output=None): self.app = ConanApp(self.cache_folder, self.user_io, self.http_requester, From ce7866e62594ed6704a5cb27603752d2c70a5d1c Mon Sep 17 00:00:00 2001 From: jgsogo Date: Wed, 25 Mar 2020 18:10:05 +0100 Subject: [PATCH 2/7] deprecate'virtualenv_python' --- conans/client/generators/virtualenv_python.py | 2 ++ conans/test/conan_v2/generators/__init__.py | 0 .../conan_v2/generators/test_virtualenv_python.py | 12 ++++++++++++ 3 files changed, 14 insertions(+) create mode 100644 conans/test/conan_v2/generators/__init__.py create mode 100644 conans/test/conan_v2/generators/test_virtualenv_python.py diff --git a/conans/client/generators/virtualenv_python.py b/conans/client/generators/virtualenv_python.py index be7b97ab8ea..0d8a45950f2 100644 --- a/conans/client/generators/virtualenv_python.py +++ b/conans/client/generators/virtualenv_python.py @@ -1,4 +1,5 @@ from conans.client.generators.virtualrunenv import VirtualRunEnvGenerator +from conans.util.conan_v2_mode import conan_v2_behavior class VirtualEnvPythonGenerator(VirtualRunEnvGenerator): @@ -7,6 +8,7 @@ class VirtualEnvPythonGenerator(VirtualRunEnvGenerator): venv_name = "conanenvpython" def __init__(self, conanfile): + conan_v2_behavior("'virtualenv_python' generator is deprecated") super(VirtualEnvPythonGenerator, self).__init__(conanfile) ppath = conanfile.env.get("PYTHONPATH") if ppath: diff --git a/conans/test/conan_v2/generators/__init__.py b/conans/test/conan_v2/generators/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/conans/test/conan_v2/generators/test_virtualenv_python.py b/conans/test/conan_v2/generators/test_virtualenv_python.py new file mode 100644 index 00000000000..c470dfcbd9c --- /dev/null +++ b/conans/test/conan_v2/generators/test_virtualenv_python.py @@ -0,0 +1,12 @@ +from conans.test.utils.conan_v2_tests import ConanV2ModeTestCase + + +class VirtualenvPythonTestCase(ConanV2ModeTestCase): + + def test_deprecate_virtualenv_python(self): + t = self.get_client() + t.run("new name/version@user/channel -b") + t.run("create . name/version@user/channel") + t.run("install name/version@user/channel -g virtualenv_python", assert_error=True) + self.assertIn("ERROR: Conan v2 incompatible: 'virtualenv_python' generator is deprecated", + t.out) From 94793693c9747ea415140f373513bb781b7d7f93 Mon Sep 17 00:00:00 2001 From: jgsogo Date: Wed, 25 Mar 2020 18:37:50 +0100 Subject: [PATCH 3/7] PYTHONPATH is not added to the python sys.path to import modules --- conans/client/installer.py | 5 ++- conans/client/profile_loader.py | 1 - conans/model/conan_file.py | 7 ++- conans/test/conan_v2/test_pythonpath.py | 58 +++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 conans/test/conan_v2/test_pythonpath.py diff --git a/conans/client/installer.py b/conans/client/installer.py index fb78a44fb38..2e0cb09529d 100644 --- a/conans/client/installer.py +++ b/conans/client/installer.py @@ -14,6 +14,7 @@ from conans.client.recorder.action_recorder import INSTALL_ERROR_BUILDING, INSTALL_ERROR_MISSING, \ INSTALL_ERROR_MISSING_BUILD_FOLDER from conans.client.source import complete_recipe_sources, config_source +from conans.client.tools.env import no_op from conans.client.tools.env import pythonpath from conans.errors import (ConanException, ConanExceptionInUserConanfileMethod, conanfile_exception_formatter) @@ -26,6 +27,7 @@ from conans.model.ref import PackageReference from conans.model.user_info import UserInfo from conans.paths import BUILD_INFO, CONANINFO, RUN_LOG_NAME +from conans.util.conan_v2_mode import CONAN_V2_MODE_ENVVAR from conans.util.env_reader import get_env from conans.util.files import (clean_dirty, is_dirty, make_read_only, mkdir, rmdir, save, set_dirty, set_dirty_context_manager) @@ -547,7 +549,8 @@ def _call_package_info(self, conanfile, package_folder, ref): conanfile.cpp_info.public_deps = public_deps # Once the node is build, execute package info, so it has access to the # package folder and artifacts - with pythonpath(conanfile): # Minimal pythonpath, not the whole context, make it 50% slower + conan_v2 = get_env(CONAN_V2_MODE_ENVVAR, False) + with pythonpath(conanfile) if not conan_v2 else no_op(): # Minimal pythonpath, not the whole context, make it 50% slower with tools.chdir(package_folder): with conanfile_exception_formatter(str(conanfile), "package_info"): conanfile.package_folder = package_folder diff --git a/conans/client/profile_loader.py b/conans/client/profile_loader.py index 040e5215653..431dbad2965 100644 --- a/conans/client/profile_loader.py +++ b/conans/client/profile_loader.py @@ -140,7 +140,6 @@ def _load_profile(text, profile_path, default_folder): # Apply the automatic PROFILE_DIR variable if cwd: profile_parser.vars["PROFILE_DIR"] = os.path.abspath(cwd).replace('\\', '/') - # Allows PYTHONPATH=$PROFILE_DIR/pythontools # Replace the variables from parents in the current profile profile_parser.apply_vars() diff --git a/conans/model/conan_file.py b/conans/model/conan_file.py index 92871b39a12..22e3937f21f 100644 --- a/conans/model/conan_file.py +++ b/conans/model/conan_file.py @@ -14,7 +14,9 @@ from conans.model.requires import Requirements from conans.model.user_info import DepsUserInfo from conans.paths import RUN_LOG_NAME +from conans.util.conan_v2_mode import CONAN_V2_MODE_ENVVAR from conans.util.conan_v2_mode import conan_v2_behavior +from conans.util.env_reader import get_env def create_options(conanfile): @@ -72,8 +74,11 @@ def create_settings(conanfile, settings): @contextmanager def _env_and_python(conanfile): with environment_append(conanfile.env): - with pythonpath(conanfile): + if get_env(CONAN_V2_MODE_ENVVAR, False): yield + else: + with pythonpath(conanfile): + yield def get_env_context_manager(conanfile, without_python=False): diff --git a/conans/test/conan_v2/test_pythonpath.py b/conans/test/conan_v2/test_pythonpath.py new file mode 100644 index 00000000000..55283ed3f9d --- /dev/null +++ b/conans/test/conan_v2/test_pythonpath.py @@ -0,0 +1,58 @@ +import textwrap + +from conans.model.ref import ConanFileReference +from conans.test.utils.conan_v2_tests import ConanV2ModeTestCase + + +class PythonBuildTest(ConanV2ModeTestCase): + conanfile = textwrap.dedent(""" + from conans import ConanFile + + class ConanToolPackage(ConanFile): + name = "conantool" + version = "1.0" + exports = "*" + build_policy = "missing" + + def package(self): + self.copy("*") + + def package_info(self): + self.env_info.PYTHONPATH.append(self.package_folder) + """) + + tooling = textwrap.dedent(""" + def bar(output): + output.info("Hello Bar") + """) + + reuse = textwrap.dedent(""" + from conans import ConanFile, tools + + class ToolsTest(ConanFile): + name = "consumer" + version = "0.1" + requires = "conantool/1.0@conan/stable" + + def build(self): + self.output.info("PYTHONPATH: {}".format(self.deps_env_info["conantool"].PYTHONPATH)) + + def package_info(self): + import tooling + tooling.bar(self.output) + """) + + def test_deprecate_pythonpath(self): + conantool_ref = ConanFileReference.loads("conantool/1.0@conan/stable") + # Create a package that exports python code + t = self.get_client() + t.save({'conanfile.py': self.conanfile, + 'tooling.py': self.tooling}) + t.run("export . conan/stable") + + # Try to reuse it + t.save({'conanfile.py': self.reuse}, clean_first=True) + t.run("create .", assert_error=True) + packages_path = t.cache.package_layout(conantool_ref).packages() + self.assertIn("consumer/0.1: PYTHONPATH: ['{}".format(packages_path), t.out) + self.assertIn("ModuleNotFoundError: No module named 'tooling'", t.out) From 28afa4c856ffb162bed148286d085d1294740de2 Mon Sep 17 00:00:00 2001 From: jgsogo Date: Wed, 25 Mar 2020 19:00:31 +0100 Subject: [PATCH 4/7] different error for py2 --- conans/test/conan_v2/test_pythonpath.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/conans/test/conan_v2/test_pythonpath.py b/conans/test/conan_v2/test_pythonpath.py index 55283ed3f9d..1b885fe5746 100644 --- a/conans/test/conan_v2/test_pythonpath.py +++ b/conans/test/conan_v2/test_pythonpath.py @@ -2,7 +2,7 @@ from conans.model.ref import ConanFileReference from conans.test.utils.conan_v2_tests import ConanV2ModeTestCase - +import six class PythonBuildTest(ConanV2ModeTestCase): conanfile = textwrap.dedent(""" @@ -55,4 +55,7 @@ def test_deprecate_pythonpath(self): t.run("create .", assert_error=True) packages_path = t.cache.package_layout(conantool_ref).packages() self.assertIn("consumer/0.1: PYTHONPATH: ['{}".format(packages_path), t.out) - self.assertIn("ModuleNotFoundError: No module named 'tooling'", t.out) + if six.PY2: + self.assertIn("ImportError: No module named tooling", t.out) + else: + self.assertIn("ModuleNotFoundError: No module named 'tooling'", t.out) From 92edb0256424436d3314f3e0d14c41d5feff991e Mon Sep 17 00:00:00 2001 From: jgsogo Date: Wed, 25 Mar 2020 20:21:17 +0100 Subject: [PATCH 5/7] virtualenv_python stays --- conans/client/generators/virtualenv_python.py | 1 - conans/test/conan_v2/generators/__init__.py | 0 .../conan_v2/generators/test_virtualenv_python.py | 12 ------------ 3 files changed, 13 deletions(-) delete mode 100644 conans/test/conan_v2/generators/__init__.py delete mode 100644 conans/test/conan_v2/generators/test_virtualenv_python.py diff --git a/conans/client/generators/virtualenv_python.py b/conans/client/generators/virtualenv_python.py index 0d8a45950f2..f509cb31b82 100644 --- a/conans/client/generators/virtualenv_python.py +++ b/conans/client/generators/virtualenv_python.py @@ -8,7 +8,6 @@ class VirtualEnvPythonGenerator(VirtualRunEnvGenerator): venv_name = "conanenvpython" def __init__(self, conanfile): - conan_v2_behavior("'virtualenv_python' generator is deprecated") super(VirtualEnvPythonGenerator, self).__init__(conanfile) ppath = conanfile.env.get("PYTHONPATH") if ppath: diff --git a/conans/test/conan_v2/generators/__init__.py b/conans/test/conan_v2/generators/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/conans/test/conan_v2/generators/test_virtualenv_python.py b/conans/test/conan_v2/generators/test_virtualenv_python.py deleted file mode 100644 index c470dfcbd9c..00000000000 --- a/conans/test/conan_v2/generators/test_virtualenv_python.py +++ /dev/null @@ -1,12 +0,0 @@ -from conans.test.utils.conan_v2_tests import ConanV2ModeTestCase - - -class VirtualenvPythonTestCase(ConanV2ModeTestCase): - - def test_deprecate_virtualenv_python(self): - t = self.get_client() - t.run("new name/version@user/channel -b") - t.run("create . name/version@user/channel") - t.run("install name/version@user/channel -g virtualenv_python", assert_error=True) - self.assertIn("ERROR: Conan v2 incompatible: 'virtualenv_python' generator is deprecated", - t.out) From 3f1b32de9c6903d29ca0b05c83ddb46407a7e54f Mon Sep 17 00:00:00 2001 From: jgsogo Date: Wed, 25 Mar 2020 20:45:27 +0100 Subject: [PATCH 6/7] slashes... --- conans/test/conan_v2/test_pythonpath.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/conans/test/conan_v2/test_pythonpath.py b/conans/test/conan_v2/test_pythonpath.py index 1b885fe5746..c53dd5b884c 100644 --- a/conans/test/conan_v2/test_pythonpath.py +++ b/conans/test/conan_v2/test_pythonpath.py @@ -35,7 +35,9 @@ class ToolsTest(ConanFile): requires = "conantool/1.0@conan/stable" def build(self): - self.output.info("PYTHONPATH: {}".format(self.deps_env_info["conantool"].PYTHONPATH)) + pythonpath = self.deps_env_info["conantool"].PYTHONPATH + ppath = [it.replace("\\\\", "/") for it in pythonpath] + self.output.info("PYTHONPATH: {}".format(ppath)) def package_info(self): import tooling @@ -53,7 +55,7 @@ def test_deprecate_pythonpath(self): # Try to reuse it t.save({'conanfile.py': self.reuse}, clean_first=True) t.run("create .", assert_error=True) - packages_path = t.cache.package_layout(conantool_ref).packages() + packages_path = t.cache.package_layout(conantool_ref).packages().replace('\\', '/') self.assertIn("consumer/0.1: PYTHONPATH: ['{}".format(packages_path), t.out) if six.PY2: self.assertIn("ImportError: No module named tooling", t.out) From 99f225f9c1e24912ec650b0c18072033a5befbf4 Mon Sep 17 00:00:00 2001 From: jgsogo Date: Mon, 30 Mar 2020 10:14:59 +0200 Subject: [PATCH 7/7] remove unused import --- conans/client/generators/virtualenv_python.py | 1 - 1 file changed, 1 deletion(-) diff --git a/conans/client/generators/virtualenv_python.py b/conans/client/generators/virtualenv_python.py index f509cb31b82..be7b97ab8ea 100644 --- a/conans/client/generators/virtualenv_python.py +++ b/conans/client/generators/virtualenv_python.py @@ -1,5 +1,4 @@ from conans.client.generators.virtualrunenv import VirtualRunEnvGenerator -from conans.util.conan_v2_mode import conan_v2_behavior class VirtualEnvPythonGenerator(VirtualRunEnvGenerator):