예제 #1
0
def parse_manifest(manifest, log_name_source):
    '''
    Analyses the manifest to see if any files are missing.
    Returns a list of missing files and a dictionary containing checksums
    and paths.
    '''
    source_dir = os.path.join(
        os.path.dirname(manifest),
        os.path.basename(manifest).replace('_manifest.md5', ''))
    source_count, file_list = ififuncs.count_stuff(source_dir)
    missing_files_list = []
    manifest_dict = {}
    paths = []
    proceed = 'Y'
    os.chdir(os.path.dirname(manifest))
    with open(manifest, 'r') as manifest_object:
        manifest_list = manifest_object.readlines()
        for entries in manifest_list:
            checksum = entries.split(' ')[0]
            if 'manifest-sha512.txt' in manifest:
                path = entries[130:].replace('\r', '').replace('\n', '')
            else:
                path = entries[34:].replace('\r', '').replace('\n', '')
            path = path.replace('\\', '/')
            if not os.path.isfile(path):
                ififuncs.generate_log(log_name_source, '%s is missing' % path)
                print(('%s is missing' % path))
                missing_files_list.append(path)
            elif os.path.isfile(path):
                manifest_dict[path] = checksum
                paths.append(path)
    manifest_file_count = len(manifest_list)
    if source_count != manifest_file_count:
        print(
            ' - There is masmatch between your file count and the manifest file count'
        )
        print(' - checking which files are different')
        for i in file_list:
            if i not in paths:
                print((
                    i,
                    'is present in your source directory but not in the source manifest'
                ))
        proceed = ififuncs.ask_yes_no('Do you want to proceed regardless?')
    if proceed == 'N':
        print('Exiting')
        sys.exit()
    else:
        if len(missing_files_list) > 0:
            print(
                ('The number of missing files: %s' % len(missing_files_list)))
            ififuncs.generate_log(
                log_name_source,
                'The number of missing files is: %s' % len(missing_files_list))
        elif len(missing_files_list) == 0:
            print('All files present')
            ififuncs.generate_log(log_name_source, 'All files present')
    return manifest_dict, missing_files_list
예제 #2
0
def main(args_):
    '''
    Launches the functions that will safely copy and paste your files.
    '''
    manifest_temp = '--'  # add two characters so that I can slice for manifest_temp[1] later.
    dircheck = None
    args, rootpos, manifest_sidecar, log_name_source, destination_final_path, manifest_root, manifest_destination, manifest, destination, dirname, desktop_manifest_dir = setup(
        args_)
    if os.path.isdir(args.source):
        dircheck = check_for_sip(args.source)
    if dircheck != None:
        if os.path.isdir(dircheck):
            source = check_for_sip(args.source)
    else:
        source = os.path.abspath(args.source)
        destination = args.destination
    overwrite_destination_manifest, overwrite_destination_dir = overwrite_check(
        destination, log_name_source, destination_final_path,
        manifest_destination)
    remove_bad_files(source, log_name_source)
    source_count, file_list = ififuncs.count_stuff(source)
    manifest_existence(manifest_root, manifest_sidecar, manifest, source_count,
                       file_list, log_name_source)
    manifest_sidecar, manifest, rootpos = control_flow(manifest_sidecar,
                                                       log_name_source,
                                                       manifest, rootpos, args,
                                                       source)
    if overwrite_destination_dir not in ('N', 'n'):
        if overwrite_destination_dir != None:
            generate_log(
                log_name_source,
                'EVENT = File Transfer Overwrite - Destination directory already exists - Overwriting.'
            )
        if not args.move:
            copy_dir(source, destination_final_path, log_name_source, rootpos,
                     destination, dirname, args)
        else:
            shutil.move(source, destination_final_path)
    else:
        generate_log(
            log_name_source,
            'EVENT = File Transfer Overwrite - Destination directory already exists - Not Overwriting.'
        )
    if args.justcopy:
        generate_log(
            log_name_source,
            'EVENT = Exiting without destination manifest or verification due to the use of -justcopy'
        )
        print(
            'Exiting without destination manifest or verification due to the use of -justcopy'
        )
        sys.exit()
    else:
        files_in_manifest = make_destination_manifest(
            overwrite_destination_manifest, log_name_source, rootpos,
            destination_final_path, manifest_destination, destination)
        destination_count = 0
        # dear god do this better, this is dreadful code!
        for _, _, filenames in os.walk(destination_final_path):
            for _ in filenames:
                destination_count += 1  #works in windows at least
        if rootpos == 'y':
            manifest_temp = tempfile.mkstemp(dir=desktop_manifest_dir,
                                             suffix='.md5')
            os.close(manifest_temp[0])  # Needed for windows.
            with open(manifest, 'r') as fo:
                dest_manifest_list = fo.readlines()
                with open(manifest_temp[1], 'w') as temp_object:
                    for i in dest_manifest_list:
                        temp_object.write(i[:33] + ' ' + os.path.basename(
                            os.path.dirname(destination_final_path)) + '/' +
                                          i[34:])
                legacy_manifest = manifest
                manifest = manifest_temp[1]
        verify_copy(manifest, manifest_destination, log_name_source,
                    overwrite_destination_manifest, files_in_manifest,
                    destination_count, source_count)
        manifest_rename = manifest[:-4] + time.strftime(
            "_%Y_%m_%dT%H_%M_%S") + '.md5'
        if os.path.normpath(os.path.dirname(manifest)) == os.path.normpath(
                desktop_manifest_dir):
            os.rename(manifest, manifest_rename)
            shutil.move(manifest_rename,
                        os.path.join(desktop_manifest_dir, 'old_manifests'))
            if rootpos == 'y':
                legacy_manifest_rename = legacy_manifest[:-4] + time.strftime(
                    "_%Y_%m_%dT%H_%M_%S") + '.md5'
                os.rename(legacy_manifest, legacy_manifest_rename)
                shutil.move(
                    legacy_manifest_rename,
                    os.path.join(desktop_manifest_dir, 'old_manifests'))
        # hack to also copy the sha512 manifest :(
        # Stop the temp manifest from copying
        if not os.path.basename(
                manifest_temp[1]) == os.path.basename(manifest):
            sha512_manifest = manifest.replace('_manifest.md5',
                                               '_manifest-sha512.txt')
            if os.path.isfile(sha512_manifest):
                shutil.copy2(sha512_manifest,
                             os.path.dirname(destination_final_path))
                print(('%s has been copied to %s' %
                       (sha512_manifest,
                        os.path.dirname(destination_final_path))))
        return log_name_source