<filename>ingest-thumbnails/pre_ingest_thumbnails.py<gh_stars>1-10
from os import listdir, symlink
import sys
from pathlib import Path
from os.path import isfile, isdir, join
import logging
import traceback


# Run this python file as a script in command line
# python3 -m pre_ingest_thumbnails <uploads_dir_abs_path> <assets_dir_abs_path>
if __name__ == "__main__":
    uploads_dir_abs_path = sys.argv[1]

    try:
         assets_dir_abs_path = sys.argv[2]
    except IndexError as e:
        msg = "Missing assets directory absolute path argument"
        logger.exception(msg)
        sys.exit(msg)

    try:
        all_entity_uuids = [f for f in listdir(uploads_dir_abs_path) if isdir(join(uploads_dir_abs_path, f))]

        # Loop through all the entity uuid dirs
        for entity_uuid in all_entity_uuids:
            entity_uuid_dir_abs_path = join(uploads_dir_abs_path, entity_uuid)
            all_file_uuids = [f for f in listdir(entity_uuid_dir_abs_path) if isdir(join(entity_uuid_dir_abs_path, f))]
            
            for file_uuid in all_file_uuids:
                file_uuid_dir_abs_path = join(uploads_dir_abs_path, entity_uuid, file_uuid)
                all_filenames = [f for f in listdir(file_uuid_dir_abs_path) if isfile(join(file_uuid_dir_abs_path, f))]

                for filename in all_filenames:
                    # Link the uploaded file uuid dir to assets
                    # /hive/hubmap/hm_uploads/<entity_uuid>/<file_uuid>/<filename> (for PROD)
                    source_file_path = join(file_uuid_dir_abs_path, filename)
                    # /hive/hubmap/assets/<file_uuid>/<filename> (for PROD)
                    target_file_dir = join(assets_dir_abs_path, file_uuid)
                    target_file_path = join(target_file_dir, filename)

                    # Create the file_uuid directory under assets dir
                    # and a symbolic link to the uploaded file
                    try:
                        Path(target_file_dir).mkdir(parents=True, exist_ok=True)
                        symlink(source_file_path, target_file_path)
                    except Exception as e:
                        logger.exception(f"Failed to create the symbolic link from {file_uuid_dir_abs_path} to {target_file_dir}")
    except Exception as e:                                                                                                            
        exc_type, exc_value, exc_traceback = sys.exc_info()
        eMsg = str(e)                                                                                                                     
        print("ERROR Occurred: " + eMsg)
        traceback.print_tb(exc_traceback)
        exit(1)
<filename>ingest-thumbnails/pre_ingest_thumbnails.py<gh_stars>1-10
from os import listdir, symlink
import sys
from pathlib import Path
from os.path import isfile, isdir, join
import logging
import traceback


# Run this python file as a script in command line
# python3 -m pre_ingest_thumbnails <uploads_dir_abs_path> <assets_dir_abs_path>
if __name__ == "__main__":
    uploads_dir_abs_path = sys.argv[1]

    try:
         assets_dir_abs_path = sys.argv[2]
    except IndexError as e:
        msg = "Missing assets directory absolute path argument"
        logger.exception(msg)
        sys.exit(msg)

    try:
        all_entity_uuids = [f for f in listdir(uploads_dir_abs_path) if isdir(join(uploads_dir_abs_path, f))]

        # Loop through all the entity uuid dirs
        for entity_uuid in all_entity_uuids:
            entity_uuid_dir_abs_path = join(uploads_dir_abs_path, entity_uuid)
            all_file_uuids = [f for f in listdir(entity_uuid_dir_abs_path) if isdir(join(entity_uuid_dir_abs_path, f))]
            
            for file_uuid in all_file_uuids:
                file_uuid_dir_abs_path = join(uploads_dir_abs_path, entity_uuid, file_uuid)
                all_filenames = [f for f in listdir(file_uuid_dir_abs_path) if isfile(join(file_uuid_dir_abs_path, f))]

                for filename in all_filenames:
                    # Link the uploaded file uuid dir to assets
                    # /hive/hubmap/hm_uploads/<entity_uuid>/<file_uuid>/<filename> (for PROD)
                    source_file_path = join(file_uuid_dir_abs_path, filename)
                    # /hive/hubmap/assets/<file_uuid>/<filename> (for PROD)
                    target_file_dir = join(assets_dir_abs_path, file_uuid)
                    target_file_path = join(target_file_dir, filename)

                    # Create the file_uuid directory under assets dir
                    # and a symbolic link to the uploaded file
                    try:
                        Path(target_file_dir).mkdir(parents=True, exist_ok=True)
                        symlink(source_file_path, target_file_path)
                    except Exception as e:
                        logger.exception(f"Failed to create the symbolic link from {file_uuid_dir_abs_path} to {target_file_dir}")
    except Exception as e:                                                                                                            
        exc_type, exc_value, exc_traceback = sys.exc_info()
        eMsg = str(e)                                                                                                                     
        print("ERROR Occurred: " + eMsg)
        traceback.print_tb(exc_traceback)
        exit(1)
# Run this python file as a script in command line # python3 -m pre_ingest_thumbnails <uploads_dir_abs_path> <assets_dir_abs_path> # Loop through all the entity uuid dirs # Link the uploaded file uuid dir to assets # /hive/hubmap/hm_uploads/<entity_uuid>/<file_uuid>/<filename> (for PROD) # /hive/hubmap/assets/<file_uuid>/<filename> (for PROD) # Create the file_uuid directory under assets dir # and a symbolic link to the uploaded file