Ejemplo n.º 1
0
    def main(self, *, parser, args):
        """Entry point for CLI program."""
        mode = 'pkg file'
        if args.launch_file_name is None:
            # If only one argument passed, use single file mode.
            mode = 'single file'
        else:
            # Test if first argument is a package, and if not change to single
            # file mode, but only if the file exists.
            try:
                get_package_prefix(args.package_name)
            except PackageNotFoundError:
                if os.path.exists(args.package_name):
                    mode = 'single file'

        path = None
        launch_arguments = []
        if mode == 'single file':
            # TODO(wjwwood): figure out how to have argparse and argcomplete
            # handle this, for now, hidden feature.
            if os.path.exists(args.package_name):
                path = args.package_name
            else:
                return 'No launch file supplied'

            if args.launch_file_name is not None:
                # Since in single file mode, the "launch file" argument is
                # actually part of the launch arguments, if set.
                launch_arguments.append(args.launch_file_name)
        elif mode == 'pkg file':
            try:
                path = get_share_file_path_from_package(
                    package_name=args.package_name,
                    file_name=args.launch_file_name)
            except PackageNotFoundError as exc:
                raise RuntimeError(
                    "Package '{}' not found: {}".format(args.package_name, exc))
            except (FileNotFoundError, MultipleLaunchFilesError) as exc:
                raise RuntimeError(str(exc))
        else:
            raise RuntimeError('unexpected mode')
        launch_arguments.extend(args.launch_arguments)

        if args.show_all_subprocesses_output:
            os.environ['OVERRIDE_LAUNCH_PROCESS_OUTPUT'] = 'both'
        if args.print:
            return print_a_launch_file(launch_file_path=path)
        elif args.show_args:
            return print_arguments_of_launch_file(launch_file_path=path)
        else:
            return launch_a_launch_file(
                launch_file_path=path,
                launch_file_arguments=launch_arguments,
                noninteractive=args.noninteractive,
                args=args,
                option_extensions=self._option_extensions,
                debug=args.debug
            )
Ejemplo n.º 2
0
def launch(shutdown_pipe, package_name, launch_file_name, launch_arguments,
           debug):
    """Launch a ROS system."""
    event_loop = asyncio.new_event_loop()
    asyncio.set_event_loop(event_loop)
    # based on ros2launch/command/launch.py:main
    if package_name is None:
        single_file = True
    else:
        single_file = False
        get_package_prefix(package_name)

    path = None
    launch_arguments = []
    if single_file:
        if os.path.exists(package_name):
            path = package_name
        else:
            raise ValueError(package_name)
        if launch_file_name is not None:
            launch_arguments.append(launch_file_name)
    else:
        try:
            path = get_share_file_path_from_package(package_name=package_name,
                                                    file_name=launch_file_name)
        except PackageNotFoundError as exc:
            raise RuntimeError("Package '{}' not found: {}".format(
                package_name, exc))
        except (FileNotFoundError, MultipleLaunchFilesError) as exc:
            raise RuntimeError(str(exc))
    launch_arguments.extend(launch_arguments)
    launch_service = LaunchService(argv=launch_arguments, debug=debug)
    launch_description = LaunchDescription([
        IncludeLaunchDescription(
            AnyLaunchDescriptionSource(path),
            launch_arguments={},
        ),
    ])
    launch_service.include_launch_description(launch_description)
    finished = False

    def shutdown():
        while not finished and not shutdown_pipe.poll(1):
            pass
        launch_service.shutdown()

    t = threading.Thread(target=shutdown)
    t.start()
    launch_service.run(shutdown_when_idle=True)
    finished = True
    t.join()
    event_loop.close()
Ejemplo n.º 3
0
    def main(self, *, parser, args):
        """Entry point for CLI program."""
        if args.launch_file_name is None:
            # TODO(wjwwood): figure out how to have argparse and argcomplete
            # handle this, for now, hidden feature.
            if os.path.exists(args.package_name):
                path = args.package_name
            else:
                return 'No launch file supplied'
        else:
            try:
                path = get_share_file_path_from_package(
                    package_name=args.package_name,
                    file_name=args.launch_file_name)
            except PackageNotFoundError as exc:
                raise RuntimeError("Package '{}' not found: {}".format(
                    args.package_name, exc))
            except (FileNotFoundError, MultipleLaunchFilesError) as exc:
                raise RuntimeError(str(exc))
        try:
            if args.print:
                return print_a_python_launch_file(python_launch_file_path=path)
            else:
                return launch_a_python_launch_file(
                    python_launch_file_path=path,
                    launch_file_arguments=args.argv,
                    debug=args.debug)
        except SyntaxError:
            print("""
Notice: SyntaxError (or related errors) can occur if your launch file ('{}') is not a Python file.
""".format(path),
                  file=sys.stderr)
            raise  # raise so the user can see the traceback in useful SyntaxError's
        except ValueError as exc:
            print("""
Notice: ValueError can occur if your launch file ('{}') is a binary file and not a Python file.
""".format(path),
                  file=sys.stderr)
            raise RuntimeError('ValueError: {}'.format(str(exc)))
        except InvalidPythonLaunchFileError as exc:
            # TODO(wjwwood): refactor this after we deprecate and then remove the old launch
            print("""
Notice: Your launch file may have been designed to be used with an older version of ROS 2.
Or that the file you specified is Python code, but not a launch file.
""",
                  file=sys.stderr)
            raise RuntimeError('InvalidPythonLaunchFileError: {}'.format(
                str(exc)))
Ejemplo n.º 4
0
    def main(self, *, parser, args):
        """Entry point for CLI program."""
        mode = 'pkg file'
        if args.launch_file_name is None:
            # If only one argument passed, use single file mode.
            mode = 'single file'
        else:
            # Test if first argument is a package, and if not change to single
            # file mode, but only if the file exists.
            try:
                get_package_prefix(args.package_name)
            except PackageNotFoundError:
                if os.path.exists(args.package_name):
                    mode = 'single file'

        path = None
        launch_arguments = []
        if mode == 'single file':
            # TODO(wjwwood): figure out how to have argparse and argcomplete
            # handle this, for now, hidden feature.
            if os.path.exists(args.package_name):
                path = args.package_name
            else:
                return 'No launch file supplied'

            if args.launch_file_name is not None:
                # Since in single file mode, the "launch file" argument is
                # actually part of the launch arguments, if set.
                launch_arguments.append(args.launch_file_name)
        elif mode == 'pkg file':
            try:
                path = get_share_file_path_from_package(
                    package_name=args.package_name,
                    file_name=args.launch_file_name)
            except PackageNotFoundError as exc:
                raise RuntimeError("Package '{}' not found: {}".format(
                    args.package_name, exc))
            except (FileNotFoundError, MultipleLaunchFilesError) as exc:
                raise RuntimeError(str(exc))
        else:
            raise RuntimeError('unexpected mode')
        launch_arguments.extend(args.launch_arguments)
        try:
            if args.show_all_subprocesses_output:
                os.environ['OVERRIDE_LAUNCH_PROCESS_OUTPUT'] = 'both'
            if args.print:
                return print_a_python_launch_file(python_launch_file_path=path)
            elif args.show_args:
                return print_arguments_of_python_launch_file(
                    python_launch_file_path=path)
            else:
                return launch_a_python_launch_file(
                    python_launch_file_path=path,
                    launch_file_arguments=launch_arguments,
                    debug=args.debug)
        except SyntaxError:
            print("""
Notice: SyntaxError (or related errors) can occur if your launch file ('{}') is not a Python file.
""".format(path),
                  file=sys.stderr)
            raise  # raise so the user can see the traceback in useful SyntaxError's
        except ValueError as exc:
            print("""
Notice: ValueError can occur if your launch file ('{}') is a binary file and not a Python file.
""".format(path),
                  file=sys.stderr)
            raise RuntimeError('ValueError: {}'.format(str(exc)))
        except InvalidPythonLaunchFileError as exc:
            # TODO(wjwwood): refactor this after we deprecate and then remove the old launch
            print("""
Notice: Your launch file may have been designed to be used with an older version of ROS 2.
Or that the file you specified is Python code, but not a launch file.
""",
                  file=sys.stderr)
            raise RuntimeError('InvalidPythonLaunchFileError: {}'.format(
                str(exc)))