# Path: masscopy.py
# def analyze_log(logfile):
#     '''
#     Analyzes logfiles on the desktop and summarises the outcome.
#     '''
#     outcome = ''
#     with open(logfile, 'r') as fo:
#         log_lines = fo.readlines()
#         for line in log_lines:
#             if 'EVENT = File Transfer Judgement - Success' in line:
#                 outcome = 'success'
#             if 'EVENT = File Transfer Outcome - Failure' in line:
#                 outcome = 'failure'
#             if 'EVENT = Existing source manifest check - Failure' in line:
#                 outcome = 'failure - might be outdated manifests in use'
#         return outcome


import os
import argparse
import sys
import shutil
import datetime
import time
import copyit
import ififuncs
import package_update
import accession
import manifest
import makezip
import accession
    import dicttoxml
            import clairmeta
from masscopy import analyze_log
    from clairmeta.utils.xml import prettyprint_xml
    from clairmeta import DCP
                objects_dir = os.path.join(sip_path, 'objects')
                uuid = os.path.basename(sip_path)
                old_basename, ext = os.path.splitext(item)
                new_path = os.path.join(objects_dir, uuid + ext)
                os.rename(os.path.join(objects_dir, os.path.basename(item)), new_path)
                manifest = os.path.join(os.path.dirname(new_path), os.path.basename(item)) + '_manifest.md5'
                updated_lines = []
                ififuncs.generate_log(
                    log_name,
                    'EVENT = Filename change - eventDetail=original filename replaced with uuid, eventOutcomeDetailNote=%s replaced with %s, agentName=%s, agentName=sipcreator.py))' % (os.path.basename(item), uuid + ext, user))
                with open(manifest, 'r') as file_object:
                    checksums = file_object.readlines()
                    for line in checksums:
                        if os.path.basename(item) in line:
                            line = line.replace(os.path.basename(item), os.path.basename(new_path))
                            updated_lines.append(line)
                with open(manifest, 'w') as fo:
                    for lines in updated_lines:
                        fo.write(lines)
    consolidate_logs(log_names, sip_path)
    return log_names


def log_report(log_names):
    '''
    Analyzes all the moveit.py logs on the desktop and print a report.
    '''
    desktop_logs_dir = ififuncs.make_desktop_logs_dir()
    for i in log_names:
        if os.path.isfile(i):

# Path: masscopy.py
# def analyze_log(logfile):
#     '''
#     Analyzes logfiles on the desktop and summarises the outcome.
#     '''
#     outcome = ''
#     with open(logfile, 'r') as fo:
#         log_lines = fo.readlines()
#         for line in log_lines:
#             if 'EVENT = File Transfer Judgement - Success' in line:
#                 outcome = 'success'
#             if 'EVENT = File Transfer Outcome - Failure' in line:
#                 outcome = 'failure'
#             if 'EVENT = Existing source manifest check - Failure' in line:
#                 outcome = 'failure - might be outdated manifests in use'
#         return outcome


# Path: sipcreator.py
import os
import argparse
import sys
import shutil
import datetime
import time
import copyit
import ififuncs
import package_update
import accession
import manifest
import makezip
import accession
    import dicttoxml
            import clairmeta
from masscopy import analyze_log
    from clairmeta.utils.xml import prettyprint_xml
    from clairmeta import DCP

                objects_dir = os.path.join(sip_path, 'objects')
                uuid = os.path.basename(sip_path)
                old_basename, ext = os.path.splitext(item)
                new_path = os.path.join(objects_dir, uuid + ext)
                os.rename(os.path.join(objects_dir, os.path.basename(item)), new_path)
                manifest = os.path.join(os.path.dirname(new_path), os.path.basename(item)) + '_manifest.md5'
                updated_lines = []
                ififuncs.generate_log(
                    log_name,
                    'EVENT = Filename change - eventDetail=original filename replaced with uuid, eventOutcomeDetailNote=%s replaced with %s, agentName=%s, agentName=sipcreator.py))' % (os.path.basename(item), uuid + ext, user))
                with open(manifest, 'r') as file_object:
                    checksums = file_object.readlines()
                    for line in checksums:
                        if os.path.basename(item) in line:
                            line = line.replace(os.path.basename(item), os.path.basename(new_path))
                            updated_lines.append(line)
                with open(manifest, 'w') as fo:
                    for lines in updated_lines:
                        fo.write(lines)
    consolidate_logs(log_names, sip_path)
    return log_names


def log_report(log_names):
    '''
    Analyzes all the moveit.py logs on the desktop and print a report.
    '''
    desktop_logs_dir = ififuncs.make_desktop_logs_dir()
    for i in log_names:
        if os.path.isfile(i):

            print(("%-*s   :  copyit job was a %s" % (50, os.path.basename(i)[:-24], analyze_log(i))))