|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from __future__ import print_function, unicode_literals |
| 5 | + |
| 6 | +import copy |
| 7 | +import logging |
| 8 | +import os |
| 9 | +import sys |
| 10 | + |
| 11 | +try: |
| 12 | + from .appconfig import AppConfig |
| 13 | + from .digital_object import DigitalObject |
| 14 | + from . import duplicates |
| 15 | + from . import loggingconfig |
| 16 | + from .serialize_to_csv import CSVOut |
| 17 | + from . import utils |
| 18 | +except ValueError: |
| 19 | + from appconfig import AppConfig |
| 20 | + from digital_object import DigitalObject |
| 21 | + import duplicates |
| 22 | + import loggingconfig |
| 23 | + from serialize_to_csv import CSVOut |
| 24 | + import utils |
| 25 | + |
| 26 | +logging_dir = os.path.dirname(os.path.abspath(__file__)) |
| 27 | + |
| 28 | +logger = logging.getLogger("accruals") |
| 29 | +logger.disabled = False |
| 30 | + |
| 31 | +# Location purpose = Transfer Source (TS) |
| 32 | +location_purpose = "TS" |
| 33 | +default_location = "accruals" |
| 34 | + |
| 35 | + |
| 36 | +# Do something with this... |
| 37 | +DOCKER = True |
| 38 | + |
| 39 | +# Store our appraisal paths. |
| 40 | +accrual_paths = [] |
| 41 | + |
| 42 | + |
| 43 | +def create_manifest(aip_index, accrual_objs): |
| 44 | + """do something.""" |
| 45 | + dupes = [] |
| 46 | + aip_obj_hashes = aip_index.get(duplicates.MANIFEST_DATA) |
| 47 | + for accrual_obj in accrual_objs: |
| 48 | + for accrual_hash in accrual_obj.hashes: |
| 49 | + if accrual_hash in aip_obj_hashes.keys(): |
| 50 | + for _, aip_items in aip_obj_hashes.items(): |
| 51 | + for item in aip_items: |
| 52 | + if accrual_obj == item: |
| 53 | + cp = copy.copy(accrual_obj) |
| 54 | + cp.package_name = item.package_name |
| 55 | + dupes.append(cp) |
| 56 | + # Only need one hash to match then break. |
| 57 | + # May also be redundant as we only have one hash from the |
| 58 | + # bag manifests... |
| 59 | + break |
| 60 | + return dupes |
| 61 | + |
| 62 | + |
| 63 | +def create_comparison_obj(transfer_path): |
| 64 | + """Do something.""" |
| 65 | + transfer_arr = [] |
| 66 | + for root, dirs, files in os.walk(transfer_path, topdown=True): |
| 67 | + for name in files: |
| 68 | + file_ = os.path.join(root, name) |
| 69 | + if os.path.isfile(file_): |
| 70 | + transfer_arr.append(DigitalObject(file_, transfer_path)) |
| 71 | + return transfer_arr |
| 72 | + |
| 73 | + |
| 74 | +def stat_transfers(accruals_path, all_transfers): |
| 75 | + """Retrieve all transfer paths and make a request to generate statistics |
| 76 | + about all the objects in that transfer path. |
| 77 | + """ |
| 78 | + aip_index = duplicates.retrieve_aip_index() |
| 79 | + reports = [] |
| 80 | + transfers = [] |
| 81 | + for transfer in all_transfers: |
| 82 | + transfer_home = os.path.join(accruals_path, transfer) |
| 83 | + if DOCKER: |
| 84 | + transfer_home = utils.get_docker_path(transfer_home) |
| 85 | + objs = create_comparison_obj(transfer_home) |
| 86 | + transfers.append(objs) |
| 87 | + reports.append({transfer: create_manifest(aip_index, objs)}) |
| 88 | + CSVOut.stat_manifests(aip_index, transfers) |
| 89 | + CSVOut.csv_out(reports, "") |
| 90 | + |
| 91 | + |
| 92 | +def main(location=default_location): |
| 93 | + """Primary entry point for this script.""" |
| 94 | + |
| 95 | + am = AppConfig().get_am_client() |
| 96 | + sources = am.list_storage_locations() |
| 97 | + |
| 98 | + accruals = False |
| 99 | + for source in sources.get("objects"): |
| 100 | + if ( |
| 101 | + source.get("purpose") == location_purpose |
| 102 | + and source.get("description") == location |
| 103 | + ): |
| 104 | + """do something.""" |
| 105 | + am.transfer_source = source.get("uuid") |
| 106 | + am.transfer_path = source.get("path") |
| 107 | + accruals = True |
| 108 | + if not accruals: |
| 109 | + logger.info("Exiting. No transfer source: {}".format(location)) |
| 110 | + sys.exit() |
| 111 | + |
| 112 | + # All transfer directories. Assumption is the same as Archivematica that |
| 113 | + # each transfer is organized into a single directory at this level. |
| 114 | + all_transfers = am.transferables().get("directories") |
| 115 | + stat_transfers(am.transfer_path, all_transfers) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + loggingconfig.setup("INFO", os.path.join(logging_dir, "report.log")) |
| 120 | + source = default_location |
| 121 | + try: |
| 122 | + source = sys.argv[1:][0] |
| 123 | + logger.error("Attempting to find transfers at: %s", source) |
| 124 | + except IndexError: |
| 125 | + pass |
| 126 | + sys.exit(main(source)) |
0 commit comments