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

add ros2 pkg executables #23

Merged
merged 2 commits into from Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions ros2pkg/ros2pkg/api/__init__.py
Expand Up @@ -12,11 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from ament_index_python import get_package_prefix
from ament_index_python import get_packages_with_prefixes
from ament_index_python import PackageNotFoundError


class PackageNotFound(Exception):

def __init__(self, package_name):
self.package_name = package_name


def get_package_names():
return get_packages_with_prefixes().keys()

Expand All @@ -29,6 +37,24 @@ def get_prefix_path(package_name):
return prefix_path


def get_executable_paths(*, package_name):
prefix_path = get_prefix_path(package_name)
if prefix_path is None:
raise PackageNotFound(package_name)
base_path = os.path.join(prefix_path, 'lib', package_name)
executable_paths = []
for dirpath, dirnames, filenames in os.walk(base_path):
# ignore folder starting with .
dirnames[:] = [d for d in dirnames if d[0] not in ['.']]
dirnames.sort()
# select executable files
for filename in sorted(filenames):
path = os.path.join(dirpath, filename)
if os.access(path, os.X_OK):
executable_paths.append(path)
return executable_paths


def package_name_completer(**kwargs):
"""Callable returning a list of packages names."""
return get_package_names()
57 changes: 57 additions & 0 deletions ros2pkg/ros2pkg/verb/executables.py
@@ -0,0 +1,57 @@
# Copyright 2017 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from ros2pkg.api import get_executable_paths
from ros2pkg.api import get_package_names
from ros2pkg.api import package_name_completer
from ros2pkg.api import PackageNotFound
from ros2pkg.verb import VerbExtension


class ExecutablesVerb(VerbExtension):
"""Output a list of package specific executables."""

def add_arguments(self, parser, cli_name):
arg = parser.add_argument(
'package_name', nargs='?',
help='The package name')
arg.completer = package_name_completer
parser.add_argument(
'--full-path',
action='store_true',
default=False,
help='Show full path of each executable')

def main(self, *, args):
if args.package_name is None:
package_names = get_package_names()
else:
package_names = [args.package_name]

for package_name in sorted(package_names):
try:
paths = get_executable_paths(package_name=package_name)
except PackageNotFound:
if args.package_name is None:
assert False, 'This should never happen'
raise RuntimeError(
"Package '{args.package_name}' not found"
.format_map(locals()))
for path in sorted(paths):
if args.full_path:
print(path)
else:
print(package_name, os.path.basename(path))
1 change: 1 addition & 0 deletions ros2pkg/setup.py
Expand Up @@ -32,6 +32,7 @@
'ros2pkg.verb = ros2pkg.verb:VerbExtension',
],
'ros2pkg.verb': [
'executables = ros2pkg.verb.executables:ExecutablesVerb',
'list = ros2pkg.verb.list:ListVerb',
'prefix = ros2pkg.verb.prefix:PrefixVerb',
],
Expand Down
27 changes: 2 additions & 25 deletions ros2run/ros2run/api/__init__.py
Expand Up @@ -16,7 +16,8 @@
import subprocess
import sys

from ros2pkg.api import get_prefix_path
from ros2pkg.api import get_executable_paths
from ros2pkg.api import PackageNotFound


class MultipleExecutables(Exception):
Expand All @@ -25,30 +26,6 @@ def __init__(self, paths):
self.paths = paths


class PackageNotFound(Exception):

def __init__(self, package_name):
self.package_name = package_name


def get_executable_paths(*, package_name):
prefix_path = get_prefix_path(package_name)
if prefix_path is None:
raise PackageNotFound(package_name)
base_path = os.path.join(prefix_path, 'lib', package_name)
executable_paths = []
for dirpath, dirnames, filenames in os.walk(base_path):
# ignore folder starting with .
dirnames[:] = [d for d in dirnames if d[0] not in ['.']]
dirnames.sort()
# select executable files
for filename in sorted(filenames):
path = os.path.join(dirpath, filename)
if os.access(path, os.X_OK):
executable_paths.append(path)
return executable_paths


def get_executable_path(*, package_name, executable_name):
paths = get_executable_paths(package_name=package_name)
paths2base = {}
Expand Down
2 changes: 1 addition & 1 deletion ros2run/ros2run/command/run.py
Expand Up @@ -14,10 +14,10 @@

from ros2cli.command import CommandExtension
from ros2pkg.api import package_name_completer
from ros2pkg.api import PackageNotFound
from ros2run.api import ExecutableNameCompleter
from ros2run.api import get_executable_path
from ros2run.api import MultipleExecutables
from ros2run.api import PackageNotFound
from ros2run.api import run_executable


Expand Down