This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
list_packages.py
114 lines (105 loc) · 3.86 KB
/
list_packages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2014 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 ament_tools.helper import argparse_existing_dir
from ament_tools.package_types import parse_package
from ament_tools.packages import find_package_paths
from ament_tools.packages import find_unique_packages
from ament_tools.topological_order import topological_order_packages
def prepare_arguments(parser):
parser.add_argument(
'basepath',
nargs='?',
type=argparse_existing_dir,
default=os.curdir,
help='Base paths to recursively crawl for packages',
)
parser.add_argument(
'--topological-order', '-t',
action='store_true',
default=False,
help='Order output based on topological order',
)
parser.add_argument(
'--names-only',
action='store_true',
default=False,
help='Print the names of the packages but not the path',
)
parser.add_argument(
'--paths-only',
action='store_true',
default=False,
help='Print the paths of the packages but not the name',
)
parser.add_argument(
'--depends-on',
help='Only show packages which depend on the given package'
)
return parser
def get_unique_depend_names(package):
return list(set(
[d.name for d in
package.build_depends +
package.buildtool_depends +
package.build_export_depends +
package.buildtool_export_depends +
package.exec_depends +
package.test_depends +
package.doc_depends]
))
def main(options):
lines = []
if not options.topological_order:
package_paths = find_package_paths(options.basepath)
for package_path in package_paths:
package = None
package_abs_path = os.path.join(options.basepath, package_path)
if options.depends_on is not None:
package = parse_package(package_abs_path)
if options.depends_on not in get_unique_depend_names(package):
continue
if options.names_only:
package = package or parse_package(package_abs_path)
lines.append(package.name)
elif options.paths_only:
lines.append(package_path)
else:
package = package or parse_package(package_abs_path)
lines.append(package.name + ' ' + package_path)
lines.sort()
else:
packages = find_unique_packages(options.basepath)
packages = topological_order_packages(packages)
for package_path, package, _ in packages:
if options.depends_on is not None:
if options.depends_on not in get_unique_depend_names(package):
continue
if options.names_only:
lines.append(package.name)
elif options.paths_only:
lines.append(package_path)
else:
lines.append(package.name + ' ' + package_path)
for line in lines:
print(line)
# meta information of the entry point
entry_point_data = dict(
verb='list_packages',
description='List names and relative paths of packages',
# Called for execution, given parsed arguments object
main=main,
# Called first to setup argparse, given argparse parser
prepare_arguments=prepare_arguments,
)