Пример #1
0
def copy_images_to_output_directory(
        image_paths: list,
        root_path: str,
        output_path: str) -> None:
    """ Copy all images to the output directory. """

    context = os.path.basename(root_path)

    for image_path in image_paths:
        # copy each relatively specified image
        if is_url(image_path):
            # unless it's a URL
            WarningDisplay.image_not_copied(
                WarningContext(context), image_path)
        else:
            # if the image path is not an absolute path, assume
            # that it's located relative to where the data is
            relative_source_path = os.path.join(
                os.path.dirname(root_path), image_path)

            # only copy if the file actually exists
            if os.path.isfile(relative_source_path):
                # retain relative directory structure
                resource_path = image_path

                if is_resource(image_path):
                    # unless it's a resource that should always be copied to the output directory
                    resource_path = get_resource_path(os.path.basename(image_path))

                relative_destination_path = os.path.join(
                    output_path, resource_path)

                # make sure any missing directories are created as needed
                create_directories_if_necessary(
                    os.path.dirname(relative_destination_path))

                resource_was_copied, resource_already_existed = copy_file_if_necessary(
                    relative_source_path, relative_destination_path)

                resource_was_overwritten = resource_already_existed and resource_was_copied
                resource_was_duplicate = resource_already_existed and not resource_was_copied

                if resource_was_duplicate:
                    # do nothing for now- this is triggered several times and is neither
                    # a problem nor something that the user is interested in knowing about
                    pass
                elif resource_was_overwritten:
                    # the resource was named identically to an existing resource, but had
                    # different or changed file contents; this might be an error, so warn about
                    WarningDisplay.resource_was_overwritten(
                        WarningContext(context), resource_path, relative_source_path)
            else:
                WarningDisplay.missing_image_error(
                    WarningContext(context), relative_source_path)
Пример #2
0
def copy_images_to_output_directory(image_paths: list, root_path: str,
                                    output_path: str) -> None:
    """ Copy all images to the output directory. """

    context = os.path.basename(root_path) if root_path is not None else '???'

    for image_path in image_paths:
        # copy each relatively specified image
        if is_url(image_path):
            # unless it's a URL
            WarningDisplay.image_not_copied(WarningContext(context),
                                            image_path)

            continue

        # if the image path is not an absolute path,
        # assume that it's located relative to where the data is
        relative_source_path = os.path.normpath(image_path)

        # only copy if the file actually exists
        if not os.path.isfile(relative_source_path):
            WarningDisplay.missing_image_error(WarningContext(context),
                                               relative_source_path)

            continue

        resource_path = get_resource_path(os.path.basename(image_path))

        relative_destination_path = os.path.join(output_path, resource_path)

        # make sure any missing directories are created as needed
        create_directories_if_necessary(
            os.path.dirname(relative_destination_path))

        resource_was_copied, resource_already_existed = copy_file_if_necessary(
            relative_source_path, relative_destination_path)

        resource_was_overwritten = resource_already_existed and resource_was_copied
        resource_was_duplicate = resource_already_existed and not resource_was_copied

        if resource_was_duplicate:
            # do nothing for now- this is triggered several times and is neither
            # a problem nor something that the user is interested in knowing about
            pass
        elif resource_was_overwritten:
            # the resource was named identically to an existing resource, but had
            # different or changed file contents; this might be an error, so warn about
            WarningDisplay.resource_was_overwritten(WarningContext(context),
                                                    resource_path,
                                                    relative_source_path)