Esempio n. 1
0
    async def build(self):  # noqa: D102
        args = self.context.args
        logger.info("Building ROS package in '{args.path}' with build type "
                    "'ament_python'".format_map(locals()))

        # reuse Python build task with additional logic
        extension = PythonBuildTask()
        extension.set_context(context=self.context)

        # additional hooks
        additional_hooks = create_environment_hook('ament_prefix_path',
                                                   Path(args.install_base),
                                                   self.context.pkg.name,
                                                   'AMENT_PREFIX_PATH',
                                                   '',
                                                   mode='prepend')
        # create package marker in ament resource index
        create_file(
            args,
            'share/ament_index/resource_index/packages/{self.context.pkg.name}'
            .format_map(locals()))
        # copy / symlink package manifest
        install(
            args, 'package.xml',
            'share/{self.context.pkg.name}/package.xml'.format_map(locals()))

        return await extension.build(additional_hooks=additional_hooks)
Esempio n. 2
0
def test_create_file():
    with TemporaryDirectory(prefix='test_colcon_') as base_path:
        args = Mock()
        args.install_base = base_path

        create_file(args, 'file.txt')
        path = Path(base_path) / 'file.txt'
        assert path.is_file()
        assert path.read_text() == ''

        create_file(args, 'path/file.txt', content='content')
        path = Path(base_path) / 'path' / 'file.txt'
        assert path.is_file()
        assert path.read_text() == 'content'
Esempio n. 3
0
    async def _install(self, dub: DubPackage) -> Optional[int]:
        self.progress('install')
        args = self.context.args  # BuildPackageArguments

        # We now install to <install_base>/lib/dub/<package>
        src_dir = Path(dub.path)

        # Ignore git files
        files = [
            f for f in os.listdir(src_dir)
            if not f.startswith('.git')
            and f != '.' and f != '..'
        ]

        # install DUB
        for f in files:
            dst_f = Path(f).parts[-1]
            _copy_path(
                args, f,
                'lib/dub/{self.context.pkg.name}/{dst_f}'.format_map(locals()))

        # install builds
        for c in dub.configurations:
            if not c.is_executable():
                continue
            obj_path = c.object_path()
            _copy_path(
                args, obj_path,
                'lib/{self.context.pkg.name}/{obj_path}'.format_map(locals()))

        # install files
        for dst, files in dub.install_files.items():
            for f in files:
                dst_f = Path(f).parts[-1]
                _copy_path(
                    args, f,
                    '{dst}/{dst_f}'.format_map(locals()))

        # create files
        for dst, files in dub.create_files.items():
            for f in files:
                create_file(args, '{dst}/{f}'.format_map(locals()))
Esempio n. 4
0
    async def build(self):  # noqa: D102
        args = self.context.args

        # get build type of package from manifest
        pkg, build_type = get_package_with_build_type(args.path)
        logger.info("Building ROS package in '{args.path}' with build type "
                    "'{build_type}'".format_map(locals()))

        # choose the task extension and additional hooks and arguments
        additional_hooks = []
        if build_type == 'ament_cmake':
            extension = CmakeBuildTask()
            additional_hooks += [
                'share/{pkg.name}/local_setup.bash'.format_map(locals()),
                'share/{pkg.name}/local_setup.bat'.format_map(locals()),
                'share/{pkg.name}/local_setup.ps1'.format_map(locals()),
                'share/{pkg.name}/local_setup.sh'.format_map(locals()),
            ]
            if args.test_result_base:
                if args.cmake_args is None:
                    args.cmake_args = []
                # ament_cmake appends the project name itself
                args.cmake_args.append('-DAMENT_TEST_RESULTS_DIR=' +
                                       os.path.dirname(args.test_result_base))
            if args.symlink_install:
                if args.cmake_args is None:
                    args.cmake_args = []
                args.cmake_args.append('-DAMENT_CMAKE_SYMLINK_INSTALL=1')
            if args.ament_cmake_args:
                if args.cmake_args is None:
                    args.cmake_args = []
                args.cmake_args += args.ament_cmake_args
            # extend CMAKE_PREFIX_PATH with AMENT_PREFIX_PATH
            ament_prefix_path = os.environ.get('AMENT_PREFIX_PATH')
            if ament_prefix_path:
                ament_prefix_path = ament_prefix_path.replace(os.pathsep, ';')
                if args.cmake_args is None:
                    args.cmake_args = []
                # check if the CMAKE_PREFIX_PATH is explicitly set
                prefix = '-DCMAKE_PREFIX_PATH='
                for i, value in reversed(list(enumerate(args.cmake_args))):
                    if not value.startswith(prefix):
                        continue
                    # extend the last existing entry
                    existing = value[len(prefix):]
                    if existing:
                        existing = ';' + existing
                    args.cmake_args[i] = \
                        '-DCMAKE_PREFIX_PATH={ament_prefix_path}{existing}' \
                        .format_map(locals())
                    break
                else:
                    # otherwise extend the environment variable
                    existing = os.environ.get('CMAKE_PREFIX_PATH', '')
                    if existing:
                        existing = ';' + existing.replace(os.pathsep, ';')
                    args.cmake_args.append(
                        '-DCMAKE_PREFIX_PATH={ament_prefix_path}{existing}'.
                        format_map(locals()))

        elif build_type == 'ament_python':
            extension = PythonBuildTask()
            additional_hooks += create_environment_hook('ament_prefix_path',
                                                        Path(
                                                            args.install_base),
                                                        pkg.name,
                                                        'AMENT_PREFIX_PATH',
                                                        '',
                                                        mode='prepend')
            # create package marker in ament resource index
            create_file(
                args, 'share/ament_index/resource_index/packages/{pkg.name}'.
                format_map(locals()))
            # copy / symlink package manifest
            install(args, 'package.xml',
                    'share/{pkg.name}/package.xml'.format_map(locals()))

        elif build_type == 'catkin':
            extension = CmakeBuildTask()
            if args.cmake_args is None:
                args.cmake_args = []
            args.cmake_args += ['-DCATKIN_INSTALL_INTO_PREFIX_ROOT=0']
            if args.test_result_base:
                # catkin appends the project name itself
                args.cmake_args.append('-DCATKIN_TEST_RESULTS_DIR=' +
                                       os.path.dirname(args.test_result_base))
            if args.catkin_cmake_args:
                args.cmake_args += args.catkin_cmake_args
            additional_hooks += create_environment_hook('ros_package_path',
                                                        Path(
                                                            args.install_base),
                                                        pkg.name,
                                                        'ROS_PACKAGE_PATH',
                                                        'share',
                                                        mode='prepend')

        elif build_type == 'cmake':
            extension = CmakeBuildTask()

        elif build_type == 'cargo':
            extension = CargoBuildTask()
            additional_hooks += create_environment_hook('ament_prefix_path',
                                                        Path(
                                                            args.install_base),
                                                        pkg.name,
                                                        'AMENT_PREFIX_PATH',
                                                        '',
                                                        mode='prepend')
            # create package marker in ament resource index
            create_file(
                args, 'share/ament_index/resource_index/packages/{pkg.name}'.
                format_map(locals()))
            # copy / symlink package manifest
            install(args, 'package.xml',
                    'share/{pkg.name}/package.xml'.format_map(locals()))

        else:
            assert False, 'Unknown build type: ' + build_type

        extension.set_context(context=self.context)
        if build_type != 'catkin':
            return await extension.build(additional_hooks=additional_hooks)
        else:
            # for catkin packages add additional hooks after the package has
            # been built and installed depending on the installed files
            rc = await extension.build(skip_hook_creation=True)

            # add Python 2 specific path to PYTHONPATH if it exists
            if os.environ.get('ROS_PYTHON_VERSION', '2') == '2':
                for subdirectory in ('dist-packages', 'site-packages'):
                    python_path = Path(args.install_base) / \
                        'lib' / 'python2.7' / subdirectory
                    logger.log(1, "checking '%s'" % python_path)
                    if python_path.exists():
                        rel_python_path = python_path.relative_to(
                            args.install_base)
                        additional_hooks += create_environment_hook(
                            'python2path',
                            Path(args.install_base),
                            pkg.name,
                            'PYTHONPATH',
                            str(rel_python_path),
                            mode='prepend')
            create_environment_scripts(self.context.pkg,
                                       args,
                                       additional_hooks=additional_hooks)

            # ensure that the install base has the marker file
            # identifying it as a catkin workspace
            marker = Path(args.install_base) / '.catkin'
            marker.touch(exist_ok=True)

            return rc
Esempio n. 5
0
    async def build(self):  # noqa: D102
        args = self.context.args
        logger.info(
            "Building ROS package in '{args.path}' with build type "
            "'ament_python'".format_map(locals()))

        # reuse Python build task with additional logic
        extension = PythonBuildTask()
        extension.set_context(context=self.context)

        # additional hooks
        additional_hooks = create_environment_hook(
            'ament_prefix_path', Path(args.install_base),
            self.context.pkg.name, 'AMENT_PREFIX_PATH', '', mode='prepend')

        # get options from the Python manifest
        try:
            env = await get_command_environment(
                'setup_py', args.build_base, self.context.dependencies)
        except RuntimeError as e:
            logger.error(str(e))
            return 1
        setup_py_data = get_setup_data(self.context.pkg, env)

        # check if the package index and manifest are being installed
        data_files = get_data_files_mapping(
            setup_py_data.get('data_files', []))
        installs_package_index = False
        installs_package_manifest = False

        # check if package index and manifest are being installed
        for source, destination in data_files.items():
            if sys.platform == 'win32':
                destination = Path(destination).as_posix()
            # work around data files incorrectly defined as not relative
            if os.path.isabs(source):
                source = os.path.relpath(source, args.path)
            if (
                destination == 'share/ament_index/resource_index/packages/' +
                self.context.pkg.name
            ):
                installs_package_index = True
            elif (
                source == 'package.xml' and
                destination == 'share/{self.context.pkg.name}/package.xml'
                .format_map(locals())
            ):
                installs_package_manifest = True

        # warn about missing explicit installation
        # for now implicitly install the marker and the manifest
        if not installs_package_index:
            # TODO remove magic helper in the future
            logger.warn(
                "Package '{self.context.pkg.name}' doesn't explicitly install "
                'a marker in the package index (colcon-ros currently does it '
                'implicitly but that fallback will be removed in the future)'
                .format_map(locals()))
            # create package marker in ament resource index
            create_file(
                args,
                'share/ament_index/resource_index/packages/'
                '{self.context.pkg.name}'.format_map(locals()))
        if not installs_package_manifest:
            # TODO remove magic helper in the future
            logger.warn(
                "Package '{self.context.pkg.name}' doesn't explicitly install "
                "the 'package.xml' file (colcon-ros currently does it "
                'implicitly but that fallback will be removed in the future)'
                .format_map(locals()))
            # copy / symlink package manifest
            install(
                args, 'package.xml',
                'share/{self.context.pkg.name}/package.xml'
                .format_map(locals()))

        return await extension.build(additional_hooks=additional_hooks)