|
| 1 | +#! /usr/bin/env python |
| 2 | +# unpack the feshie tar ball and extract parts |
| 3 | +from shutil import move, rmtree |
| 4 | +from os import listdir, stat,remove |
| 5 | +from os.path import exists, isdir |
| 6 | +from datetime import datetime |
| 7 | +import tarfile |
| 8 | +from subprocess import Popen |
| 9 | +import sys |
| 10 | +from sys import argv |
| 11 | +import logging |
| 12 | +# Where we unpack |
| 13 | +UNPACKED="/home/mountainsensing/www/feshie/unpacked/" |
| 14 | +WEBCAM="/home/mountainsensing/www/feshie/webcams/estate/" |
| 15 | +LOGFILES="/home/mountainsensing/www/feshie/logfiles/" |
| 16 | +ARCHIVE="/home/mountainsensing/www/feshie/archive/" |
| 17 | + |
| 18 | +def extract_file(filename): |
| 19 | + tf = tarfile.open(name=filename, mode="r") |
| 20 | + tf.extractall(UNPACKED) |
| 21 | + tf.close |
| 22 | + |
| 23 | + |
| 24 | +def process_image(filename): |
| 25 | + logging.debug("processing: %s" %filename) |
| 26 | + new_fname = WEBCAM + filename.split("/")[-1].split("_")[2]+".jpg" |
| 27 | + _move(filename, new_fname) |
| 28 | + |
| 29 | +def _move(src, dst): |
| 30 | + if not exists(dst): |
| 31 | + move(src,dst) |
| 32 | + logging.debug("file moved to %s " % dst) |
| 33 | + else: |
| 34 | + logging.warn("File %s already exists" % dst) |
| 35 | + |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + logging.basicConfig(format='%(asctime)s - %(filename)s:%(lineno)s - %(levelname)s - %(message)s', level=logging.DEBUG) |
| 39 | + if len(argv) != 2: |
| 40 | + logging.critical("Must specify filename to extract") |
| 41 | + exit(1) |
| 42 | + tarball = argv[1] |
| 43 | + try: |
| 44 | + logging.info("unpacking") |
| 45 | + extract_file(tarball) |
| 46 | + except IOError as e: |
| 47 | + logging.critical("Failed to unpack tarball") |
| 48 | + exit(3) |
| 49 | + l = listdir(UNPACKED + "/data") |
| 50 | + if l == []: |
| 51 | + logging.critical("No files unpacked") |
| 52 | + exit(4) |
| 53 | + for f in l: |
| 54 | + if "TIMING" in f: |
| 55 | + process_image(UNPACKED + "/data/" + f ) |
| 56 | + if f == "python-log" or f == "executable_output": |
| 57 | + #Stub files don't contain anything useful |
| 58 | + remove("%sdata/%s" % (UNPACKED,f)) |
| 59 | + logging.info("%s removed" % f) |
| 60 | + continue |
| 61 | + if "log" in f or "output" in f: |
| 62 | + _move("%sdata/%s" % (UNPACKED,f) ,LOGFILES + f) |
| 63 | + _move(tarball, ARCHIVE + tarball.split("/")[-1]) |
0 commit comments