Skip to content

Commit

Permalink
Implement a custom distutils command to symlink data_files (#592)
Browse files Browse the repository at this point in the history
The default implementation of install_data will always copy files into
the destination directory. When we use the 'develop' command, we
actually need to specifically tell setuptools to do something with the
data_files or they will be ignored. Instead of telling setuptools to use
install_data as-is, we can implement a custom version of install_data
that will try to symlink the files instead.
  • Loading branch information
cottsay authored Feb 3, 2024
1 parent ad0feb4 commit f7dfec7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
Empty file.
Empty file.
26 changes: 26 additions & 0 deletions colcon_core/distutils/commands/symlink_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 Open Source Robotics Foundation, Inc.
# Licensed under the Apache License, Version 2.0

from distutils.command.install_data import install_data
import os


class symlink_data(install_data): # noqa: N801
"""Like install_data, but symlink files instead of copying."""

def copy_file(self, src, dst, **kwargs): # noqa: D102
if kwargs.get('link'):
return super().copy_file(src, dst, **kwargs)

if self.force:
# os.symlink fails if the destination exists as a regular file
if os.path.isdir(dst):
target = os.path.join(dst, os.path.basename(src))
else:
target = dst
if os.path.exists(dst) and not os.path.islink(dst):
os.remove(target)

kwargs['link'] = 'sym'
src = os.path.abspath(src)
return super().copy_file(src, dst, **kwargs)
25 changes: 23 additions & 2 deletions colcon_core/task/python/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ async def build(self, *, additional_hooks=None): # noqa: D102
# prevent installation of dependencies specified in setup.py
cmd.append('--single-version-externally-managed')
self._append_install_layout(args, cmd)
if setup_py_data.get('data_files'):
cmd += ['install_data']
if rc is not None:
cmd += ['--force']
completed = await run(
self.context, cmd, cwd=args.path, env=env)
if completed.returncode:
return completed.returncode

else:
self._undo_install(pkg, args, setup_py_data, python_lib)
rc = self._undo_install(pkg, args, setup_py_data, python_lib)
if rc:
return rc
temp_symlinks = self._symlinks_in_build(args, setup_py_data)

# invoke `setup.py develop` step in build space
Expand All @@ -142,7 +148,9 @@ async def build(self, *, additional_hooks=None): # noqa: D102
'--no-deps',
]
if setup_py_data.get('data_files'):
cmd += ['install_data']
cmd += ['symlink_data']
if rc is not None:
cmd += ['--force']
completed = await run(
self.context, cmd, cwd=args.build_base, env=env)
finally:
Expand Down Expand Up @@ -189,6 +197,12 @@ async def _get_available_commands(self, path, env):
return commands

async def _undo_develop(self, pkg, args, env):
"""
Undo a previously run 'develop' command.
:returns: None if develop was not previously detected, otherwise
an integer return code where zero indicates success.
"""
# undo previous develop if .egg-info is found and develop symlinks
egg_info = os.path.join(
args.build_base, '%s.egg-info' % pkg.name.replace('-', '_'))
Expand All @@ -207,6 +221,12 @@ async def _undo_develop(self, pkg, args, env):
return completed.returncode

def _undo_install(self, pkg, args, setup_py_data, python_lib):
"""
Undo a previously run 'install' command.
:returns: None if install was not previously detected, otherwise
an integer return code where zero indicates success.
"""
# undo previous install if install.log is found
install_log = os.path.join(args.build_base, 'install.log')
if not os.path.exists(install_log):
Expand Down Expand Up @@ -246,6 +266,7 @@ def _undo_install(self, pkg, args, setup_py_data, python_lib):
with suppress(OSError):
os.rmdir(d)
os.remove(install_log)
return 0

def _symlinks_in_build(self, args, setup_py_data):
items = ['setup.py']
Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ colcon_core.verb =
test = colcon_core.verb.test:TestVerb
console_scripts =
colcon = colcon_core.command:main
distutils.commands =
symlink_data = colcon_core.distutils.commands.symlink_data:symlink_data
pytest11 =
colcon_core_warnings_stderr = colcon_core.pytest.hooks

Expand Down

0 comments on commit f7dfec7

Please sign in to comment.