From 484b836687bb3ac408bfe79f6702cae476e2f01e Mon Sep 17 00:00:00 2001 From: Bea Steers Date: Tue, 7 Nov 2023 00:56:40 -0500 Subject: [PATCH] rename the stream folders not the recording dir --- redis_record/storage/recorder/zip.py | 2 +- redis_record/util.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/redis_record/storage/recorder/zip.py b/redis_record/storage/recorder/zip.py index 58faa6f..a9171ff 100644 --- a/redis_record/storage/recorder/zip.py +++ b/redis_record/storage/recorder/zip.py @@ -39,7 +39,6 @@ def ensure_writer(self, record_name, force=False): if self.writer is None: self.writer = {} self.recording_dir = os.path.join(self.out_dir, record_name) - move_with_suffix(self.recording_dir) os.makedirs(self.recording_dir, exist_ok=True) log.info("Created Recorder: %s", self.recording_dir) return self @@ -47,6 +46,7 @@ def ensure_writer(self, record_name, force=False): def ensure_channel(self, channel): if channel not in self.writer: out_dir = os.path.join(self.recording_dir, channel) + move_with_suffix(out_dir, prefix='_') self.writer[channel] = ZipWriter(out_dir, self.max_len, self.max_size) diff --git a/redis_record/util.py b/redis_record/util.py index c1c2034..a1f9829 100644 --- a/redis_record/util.py +++ b/redis_record/util.py @@ -3,6 +3,10 @@ import datetime from .config import * +import logging +log = logging.getLogger(__name__) +log.setLevel(logging.INFO) + def maybe_encode(s: str|bytes, encoding='utf-8'): '''If the input is str, encode as utf-8 (or other).''' @@ -103,13 +107,19 @@ def get_recording_filename(name, recording_dir=RECORDING_DIR): return os.path.join(recording_dir, f'{name}.mcap') -def move_with_suffix(src): +def move_with_suffix(src, prefix=None, suffix=None, has_ext=None): if os.path.exists(src): - base, ext = os.path.splitext(src) + has_ext = os.path.isfile(src) if has_ext is None else has_ext + if has_ext: + base, ext = os.path.splitext(src) + else: + base, ext = src, '' + base = f"{prefix or ''}{base}{suffix or ''}" + i = 1 dest = f"{base}_{i}{ext}" while os.path.exists(dest): i += 1 dest = f"{base}_{i}{ext}" - print("\nMOVING", src, 'to', dest) + log.info("MOVING %s to %s", src, dest) os.rename(src, dest)