Skip to content

Commit

Permalink
+ relocate file for lost+found radiotracking files
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashoechst committed Jul 3, 2023
1 parent b28f1b4 commit 85f00e0
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions home/pi/relocate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3

import argparse
import pathlib
import logging
import datetime
import shutil

parser = argparse.ArgumentParser(description="Relocate lost+found files of tRackIT OS to their original filenames")
parser.add_argument('hostname', help="original hostname")
parser.add_argument("-o", "--outpath", help="path to copy files to", type=pathlib.Path, default=".")
parser.add_argument("-i", "--inpath", help="path to find files in", type=pathlib.Path, default=".")
parser.add_argument("-p", "--prefix", help="prefix of files to find", default="#")
parser.add_argument('-v', "--verbose", action='store_true')

HEADERS = {
"[optional arguments]": ".ini",
"Device;Time;Frequency;Duration;max (dBW);avg (dBW);std (dB);noise (dBW);snr (dB)": ".csv",
"Time;Frequency;Duration;0": "-matched.csv",
"Device;Time;State": "-state.csv",
}


args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)

logging.debug(args.__dict__)

for path in args.inpath.glob(f"{args.prefix}*"):
logging.debug("Handling file %s", path)

# get creation time
unix_ts = pathlib.Path(path).stat().st_ctime
ts = datetime.datetime.fromtimestamp(unix_ts)
logging.debug("Creation: %s", ts)

# get header
with open(path) as f:
head = f.readline()[:-1]
logging.debug("Head: %s", head)

# lookup file extension
try:
ext = HEADERS[head]
logging.debug("Extension: %s", ext)
except KeyError:
logging.warning("No extension found for header \"%s\", skipping", head)
continue

# build path
dest_name = f"{args.hostname}_{ts:%Y-%m-%dT%H%M%S}{ext}"
logging.debug("Destination: %s", dest_name)

dest_path = args.outpath / dest_name
logging.info("Copying %s to %s", path, dest_path)
shutil.copy(path, dest_path)

0 comments on commit 85f00e0

Please sign in to comment.