|
| 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, ImportError): |
| 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 = AppConfig().accruals_transfer_source |
| 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 | + near_matches = [] |
| 47 | + non_matches = [] |
| 48 | + aip_obj_hashes = aip_index.get(duplicates.MANIFEST_DATA) |
| 49 | + for accrual_obj in accrual_objs: |
| 50 | + for accrual_hash in accrual_obj.hashes: |
| 51 | + if accrual_hash in aip_obj_hashes.keys(): |
| 52 | + for _, aip_items in aip_obj_hashes.items(): |
| 53 | + for aip_item in aip_items: |
| 54 | + if accrual_obj == aip_item: |
| 55 | + accrual_obj.flag = True |
| 56 | + cp = copy.copy(accrual_obj) |
| 57 | + cp.package_name = aip_item.package_name |
| 58 | + dupes.append(cp) |
| 59 | + else: |
| 60 | + diff = accrual_obj % aip_item |
| 61 | + if ( |
| 62 | + diff == "No matching components" |
| 63 | + or "checksum match" not in diff |
| 64 | + ): |
| 65 | + """Don't output.""" |
| 66 | + continue |
| 67 | + accrual_obj.flag = True |
| 68 | + cp1 = copy.copy(accrual_obj) |
| 69 | + cp2 = copy.copy(aip_item) |
| 70 | + near_matches.append([cp1, cp2]) |
| 71 | + # Only need one hash to match then break. |
| 72 | + # May also be redundant as we only have one hash from the |
| 73 | + # bag manifests... |
| 74 | + break |
| 75 | + for accrual_obj in accrual_objs: |
| 76 | + if accrual_obj.flag is False: |
| 77 | + cp = copy.copy(accrual_obj) |
| 78 | + if cp not in non_matches: |
| 79 | + non_matches.append(cp) |
| 80 | + return dupes, near_matches, non_matches |
| 81 | + |
| 82 | + |
| 83 | +def create_comparison_obj(transfer_path): |
| 84 | + """Do something.""" |
| 85 | + transfer_arr = [] |
| 86 | + for root, dirs, files in os.walk(transfer_path, topdown=True): |
| 87 | + for name in files: |
| 88 | + file_ = os.path.join(root, name) |
| 89 | + if os.path.isfile(file_): |
| 90 | + transfer_arr.append(DigitalObject(file_, transfer_path)) |
| 91 | + return transfer_arr |
| 92 | + |
| 93 | + |
| 94 | +def stat_transfers(accruals_path, all_transfers): |
| 95 | + """Retrieve all transfer paths and make a request to generate statistics |
| 96 | + about all the objects in that transfer path. |
| 97 | + """ |
| 98 | + aip_index = duplicates.retrieve_aip_index() |
| 99 | + dupe_reports = [] |
| 100 | + near_reports = [] |
| 101 | + no_match_reports = [] |
| 102 | + transfers = [] |
| 103 | + for transfer in all_transfers: |
| 104 | + transfer_home = os.path.join(accruals_path, transfer) |
| 105 | + if DOCKER: |
| 106 | + transfer_home = utils.get_docker_path(transfer_home) |
| 107 | + objs = create_comparison_obj(transfer_home) |
| 108 | + transfers.append(objs) |
| 109 | + match_manifest, near_manifest, no_match_manifest = create_manifest( |
| 110 | + aip_index, objs |
| 111 | + ) |
| 112 | + if match_manifest: |
| 113 | + dupe_reports.append({transfer: match_manifest}) |
| 114 | + if near_manifest: |
| 115 | + near_reports.append({transfer: near_manifest}) |
| 116 | + if no_match_manifest: |
| 117 | + no_match_reports.append({transfer: no_match_manifest}) |
| 118 | + CSVOut.stat_manifests(aip_index, transfers) |
| 119 | + if dupe_reports: |
| 120 | + CSVOut.dupe_csv_out(dupe_reports, "") |
| 121 | + if near_reports: |
| 122 | + CSVOut.near_csv_out(near_reports, "") |
| 123 | + if no_match_reports: |
| 124 | + CSVOut.no_match_csv_out(no_match_reports, "") |
| 125 | + |
| 126 | + |
| 127 | +def main(location=default_location): |
| 128 | + """Primary entry point for this script.""" |
| 129 | + |
| 130 | + am = AppConfig().get_am_client() |
| 131 | + sources = am.list_storage_locations() |
| 132 | + |
| 133 | + accruals = False |
| 134 | + for source in sources.get("objects"): |
| 135 | + if ( |
| 136 | + source.get("purpose") == location_purpose |
| 137 | + and source.get("description") == location |
| 138 | + ): |
| 139 | + """do something.""" |
| 140 | + am.transfer_source = source.get("uuid") |
| 141 | + am.transfer_path = source.get("path") |
| 142 | + accruals = True |
| 143 | + if not accruals: |
| 144 | + logger.info("Exiting. No transfer source: {}".format(location)) |
| 145 | + sys.exit() |
| 146 | + |
| 147 | + # All transfer directories. Assumption is the same as Archivematica that |
| 148 | + # each transfer is organized into a single directory at this level. |
| 149 | + all_transfers = am.transferables().get("directories") |
| 150 | + stat_transfers(am.transfer_path, all_transfers) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + loggingconfig.setup("INFO", os.path.join(logging_dir, "report.log")) |
| 155 | + source = default_location |
| 156 | + try: |
| 157 | + source = sys.argv[1:][0] |
| 158 | + logger.error("Attempting to find transfers at: %s", source) |
| 159 | + except IndexError: |
| 160 | + pass |
| 161 | + sys.exit(main(source)) |
0 commit comments