-
Notifications
You must be signed in to change notification settings - Fork 18
/
gz.in
executable file
·316 lines (264 loc) · 9.43 KB
/
gz.in
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env ruby
# frozen_string_literal: true
# Copyright (C) 2019 Open Source Robotics Foundation
#
# 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.
require 'optparse'
require 'yaml'
# Module to wrap included shared library module
module SharedLibInterface
# We use 'dl' for Ruby <= 1.9.x and 'fiddle' for Ruby >= 2.0.x
if RUBY_VERSION.split('.')[0] < '2'
require 'dl'
require 'dl/import'
include DL
else
require 'fiddle'
require 'fiddle/import'
include Fiddle
end
end
# List of required keys in each YAML file.
# - format: Format of the YAML file. This version has to be supported by this
# script.
# - library_version: Expected library version to work with the conf file.
# - library: Name of the shared library that will handle the commands.
# - commands: List of top level commands supported by the library, as well as
# a brief description.
required_tags =
{
'1.0.0' => %w[format library_name library_version library_path commands]
}
commands = {}
yaml_found = false
conf_dirs = '@CMAKE_INSTALL_PREFIX@/share/gz/'
conf_dirs = ENV['GZ_CONFIG_PATH'] if ENV.key?('GZ_CONFIG_PATH')
conf_dirs = conf_dirs.split('@ENV_PATH_DELIMITER@')
# Deprecated
conf_dirs.append(ENV['IGN_CONFIG_PATH']) if ENV.key?('IGN_CONFIG_PATH')
conf_dirs.each do |conf_directory|
next if Dir.glob(conf_directory + '/*.yaml').empty?
yaml_found = true
# Iterate over the list of configuration files.
Dir.glob(conf_directory + '/*.yaml') do |conf_file|
next if conf_file == [',', '..'].any?
# Read the configuration file.
yml = YAML.load_file(conf_file)
# Sanity check: Verify that the content of the file is YAML.
unless yml && yml.is_a?(Hash)
puts "Configuration warning: File [#{conf_file}] does not contain a "\
'valid YAML format. Ignoring this configuration file.'
next
end
# Sanity check: Verify that the the configuration file format is supported.
unless yml.key?('format') && required_tags.key?(yml['format'])
puts "Configuration error: File [#{conf_file}] uses format "\
"#{yml[format]} but this version is not supported."
exit(-1)
end
# Sanity check: Verify that the format of the conf file is correct.
required_tags[yml['format']].each do |tag|
# Verify that the YAML file contains the needed keys.
unless yml.key?(tag)
puts "Configuration error: File [#{conf_file}] has a missing key. "\
"Make sure that the file contains '#{tag}'."
exit(-1)
end
# Verify that the value for this key is not empty.
next if yml[tag]
puts "Configuration error: File [#{conf_file}] does not contain any "\
"value for the key '#{tag}'."
exit(-1)
end
yml['commands'].each do |cmd|
cmd.each do |key, value|
# Sanity check: Verify that we have a non-empty key and value.
unless key && value
puts "Configuration error: File [#{conf_file}] contains an empty "\
"value in the 'commands' section."
exit(-1)
end
# Sanity check: A 'help' command is not allowed.
if key.eql? 'help'
puts "Configuration error: File [#{conf_file}] contains a command "\
"named 'help' but this is a reserved name."
exit(-1)
end
# Sanity check: Verify that a command has not been used before by other
# library.
if commands.key?(key) &&
commands[key].values[0]['library_name'] != yml['library_name']
puts "Configuration error: File [#{conf_file}] contains a command "\
'already registered by '\
"#{commands[key].values[0]['library_name']}."
exit(-1)
end
# Create a hash where the command is the key and the value contains all
# the additional information (in a hash).
if !commands.key?(key)
commands[key] =
{ yml['library_version'] =>
{
'library_name' => yml['library_name'],
'library_path' => yml['library_path'],
'description' => value
} }
else
new_value = {}
new_value[yml['library_version']] =
{
'library_name' => yml['library_name'],
'library_path' => yml['library_path'],
'description' => value
}
commands[key].update(new_value)
end
end
end
end
end
# Check that we have at least one configuration file with gz commands.
unless yaml_found
puts "I cannot find any available 'gz' command:\n"\
"\t* Did you install any Gazebo library?\n"\
"\t* Did you set the GZ_CONFIG_PATH environment variable?\n"\
"\t E.g.: export GZ_CONFIG_PATH=$HOME/local/share/gz\n"
exit(-1)
end
# Use an auxiliary hash to sort the commands ordered by version. Newest
# versions go first.
sorted_commands = {}
commands.each do |cmd, versions|
sorted_commands[cmd] = versions.sort_by do |k, _v|
# Prereleases include the "~" symbol. However Gem::Version doesn't support
# it, so we remove it.
k = k.tr('~', '')
Gem::Version.new(k)
end.reverse
end
# Commands is a hash where the key is the command and the value is a list.
commands = sorted_commands
# Debug: show the list of available commands.
# puts commands
# Read the command line arguments.
usage = 'The \'gz\' command provides a command line interface to the Gazebo'\
" Tools.\n\n"\
" gz <command> [options]\n\n"\
"List of available commands:\n\n"\
" help: Print this help text.\n"
# Used to align the commands and the description.
padding_width = 15
commands.keys.sort.each do |cmd|
versions = commands[cmd]
# Calculate the padding to add between the command and the description.
padding_to_apply = padding_width - cmd.size - 1
padding = ''
padding_to_apply.times do
padding += ' '
end
usage += ' ' + cmd + ':' + padding + versions.first[1]['description'] + "\n"
end
usage += "\nOptions:\n\n"\
" --force-version <VERSION> Use a specific library version.\n"\
" --versions Show the available versions.\n"\
' --commands Show the available commands.'
usage += "\nUse 'gz help <command>' to print help for a command.\n"
OptionParser.new do |opts|
opts.banner = usage
end.parse
# The user wants to show the available commands.
if ARGV.include?('--commands')
commands.each_key do |cmd|
puts cmd
end
exit(0)
end
# Check that there is at least one command and there is a plugin that knows
# how to handle it.
if ARGV.empty? || (ARGV[0] != 'help' && !commands.key?(ARGV[0]))
puts usage
exit(-1)
end
# The 'help' command is managed here.
if ARGV[0].eql? 'help'
# Sanity check: Verify that there is a known command after help.
unless ARGV.size == 2 && commands.key?(ARGV[1])
puts usage
exit(0)
end
# Remove the help command and append --help.
ARGV.delete_at(0)
ARGV.append('--help')
end
version_index = 0
# The user wants to use a specific version of the command.
if ARGV.include?('--force-version')
i = ARGV.index('--force-version')
# Sanity check: Verify that there is an argument after --force-version
unless ARGV.size >= i + 2
puts usage
exit(-1)
end
required_version = ARGV.at(i + 1)
# Sanity check: The version value shouldn't start with '-' or '--'.
if required_version.start_with?('-', '--')
puts usage
exit(-1)
end
# Sanity check: Verify if we have the command for the specified version.
found = false
required_arr = required_version.split('.')
commands[ARGV[0]].each_with_index do |version, index|
version_arr = version.first.split('.')
next unless required_arr.zip(version_arr).map \
{ |required, current| required == current }.all?
found = true
version_index = index
break
end
unless found
puts 'Version error: I cannot find this command in '\
"version [#{required_version}]."
exit(-1)
end
ARGV.delete_at(i)
ARGV.delete_at(i)
end
# The user wants to show the available versions.
if ARGV.include?('--versions')
commands[ARGV[0]].each do |version|
puts version.first
end
exit(0)
end
if defined? RubyInstaller
# RubyInstaller does not search for dlls in PATH
# https://github.com/oneclick/rubyinstaller2/wiki/For-gem-developers#-dll-loading
ENV['RUBY_DLL_PATH'] = ENV['PATH']
RubyInstaller::Runtime.enable_dll_search_paths
end
# Start Backward before loading plugins
begin
SharedLibInterface::Importer.dlload '@backward_library_name@'
rescue SharedLibInterface::DLError
warn 'Library error: @backward_library_name@ not found. Improved backtrace'\
' generation will be disabled'
end
# Read the plugin that handles the command.
cmd_rb_library = commands[ARGV[0]].at(version_index)[1]['library_path']
# Pass the request to the specific ruby library.
require cmd_rb_library
cmd = Cmd.new
# Set the process title to something nice.
Process.setproctitle("gz #{ARGV.join(' ')}")
cmd.execute(ARGV)