From f7dfec7efc54b8d9a4ce0ba6e43c7137b5810011 Mon Sep 17 00:00:00 2001 From: Scott K Logan Date: Fri, 2 Feb 2024 22:34:58 -0600 Subject: [PATCH] Implement a custom distutils command to symlink data_files (#592) 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. --- colcon_core/distutils/__init__.py | 0 colcon_core/distutils/commands/__init__.py | 0 .../distutils/commands/symlink_data.py | 26 +++++++++++++++++++ colcon_core/task/python/build.py | 25 ++++++++++++++++-- setup.cfg | 2 ++ 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 colcon_core/distutils/__init__.py create mode 100644 colcon_core/distutils/commands/__init__.py create mode 100644 colcon_core/distutils/commands/symlink_data.py diff --git a/colcon_core/distutils/__init__.py b/colcon_core/distutils/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/colcon_core/distutils/commands/__init__.py b/colcon_core/distutils/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/colcon_core/distutils/commands/symlink_data.py b/colcon_core/distutils/commands/symlink_data.py new file mode 100644 index 00000000..ae1c7e74 --- /dev/null +++ b/colcon_core/distutils/commands/symlink_data.py @@ -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) diff --git a/colcon_core/task/python/build.py b/colcon_core/task/python/build.py index 65d0e907..5dc89e44 100644 --- a/colcon_core/task/python/build.py +++ b/colcon_core/task/python/build.py @@ -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 @@ -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: @@ -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('-', '_')) @@ -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): @@ -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'] diff --git a/setup.cfg b/setup.cfg index 99e4626d..e935db42 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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