diff --git a/tools/fiximages.py b/tools/fiximages.py new file mode 100644 index 000000000..9c13ebc5a --- /dev/null +++ b/tools/fiximages.py @@ -0,0 +1,52 @@ +import logging + +logging.basicConfig(level=logging.DEBUG) + +from data.database import Image, ImageStorage +from app import storage + +logger = logging.getLogger(__name__) +logging.getLogger('boto').setLevel(logging.CRITICAL) + + +def migrate_checksum(image_storage): + checksum_path = storage.image_checksum_path(image_storage.uuid) + try: + checksum = storage.get_content(checksum_path) + image_storage.checksum = checksum + logger.debug('Backfilled checksum for image: %s as: %s', image_storage.uuid, checksum) + return True + except IOError: + pass + + return False + + +def fix_images(image_storage_query): + for image_storage in image_storage_query: + dirty = False + if image_storage.checksum is None: + dirty = migrate_checksum(image_storage) + + if image_storage.uploading is None: + dirty = True + mark_path = storage.image_mark_path(image_storage.uuid) + if storage.exists(mark_path): + image_storage.uploading = True + else: + image_storage.uploading = image_storage.checksum is None + + logger.debug('Set uploading flag to %s for image: %s', image_storage.uploading, + image_storage.uuid) + + if dirty: + image_storage.save() + + +storage_to_fix = (ImageStorage.select() + .where((ImageStorage.checksum >> None) & + ((ImageStorage.uploading >> None) | (ImageStorage.uploading == False)))) + +logger.debug('Fixing %s images', storage_to_fix.count()) + +fix_images(storage_to_fix)