Skip to content

Commit

Permalink
Removing reindexing from rosbag rotate (#257)
Browse files Browse the repository at this point in the history
* removed reindexing rosbags and added .bag.active to rosbag rotate

* rotate files that contain .bag

* added year

* removed unused imports

* fixed redundancy

* fixed paths

* improved names

* changed to better name
  • Loading branch information
jeferal authored Nov 17, 2023
1 parent 03e0426 commit 48dc278
Showing 1 changed file with 10 additions and 31 deletions.
41 changes: 10 additions & 31 deletions sr_logging_common/logging/bag_rotate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

# Copyright 2019, 2022 Shadow Robot Company Ltd.
# Copyright 2019, 2022-2023 Shadow Robot Company Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
Expand All @@ -14,23 +14,11 @@
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

from os import listdir, remove, rename
from os.path import exists
import subprocess
from os import listdir, remove
from os.path import join
import rospy


def reindex_bag(active_bag):
reindexing = subprocess.run(["rosbag", "reindex", active_bag], capture_output=True, text=True, check=False)
fixed_bag = f"{active_bag[:-len('.active')]}"

if reindexing.returncode == 0:
backup_file = f"{fixed_bag}.orig.active"
if exists(backup_file):
remove(backup_file)
rename(active_bag, fixed_bag)


class SrBagRotate:
def __init__(self, path, desired_bag_number, prefix):
self._path = path
Expand All @@ -40,29 +28,20 @@ def __init__(self, path, desired_bag_number, prefix):
self.run()

def remove_old_bags(self):
bags_to_remove = self.get_suffixed_bags(".bag")
bags_to_remove.extend(self.get_suffixed_bags(".bag.orig.active"))
bags_to_remove.sort()
existing_bags = self.get_file_paths_by_key(".bag")
existing_bags.sort()

for i, bag in enumerate(bags_to_remove):
if i < len(bags_to_remove)-self._desired_bag_number - 1:
for i, bag in enumerate(existing_bags):
if i < len(existing_bags)-self._desired_bag_number - 1:
remove(bag)

def get_suffixed_bags(self, suffix):
def get_file_paths_by_key(self, key):
all_files = listdir(self._path)
all_bags = [s_file for s_file in all_files if s_file.endswith(suffix)]
suffixed_bags = []
for bag in all_bags:
actual_prefix_parts = set([t for t in bag.split("_") if t.isalpha()])
if self._desired_prefix_parts == actual_prefix_parts:
suffixed_bags.append(f"{self._path}/{bag}")
return suffixed_bags
matching_files = [join(self._path, file_name) for file_name in all_files if key in file_name]
return matching_files

def run(self):
while not rospy.is_shutdown():
active_bags = self.get_suffixed_bags(".bag.active")
for bag in active_bags:
reindex_bag(bag)
self.remove_old_bags()
rospy.sleep(1)

Expand Down

0 comments on commit 48dc278

Please sign in to comment.