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

Use deb_system scheme when available to avoid 'local' in path #504

Merged
merged 23 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/colcon
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ from colcon_core.command import LOG_LEVEL_ENVIRONMENT_VARIABLE # noqa: E402
from colcon_core.entry_point \
import EXTENSION_BLOCKLIST_ENVIRONMENT_VARIABLE # noqa: E402
from colcon_core.environment.path import PathEnvironment # noqa: E402
from colcon_core.environment.path \
import PythonScriptsPathEnvironment # noqa: E402
from colcon_core.environment.pythonpath \
import PythonPathEnvironment # noqa: E402
from colcon_core.event_handler.console_direct \
Expand Down Expand Up @@ -79,6 +81,7 @@ custom_entry_points.update({
'colcon_core.argument_parser': {},
'colcon_core.environment': {
'path': PathEnvironment,
'pythonscriptspath': PythonScriptsPathEnvironment,
'pythonpath': PythonPathEnvironment,
},
'colcon_core.environment_variable': {
Expand Down
39 changes: 39 additions & 0 deletions colcon_core/environment/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from colcon_core.environment import EnvironmentExtensionPoint
from colcon_core.environment import logger
from colcon_core.plugin_system import satisfies_version
from colcon_core.python_install_path import get_python_install_path


class PathEnvironment(EnvironmentExtensionPoint):
Expand Down Expand Up @@ -45,3 +46,41 @@ def _create_environment_hooks(
break

return hooks


class PythonScriptsPathEnvironment(EnvironmentExtensionPoint):
"""Extend the `PATH` variable to find python scripts."""

def __init__(self): # noqa: D107
super().__init__()
satisfies_version(
EnvironmentExtensionPoint.EXTENSION_POINT_VERSION, '^1.0')

def create_environment_hooks(self, prefix_path, pkg_name): # noqa: D102
hooks = self._create_environment_hooks(prefix_path, pkg_name, 'bin')
if sys.platform == 'win32':
hooks += self._create_environment_hooks(
prefix_path, pkg_name, 'Scripts', '-scripts')
cottsay marked this conversation as resolved.
Show resolved Hide resolved
return hooks

def _create_environment_hooks(
self, prefix_path, pkg_name, subdirectory, suffix=''
):
hooks = []
bin_path = get_python_install_path('scripts', {'base': prefix_path})
logger.log(1, "checking '%s'" % bin_path)
try:
names = os.listdir(str(bin_path))
except FileNotFoundError:
pass
else:
for name in names:
if not (bin_path / name).is_file():
continue
rel_bin_path = bin_path.relative_to(prefix_path)
hooks += shell.create_environment_hook(
'path' + suffix, prefix_path, pkg_name, 'PATH',
cottsay marked this conversation as resolved.
Show resolved Hide resolved
str(rel_bin_path), mode='prepend')
break

return hooks
7 changes: 2 additions & 5 deletions colcon_core/environment/pythonpath.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Copyright 2016-2018 Dirk Thomas
# Licensed under the Apache License, Version 2.0

from pathlib import Path
import sysconfig

from colcon_core import shell
from colcon_core.environment import EnvironmentExtensionPoint
from colcon_core.environment import logger
from colcon_core.plugin_system import satisfies_version
from colcon_core.python_install_path import get_python_install_path


class PythonPathEnvironment(EnvironmentExtensionPoint):
Expand All @@ -21,8 +19,7 @@ def __init__(self): # noqa: D107
def create_environment_hooks(self, prefix_path, pkg_name): # noqa: D102
hooks = []

python_path = Path(
sysconfig.get_path('purelib', vars={'base': prefix_path}))
python_path = get_python_install_path('purelib', {'base': prefix_path})
logger.log(1, "checking '%s'" % python_path)
if python_path.exists():
rel_python_path = python_path.relative_to(prefix_path)
Expand Down
25 changes: 25 additions & 0 deletions colcon_core/python_install_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

from pathlib import Path
import sysconfig


def get_python_install_path(name, vars_=None):
"""
Get Python install paths matching Colcon's preferred scheme.

See sysconfig.get_path for more info about the arguments.

:param name: Name of the path type
:param vars_: A dictionary of variables updating the values of
sysconfig.get_config_vars()
:rtype: Pathlib.Path
"""
kwargs = {}
if vars_:
kwargs['vars'] = vars_
cottsay marked this conversation as resolved.
Show resolved Hide resolved
if 'deb_system' in sysconfig.get_scheme_names():
kwargs['scheme'] = 'deb_system'

return Path(sysconfig.get_path(name, **kwargs))
4 changes: 2 additions & 2 deletions colcon_core/task/python/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import shutil
import sys
from sys import executable
import sysconfig

from colcon_core.environment import create_environment_hooks
from colcon_core.environment import create_environment_scripts
from colcon_core.logging import colcon_logger
from colcon_core.plugin_system import satisfies_version
from colcon_core.python_install_path import get_python_install_path
from colcon_core.shell import create_environment_hook
from colcon_core.shell import get_command_environment
from colcon_core.subprocess import check_output
Expand Down Expand Up @@ -296,7 +296,7 @@ def _symlinks_in_build(self, args, setup_py_data):
return temp_symlinks

def _get_python_lib(self, args):
path = sysconfig.get_path('purelib', vars={'base': args.install_base})
path = get_python_install_path('purelib', {'base': args.install_base})
return os.path.relpath(path, start=args.install_base)

def _append_install_layout(self, args, cmd):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ python_classes = !TestFailure
colcon_core.argument_parser =
colcon_core.environment =
path = colcon_core.environment.path:PathEnvironment
pythonscriptspath = colcon_core.environment.path:PythonScriptsPathEnvironment
pythonpath = colcon_core.environment.pythonpath:PythonPathEnvironment
sloretz marked this conversation as resolved.
Show resolved Hide resolved
colcon_core.environment_variable =
all_shells = colcon_core.shell:ALL_SHELLS_ENVIRONMENT_VARIABLE
Expand Down
2 changes: 2 additions & 0 deletions test/spell_check.words
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pydocstyle
pytest
pytests
pythonpath
pythonscriptspath
pythonwarnings
readouterr
readthedocs
Expand Down Expand Up @@ -119,6 +120,7 @@ todo
traceback
tryfirst
tuples
ubuntu
sloretz marked this conversation as resolved.
Show resolved Hide resolved
uninstall
unittest
unittests
Expand Down
6 changes: 3 additions & 3 deletions test/test_environment_pythonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Licensed under the Apache License, Version 2.0

from pathlib import Path
import sysconfig
from tempfile import TemporaryDirectory
from unittest.mock import patch

from colcon_core.environment.pythonpath import PythonPathEnvironment
from colcon_core.python_install_path import get_python_install_path


def test_pythonpath():
Expand All @@ -23,8 +23,8 @@ def test_pythonpath():
assert len(hooks) == 0

# Python path exists
python_path = Path(
sysconfig.get_path('purelib', vars={'base': prefix_path}))
python_path = get_python_install_path(
'purelib', {'base': prefix_path})
python_path.mkdir(parents=True)
hooks = extension.create_environment_hooks(prefix_path, 'pkg_name')
assert len(hooks) == 2