From 0fe9f4c42b7533d726806a68ad5e74ac26781b73 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Thu, 17 Jan 2019 13:45:39 +0000 Subject: [PATCH] Brute-force clean out tmp at exit --- openflexure_microscope/camera/capture.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/openflexure_microscope/camera/capture.py b/openflexure_microscope/camera/capture.py index 1a8f4b8b..7a76a74e 100644 --- a/openflexure_microscope/camera/capture.py +++ b/openflexure_microscope/camera/capture.py @@ -1,10 +1,12 @@ import uuid import io import os +import glob import datetime import copy import logging from PIL import Image +import atexit pil_formats = ['JPG', 'JPEG', 'PNG', 'TIF', 'TIFF'] thumbnail_size = (60, 60) @@ -12,6 +14,15 @@ thumbnail_size = (60, 60) BASE_CAPTURE_PATH = os.path.join(os.path.expanduser('~'), 'micrographs') TEMP_CAPTURE_PATH = os.path.join(BASE_CAPTURE_PATH, 'tmp') +def clear_tmp(): + global TEMP_CAPTURE_PATH + + logging.info("Clearing {}...".format(TEMP_CAPTURE_PATH)) + files = glob.glob('{}/*'.format(TEMP_CAPTURE_PATH)) + for f in files: + os.remove(f) + logging.debug("Removed {}".format(f)) + class CaptureObject(object): """ StreamObject used to store and process capture data, and metadata. @@ -293,3 +304,5 @@ class CaptureObject(object): self.delete_stream() if not self.keep_on_disk: self.delete_file() + +atexit.register(clear_tmp) \ No newline at end of file