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

Display bag summary using ros2 bag info (reduced size) #45

Merged
merged 14 commits into from
Oct 30, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ros2bag/ros2bag/verb/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys

from ros2bag.verb import VerbExtension

from rosbag2_transport import rosbag2_transport_py


class InfoVerb(VerbExtension):
"""ros2 bag info."""

def add_arguments(self, parser, cli_name): # noqa: D102
arg = parser.add_argument(
parser.add_argument(
'bag_file', help='bag file to introspect')

def main(self, *, args): # noqa: D102
bag_file = args.bag_file
print('calling ros2 bag info on', bag_file)
if not os.path.exists(bag_file):
return "Error: bag file '{}' does not exist!".format(bag_file)

rosbag2_transport_py.info(uri=bag_file)
14 changes: 10 additions & 4 deletions ros2bag/ros2bag/verb/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def add_arguments(self, parser, cli_name): # noqa: D102
'-s', '--storage', default='sqlite3',
help='storage identifier to be used, defaults to "sqlite3"')

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

def main(self, *, args): # noqa: D102
if args.all and args.topics:
return 'Invalid choice: Can not specify topics and -a at the same time.'
Expand All @@ -52,14 +58,14 @@ def main(self, *, args): # noqa: D102
if os.path.isdir(uri):
return "Error: Output folder '{}' already exists.".format(uri)

try:
os.makedirs(uri)
except:
return "Error: Could not create bag folder '{}'.".format(uri)
self.create_bag_directory(uri)

if args.all:
rosbag2_transport_py.record(uri=uri, storage_id=args.storage, all=True)
elif args.topics and len(args.topics) > 0:
rosbag2_transport_py.record(uri=uri, storage_id=args.storage, topics=args.topics)
else:
self._subparser.print_help()

if os.path.isdir(uri) and not os.listdir(uri):
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems to be there is a design flaw here. I believe there should be a function (exposed) which can check whether the specified plugin is correct. That is, before calling straight up record, I think we should be able to initialize and setup the recorder at first and then call record. Having the recorder setup first allows for error checking.

Then second, the code for removing the folder only works right now because record has to be cancelled by ctrl-c (because of the spin inside). I believe this should be handled by a signint handler, to cancel the record deliberately.

Copy link
Contributor

@anhosi anhosi Oct 24, 2018

Choose a reason for hiding this comment

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

IMHO the "design flaw" is that the directory creation happens already in the python layer before instead of having the storage taking care of that. The reason for this is that cross platform file handling is hard to do in C++ but much easier in Python.
Creating a separate initialize method would only solidify this workaround and would still leave some scenarios uncovered that cannot be detected beforehand.

Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding the second point I agree that we will need to do our own signal handling for that and then we also need to change this "failure detection". I would prefer to do this with another pull request.

os.rmdir(uri)
24 changes: 17 additions & 7 deletions rosbag2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ find_package(rosbag2_storage REQUIRED)
find_package(rosidl_generator_cpp REQUIRED)

add_library(${PROJECT_NAME} SHARED
src/rosbag2/info.cpp
src/rosbag2/sequential_reader.cpp
src/rosbag2/serialization_format_converter_factory.cpp
src/rosbag2/typesupport_helpers.cpp
Expand Down Expand Up @@ -71,13 +72,6 @@ if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()

ament_add_gmock(test_typesupport_helpers
test/rosbag2/test_typesupport_helpers.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(TARGET test_typesupport_helpers)
target_link_libraries(test_typesupport_helpers rosbag2)
endif()

add_library(
converter_test_plugin
SHARED
Expand All @@ -96,6 +90,22 @@ if(BUILD_TESTING)
target_include_directories(test_converter_factory PRIVATE include)
target_link_libraries(test_converter_factory rosbag2)
endif()

ament_add_gmock(test_typesupport_helpers
test/rosbag2/test_typesupport_helpers.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(TARGET test_typesupport_helpers)
target_link_libraries(test_typesupport_helpers rosbag2)
endif()

ament_add_gmock(test_info
test/rosbag2/test_info.cpp
test/rosbag2/mock_metadata_io.hpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(TARGET test_info)
target_link_libraries(test_info rosbag2)
ament_target_dependencies(test_info rosbag2_test_common)
endif()
endif()

ament_package()
37 changes: 37 additions & 0 deletions rosbag2/include/rosbag2/info.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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__INFO_HPP_
#define ROSBAG2__INFO_HPP_

#include <string>

#include "rosbag2/types.hpp"
#include "visibility_control.hpp"

namespace rosbag2
{

class ROSBAG2_PUBLIC Info
{
public:
virtual ~Info() = default;

virtual rosbag2::BagMetadata read_metadata(
const std::string & uri, const std::string & storage_id = "");
};

} // namespace rosbag2

#endif // ROSBAG2__INFO_HPP_
3 changes: 3 additions & 0 deletions rosbag2/include/rosbag2/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
#ifndef ROSBAG2__TYPES_HPP_
#define ROSBAG2__TYPES_HPP_

#include "rosbag2_storage/bag_metadata.hpp"
#include "rosbag2_storage/serialized_bag_message.hpp"
#include "rosbag2_storage/topic_with_type.hpp"

namespace rosbag2
{
using BagMetadata = rosbag2_storage::BagMetadata;
using SerializedBagMessage = rosbag2_storage::SerializedBagMessage;
using TopicMetadata = rosbag2_storage::TopicMetadata;
using TopicWithType = rosbag2_storage::TopicWithType;
} // namespace rosbag2

Expand Down
1 change: 1 addition & 0 deletions rosbag2/include/rosbag2/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ROSBAG2_PUBLIC Writer
virtual void write(std::shared_ptr<SerializedBagMessage> message);

private:
std::string uri_;
rosbag2_storage::StorageFactory factory_;
std::shared_ptr<rosbag2_storage::storage_interfaces::ReadWriteInterface> storage_;
};
Expand Down
1 change: 1 addition & 0 deletions rosbag2/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>test_msgs</test_depend>
<test_depend>rosbag2_test_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
Expand Down
48 changes: 48 additions & 0 deletions rosbag2/src/rosbag2/info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.

#include "rosbag2/info.hpp"

#include <memory>
#include <string>

#include "rosbag2_storage/filesystem_helper.hpp"
#include "rosbag2_storage/metadata_io.hpp"
#include "rosbag2_storage/storage_factory.hpp"

namespace rosbag2
{

rosbag2::BagMetadata Info::read_metadata(const std::string & uri, const std::string & storage_id)
{
rosbag2_storage::MetadataIo metadata_io;
if (metadata_io.metadata_file_exists(uri)) {
return metadata_io.read_metadata(uri);
}
if (!storage_id.empty()) {
rosbag2_storage::StorageFactory factory;
std::shared_ptr<rosbag2_storage::storage_interfaces::ReadOnlyInterface> storage;
storage = factory.open_read_only(uri, storage_id);
if (!storage) {
throw std::runtime_error("The metadata.yaml file does not exist and the bag could not be "
"opened.");
}
auto bag_metadata = storage->get_metadata();
bag_metadata.bag_size = rosbag2_storage::FilesystemHelper::calculate_directory_size(uri);
return bag_metadata;
}
throw std::runtime_error("The metadata.yaml file does not exist. Please specify a the "
"storage id of the bagfile to query it directly");
}
} // namespace rosbag2
9 changes: 9 additions & 0 deletions rosbag2/src/rosbag2/writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@

#include <memory>
#include <string>
#include <utility>

#include "rosbag2_storage/metadata_io.hpp"
#include "rosbag2/info.hpp"
#include "rosbag2/storage_options.hpp"

namespace rosbag2
{

Writer::~Writer()
{
if (!uri_.empty()) {
rosbag2_storage::MetadataIo metadata_io;
metadata_io.write_metadata(uri_, storage_->get_metadata());
}

storage_.reset(); // Necessary to ensure that the writer is destroyed before the factory
}

Expand All @@ -33,6 +41,7 @@ void Writer::open(const StorageOptions & options)
if (!storage_) {
throw std::runtime_error("No storage could be initialized. Abort");
}
uri_ = options.uri;
}

void Writer::create_topic(const TopicWithType & topic_with_type)
Expand Down
34 changes: 34 additions & 0 deletions rosbag2/test/rosbag2/mock_metadata_io.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__MOCK_METADATA_IO_HPP_
#define ROSBAG2__MOCK_METADATA_IO_HPP_

#include <gmock/gmock.h>

#include <memory>
#include <string>
#include <vector>

#include "rosbag2_storage/bag_metadata.hpp"
#include "rosbag2_storage/metadata_io.hpp"

class MockMetadataIo : public rosbag2_storage::MetadataIo
{
public:
MOCK_METHOD2(write_metadata, void(const std::string &, rosbag2_storage::BagMetadata));
MOCK_METHOD1(read_metadata, rosbag2_storage::BagMetadata(const std::string &));
};

#endif // ROSBAG2__MOCK_METADATA_IO_HPP_
63 changes: 63 additions & 0 deletions rosbag2/test/rosbag2/mock_storage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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__MOCK_STORAGE_HPP_
#define ROSBAG2__MOCK_STORAGE_HPP_

#include <memory>
#include <string>
#include <vector>

#include "rosbag2_storage/bag_metadata.hpp"
#include "rosbag2_storage/serialized_bag_message.hpp"
#include "rosbag2_storage/topic_with_type.hpp"
#include "rosbag2_storage/storage_interfaces/read_write_interface.hpp"

class MockStorage : public rosbag2_storage::storage_interfaces::ReadWriteInterface
{
public:
~MockStorage() override = default;

void open(const std::string & uri, rosbag2_storage::storage_interfaces::IOFlag flag) override
{
(void) uri;
(void) flag;
}

void create_topic(const rosbag2_storage::TopicWithType & topic) override
{
(void) topic;
}

bool has_next() override {return true;}

std::shared_ptr<rosbag2_storage::SerializedBagMessage> read_next() override {return nullptr;}

void write(std::shared_ptr<const rosbag2_storage::SerializedBagMessage> msg) override
{
(void) msg;
}

std::vector<rosbag2_storage::TopicWithType> get_all_topics_and_types() override
{
return std::vector<rosbag2_storage::TopicWithType>();
}

rosbag2_storage::BagMetadata get_metadata() override
{
return rosbag2_storage::BagMetadata();
}
};

#endif // ROSBAG2__MOCK_STORAGE_HPP_
Loading