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 convert verb #306

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions ros2bag/ros2bag/verb/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2018 Open Source Robotics Foundation, Inc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

year 2020

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same below

#
# 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 datetime
import os

from ros2bag.verb import VerbExtension


class ConvertVerb(VerbExtension):
"""ros2 bag convert."""

def add_arguments(self, parser, cli_name): # noqa: D102
parser.add_argument(
'bag_file', help='bag file to convert')
parser.add_argument(
'-o', '--output',
help='destination of the bagfile to create, \
defaults to a timestamped folder in the current directory')
parser.add_argument(
'-s', '--storage', default='sqlite3',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for a convert verb I would consider renaming this to --input-storage to highlight the conversion from input to output.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not completely sure a default for input makes sense here. The storage identifier might be guessed from the metadata.yaml if present, but otherwise this has to be specified.
It makes more sense for me to have it defaulted to sqlite3 on the output side.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In current master, the play and info verbs also set the input storage argument default to sqlite3, so I thought it best to keep the convention. I agree that it should be guessed, but the necessary api doesn't seem to be available in python, and IMO shouldn't be added in this PR.

I would rename it to --input-storage, remove the default and set required=True. I would also remove the short -s and not add -i to avoid confusion with -o, which is a filepath rather than storage specifier.

help='storage identifier to be used for the input bag, defaults to "sqlite3"')
parser.add_argument(
'--out-storage', default='sqlite3',
help='storage identifier to be used for the output bag, defaults to "sqlite3"')
parser.add_argument(
'-f', '--serialization-format', default='',
help='rmw serialization format in which the messages are saved, defaults to the'
' rmw currently in use')
parser.add_argument(
'--compression-mode', type=str, default='none',
choices=['none', 'file', 'message'],
help='Determine whether to compress by file or message. Default is "none".')
parser.add_argument(
'--compression-format', type=str, default='', choices=['zstd'],
help='Specify the compression format/algorithm. Default is none.')

def create_bag_directory(self, uri):
try:
os.makedirs(uri)
except OSError:
return "[ERROR] [ros2bag]: Could not create bag folder '{}'.".format(uri)

def main(self, *, args): # noqa: D102
bag_file = args.bag_file
if not os.path.exists(bag_file):
return "[ERROR] [ros2bag] bag file '{}' does not exist!".format(bag_file)

if args.compression_format and args.compression_mode == 'none':
return 'Invalid choice: Cannot specify compression format without a compression mode.'
args.compression_mode = args.compression_mode.upper()

uri = args.output or datetime.datetime.now().strftime('rosbag2_%Y_%m_%d-%H_%M_%S')
self.create_bag_directory(uri)

# NOTE(hidmic): in merged install workspaces on Windows, Python entrypoint lookups
# combined with constrained environments (as imposed by colcon test)
# may result in DLL loading failures when attempting to import a C
# extension. Therefore, do not import rosbag2_transport at the module
# level but on demand, right before first use.
from rosbag2_transport import rosbag2_transport_py

rosbag2_transport_py.convert(
in_uri=bag_file,
in_storage_id=args.storage,
out_uri=uri,
out_storage_id=args.out_storage,
serialization_format=args.serialization_format,
compression_mode=args.compression_mode,
compression_format=args.compression_format)

if os.path.isdir(uri) and not os.listdir(uri):
os.rmdir(uri)
1 change: 1 addition & 0 deletions ros2bag/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'info = ros2bag.verb.info:InfoVerb',
'play = ros2bag.verb.play:PlayVerb',
'record = ros2bag.verb.record:RecordVerb',
'convert = ros2bag.verb.convert:ConvertVerb',
],
}
)
9 changes: 9 additions & 0 deletions rosbag2_transport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ find_package(rosbag2_cpp REQUIRED)
find_package(shared_queues_vendor REQUIRED)

add_library(${PROJECT_NAME} SHARED
src/rosbag2_transport/converter.cpp
src/rosbag2_transport/player.cpp
src/rosbag2_transport/formatter.cpp
src/rosbag2_transport/generic_publisher.cpp
Expand Down Expand Up @@ -100,6 +101,14 @@ if(BUILD_TESTING)
ament_target_dependencies(test_info rosbag2_test_common)
endif()

ament_add_gmock(test_convert
test/rosbag2_transport/test_convert.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(TARGET test_convert)
target_link_libraries(test_convert rosbag2_transport)
ament_target_dependencies(test_convert test_msgs rosbag2_test_common)
endif()

ament_add_gmock(test_record_all
test/rosbag2_transport/test_record_all.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
34 changes: 34 additions & 0 deletions rosbag2_transport/include/rosbag2_transport/convert_options.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018, Bosch Software Innovations GmbH.
//
// 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.

#ifndef ROSBAG2_TRANSPORT__CONVERT_OPTIONS_HPP_
#define ROSBAG2_TRANSPORT__CONVERT_OPTIONS_HPP_

#include <chrono>
#include <string>
#include <vector>

namespace rosbag2_transport
{
struct ConvertOptions
{
public:
std::string rmw_serialization_format;
std::string compression_mode = "";
std::string compression_format = "";
};

} // namespace rosbag2_transport

#endif // ROSBAG2_TRANSPORT__CONVERT_OPTIONS_HPP_
13 changes: 13 additions & 0 deletions rosbag2_transport/include/rosbag2_transport/rosbag2_transport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <vector>

#include "rosbag2_transport/convert_options.hpp"
#include "rosbag2_transport/play_options.hpp"
#include "rosbag2_transport/record_options.hpp"
#include "rosbag2_transport/storage_options.hpp"
Expand Down Expand Up @@ -76,6 +77,18 @@ class Rosbag2Transport
ROSBAG2_TRANSPORT_PUBLIC
void play(const StorageOptions & storage_options, const PlayOptions & play_options);

/**
* Convert a bagfile from one storage to another.
*
* \param input_storage_options Options regarding the input storage (e.g. input bag file name)
* \param output_storage_options Options regarding the output storage (e.g. bag file name)
*/
ROSBAG2_TRANSPORT_PUBLIC
void convert(
const StorageOptions & input_storage_options,
const StorageOptions & output_storage_options,
const ConvertOptions & convert_options);

/**
* Print the bag info contained in the metadata yaml file.
*
Expand Down
43 changes: 43 additions & 0 deletions rosbag2_transport/src/rosbag2_transport/converter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018 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.

#include <memory>
#include <utility>
#include <string>
#include "converter.hpp"

#include "rosbag2_cpp/reader.hpp"
#include "rosbag2_cpp/writer.hpp"
#include "rosbag2_transport/logging.hpp"

namespace rosbag2_transport
{
Converter::Converter(
std::shared_ptr<rosbag2_cpp::Reader> reader,
std::shared_ptr<rosbag2_cpp::Writer> writer)
: reader_(std::move(reader)), writer_(std::move(writer)) {}

void Converter::convert(std::string serialization_format)
{
for (auto topic : reader_->get_all_topics_and_types()) {
topic.serialization_format = serialization_format;
writer_->create_topic(topic);
}
while (reader_->has_next()) {
auto msg = reader_->read_next();
writer_->write(msg);
}
}

} // namespace rosbag2_transport
56 changes: 56 additions & 0 deletions rosbag2_transport/src/rosbag2_transport/converter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2018 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.

#ifndef ROSBAG2_TRANSPORT__CONVERTER_HPP_
#define ROSBAG2_TRANSPORT__CONVERTER_HPP_

#include <future>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include "rosbag2_cpp/types.hpp"
#include "rosbag2_cpp/reader.hpp"
#include "rosbag2_cpp/writer.hpp"
#include "rosbag2_transport/storage_options.hpp"

namespace rosbag2
{
class Writer;
class Reader;
}

namespace rosbag2_transport
{

class Converter
{
public:
explicit Converter(
std::shared_ptr<rosbag2_cpp::Reader> reader,
std::shared_ptr<rosbag2_cpp::Writer> writer);

void convert(std::string serialization_format);

private:
std::shared_ptr<rosbag2_cpp::Reader> reader_;
std::shared_ptr<rosbag2_cpp::Writer> writer_;
};

} // namespace rosbag2_transport

#endif // ROSBAG2_TRANSPORT__CONVERTER_HPP_
19 changes: 19 additions & 0 deletions rosbag2_transport/src/rosbag2_transport/rosbag2_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "rosbag2_transport/logging.hpp"

#include "converter.hpp"
#include "formatter.hpp"
#include "player.hpp"
#include "recorder.hpp"
Expand Down Expand Up @@ -104,6 +105,24 @@ void Rosbag2Transport::play(
}
}

void Rosbag2Transport::convert(
const StorageOptions & input_storage_options,
const StorageOptions & output_storage_options,
const ConvertOptions & convert_options)
{
try {
reader_->open(input_storage_options, {"", convert_options.rmw_serialization_format});
writer_->open(
output_storage_options,
{convert_options.rmw_serialization_format, convert_options.rmw_serialization_format});

Converter converter(reader_, writer_);
converter.convert(convert_options.rmw_serialization_format);
} catch (std::runtime_error & e) {
ROSBAG2_TRANSPORT_LOG_ERROR("Failed to convert: %s", e.what());
}
}

void Rosbag2Transport::print_bag_info(const std::string & uri, const std::string & storage_id)
{
rosbag2_storage::BagMetadata metadata;
Expand Down
Loading