Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/deactivate sh env #15605

Draft
wants to merge 4 commits into
base: develop2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion conan/tools/env/environment.py
Expand Up @@ -476,7 +476,7 @@ def save_ps1(self, file_location, generate_deactivate=True,):

def save_sh(self, file_location, generate_deactivate=True):
filepath, filename = os.path.split(file_location)
deactivate_file = os.path.join(filepath, "deactivate_{}".format(filename))
deactivate_file = os.path.join("$script_folder", "deactivate_{}".format(filename))
deactivate = textwrap.dedent("""\
echo "echo Restoring environment" > "{deactivate_file}"
for v in {vars}
Expand Down
22 changes: 18 additions & 4 deletions conans/client/generators/__init__.py
@@ -1,5 +1,6 @@
import inspect
import os
import re
import traceback
import importlib

Expand Down Expand Up @@ -201,11 +202,24 @@ def relativize_generated_file(content, conanfile, placeholder):
abs_base_path = conanfile.folders._base_generators
if not abs_base_path or not os.path.isabs(abs_base_path):
return content
abs_base_path = os.path.join(abs_base_path, "") # For the trailing / to dissambiguate matches
#abs_base_path = os.path.join(abs_base_path, "") # For the trailing / to dissambiguate matches
generators_folder = conanfile.generators_folder
rel_path = os.path.relpath(abs_base_path, generators_folder)
new_path = placeholder if rel_path == "." else os.path.join(placeholder, rel_path)
new_path = os.path.join(new_path, "") # For the trailing / to dissambiguate matches
content = content.replace(abs_base_path, new_path)
content = content.replace(abs_base_path.replace("\\", "/"), new_path.replace("\\", "/"))
#new_path = os.path.join(new_path, "") # For the trailing / to dissambiguate matches

print("OLD", abs_base_path)
print("NEW", new_path)
def replace(text, old, new, sep):
regex = rf"({re.escape(old)}[^?%*:;|<>\"\n]*{re.escape(sep)}?)"

def repl(x):
p = x.group()
return p.replace(old, new, 1) if os.path.exists(p) else p
return re.sub(regex, repl, text, 0, re.MULTILINE)

content = replace(content, abs_base_path, new_path, os.sep)
if os.sep == "\\":
content = replace(content, abs_base_path.replace("\\", "/"),
new_path.replace("\\", "/"), "/")
return content
11 changes: 11 additions & 0 deletions conans/test/integration/environment/test_env.py
Expand Up @@ -841,3 +841,14 @@ def test(self):
c.run("create tool --build-require -s:b build_type=Release -s:h build_type=Debug")
assert "tool/0.1 (test package): Building TEST_PACKAGE IN Debug!!" in c.out
assert "MYLIBVAR=MYLIBVALUE:Release" in c.out


def test_deactivate_relocatable_substitute():
c = TestClient()
# this cannot be tested in CI, because permissions over root folder
# c.current_folder = "/build"
c.save({"conanfile.py": GenConanfile("pkg", "0.1")})
c.run("install . -s os=Linux -s:b os=Linux")
conanbuild = c.load("conanbuildenv.sh")
result = os.path.join("$script_folder", "deactivate_conanbuildenv.sh")
assert fr'"{result}"' in conanbuild
25 changes: 25 additions & 0 deletions conans/test/unittests/client/test_relativize.py
@@ -0,0 +1,25 @@
import os
import textwrap

from conans.client.generators import relativize_generated_file
from conans.test.utils.mocks import ConanFileMock


def test_relativize(monkeypatch):
conanfile = ConanFileMock()
conanfile.folders.set_base_generators("/home/frodo")
conanfile.folders.generators = "generators"
content = textwrap.dedent("""\
export PATH=/path/to/home/frodo:/home/frodo:/home/frodo/path/other
export OTHER=/home/frodo/
export PYTHON="/home/frodo/something":"/home/frodo/path with space/other"
""")
expected = textwrap.dedent("""\
export PATH=/path/to/home/frodo:$$$/..:$$$/../path/other
export OTHER=$$$/../
export PYTHON="$$$/../something":"$$$/../path with space/other"
""")
monkeypatch.setattr(os.path, "exists", lambda _: True)
result = relativize_generated_file(content, conanfile, "$$$")
print(result)
assert result == expected