Beispiel #1
0
def checkNoInput(files):
    # Check if input arguments list results in any existing input files at all
    # (and exits if not)

    if len(files) == 0:
        printWarning("no images to check!")
        sys.exit(config.ERR_CODE_NO_IMAGES)
def checkNoInput(files):
    # Check if input arguments list results in any existing input files at all
    # (and exits if not)

    if len(files) == 0:
        printWarning("no images to check!")
        sys.exit(config.ERR_CODE_NO_IMAGES)
Beispiel #3
0
def checkFiles(images, pretty=False):
    pretty = config.outputPretty
    if pretty:
        try:
            from xml.etree import ElementTree
            from xml.dom import minidom
        except ImportError:
            pretty = False

    if len(images) == 0:
        printWarning("no images to check!")

    for image in images:
            thisFile = image

            isFile = os.path.isfile(thisFile)

            if isFile:
                # Analyse file
                result = checkOneFile(thisFile)

                if pretty:
                    # Write pretty output
                    reparsed = minidom.parseString(result)
                    sys.stdout.write(reparsed.toprettyxml(indent="  "))
                else:
                    # Write ugly output
                    sys.stdout.write(result)
Beispiel #4
0
def checkNullArgs(args):
    # This method checks if the input arguments list and exits program if invalid or no input argument is supplied.
    
    if len(args) == 0:
        print("\n")
        printWarning("no images found (or supplied) to check!")
        print("\n")
        parser.print_help()
        sys.exit(ERR_CODE_NO_IMAGES)
def checkNullArgs(args):
    # This method checks if the input arguments list and exits program if invalid or no input argument is supplied.

    if len(args) == 0:
        print("\n")
        printWarning("no images found (or supplied) to check!")
        print("\n")
        parser.print_help()
        sys.exit(ERR_CODE_NO_IMAGES)
Beispiel #6
0
def jp2CreationInfo(jp2ImageOut):
    # Check if output image was created (Aware exit status doesn't really help here),
    # and print info to screen 
    if os.path.isfile(jp2ImageOut)==False:
        jp2Created=False
        shared.printWarning(jp2ImageOut + " not created! \n")
    else:
        shared.printInfo("created output image " + jp2ImageOut + "\n")
        jp2Created=True
    
    return jp2Created 
Beispiel #7
0
def checkFiles(images):
    if len(images) == 0:
        printWarning("no images to check!")

    for image in images:
            thisFile = image

            isFile = os.path.isfile(thisFile)

            if isFile:
                # Analyse file
                result=checkOneFile(thisFile)
                
                # Write output to stdout
                sys.stdout.write(result)
Beispiel #8
0
def checkFiles(images):
    if len(images) == 0:
        printWarning("no images to check!")

    for image in images:
        thisFile = image

        isFile = os.path.isfile(thisFile)

        if isFile:
            # Analyse file
            result = checkOneFile(thisFile)

            # Write output to stdout
            sys.stdout.write(result)
Beispiel #9
0
def findFiles(recurse, paths):

    WILDCARD = "*"

    # process the list of input paths
    for root in paths:

        # WILDCARD IN PATH OR FILENAME
        # In Linux wilcard expansion done by bash so, add file to list
        if os.path.isfile(root):
            existingFiles.append(root)
        # Windows (& Linux with backslash prefix) does not expand wildcard '*'
        # Find files in the input path and add to list
        elif WILDCARD in root:
            # get the absolute path if not given
            if not(os.path.isabs(root)):
                root = os.path.abspath(root)

            # Expand wildcard in the input path. Returns a list of files,
            # folders
            filesList = glob.glob(root)

            # If the input path is a directory, then glob expands it to full
            # name
            if len(filesList) == 1:
                # set root to the expanded directory path
                root = filesList[0]

            # get files from directory
            """ Disabled JvdK: if enabled all files in direct child directories are analysed - do we really want that?
            if os.path.isdir(root) and not recurse:
                getFilesFromDir(root)
            """

            # If the input path returned files list, add files to List
            if len(filesList) > 1:
                for f in filesList:
                    if os.path.isfile(f):
                        existingFiles.append(f)

        elif os.path.isdir(root) == False and os.path.isfile(root) == False:
            # One or more (but not all) paths do no exist - print a warning
            msg = root + " does not exist"
            printWarning(msg)

        """ Disabled JvdK: 
        elif os.path.isdir(root) and not recurse:
            #input path is a directory and is not recursive
            getFilesFromDir(root)
        """

        # RECURSION and WILDCARD IN RECURSION
        # Check if recurse in the input path
        if recurse:
            # get absolute input path if not given
            if not(os.path.isabs(root)):
                root = os.path.abspath(root)

            if WILDCARD in root:
                pathAndFilePattern = os.path.split(root)
                path = pathAndFilePattern[0]
                filePattern = pathAndFilePattern[1]
                filenameAndExtension = os.path.splitext(filePattern)
                # input path contains wildcard
                if WILDCARD in path:
                    filepath = glob.glob(path)
                    # if filepath is a folder, get files in current directory
                    if len(filepath) == 1:
                        getFilesWithPatternFromTree(filepath[0], filePattern)
                    # if filepath is a list of files/folder
                    # get all files in the tree matching the file pattern
                    if len(filepath) > 1:
                        for f in filepath:
                            if os.path.isdir(f):
                                getFilesWithPatternFromTree(f, filePattern)
                # file name or extension contains wildcard
                elif WILDCARD in filePattern:
                    getFilesWithPatternFromTree(path, filePattern)
                elif WILDCARD in filenameAndExtension:
                    filenameAndExtension = os.path.splitext(filePattern)
                    extension = WILDCARD + filenameAndExtension[1]
                    getFilesWithPatternFromTree(path, extension)
            # get files in the current folder and sub dirs w/o wildcard in path
            elif os.path.isdir(root):
                getFilesFromTree(root)
def findFiles(recurse, paths):

    WILDCARD = "*"

    # process the list of input paths
    for root in paths:
    
        if config.PYTHON_VERSION.startswith(config.PYTHON_2):
            # Convert root to UTF-8 (only needed for Python 2.x)
            root = unicode(root, 'utf-8')

        # WILDCARD IN PATH OR FILENAME
        # In Linux wilcard expansion done by bash so, add file to list
        if os.path.isfile(root):
            existingFiles.append(root)
        # Windows (& Linux with backslash prefix) does not expand wildcard '*'
        # Find files in the input path and add to list
        elif WILDCARD in root:
            # get the absolute path if not given
            if not(os.path.isabs(root)):
                root = os.path.abspath(root)

            # Expand wildcard in the input path. Returns a list of files,
            # folders
            filesList = glob.glob(root)

            # If the input path is a directory, then glob expands it to full
            # name
            if len(filesList) == 1 and os.path.isdir(filesList[0]):
                # set root to the expanded directory path
                root = filesList[0]

            # get files from directory
            """ Disabled JvdK: if enabled all files in direct child directories are analysed - do we really want that?
            if os.path.isdir(root) and not recurse:
                getFilesFromDir(root)
            """

            # If the input path returned files list, add files to List

            if len(filesList) == 1 and os.path.isfile(filesList[0]):
                existingFiles.append(filesList[0])

            if len(filesList) > 1:
                for f in filesList:
                    if os.path.isfile(f):
                        existingFiles.append(f)

        elif os.path.isdir(root) == False and os.path.isfile(root) == False:
            # One or more (but not all) paths do no exist - print a warning
            msg = root + " does not exist"
            printWarning(msg)

        """ Disabled JvdK:
        elif os.path.isdir(root) and not recurse:
            #input path is a directory and is not recursive
            getFilesFromDir(root)
        """

        # RECURSION and WILDCARD IN RECURSION
        # Check if recurse in the input path
        if recurse:
            # get absolute input path if not given
            if not(os.path.isabs(root)):
                root = os.path.abspath(root)

            if WILDCARD in root:
                pathAndFilePattern = os.path.split(root)
                path = pathAndFilePattern[0]
                filePattern = pathAndFilePattern[1]
                filenameAndExtension = os.path.splitext(filePattern)
                # input path contains wildcard
                if WILDCARD in path:
                    filepath = glob.glob(path)
                    # if filepath is a folder, get files in current directory
                    if len(filepath) == 1:
                        getFilesWithPatternFromTree(filepath[0], filePattern)
                    # if filepath is a list of files/folder
                    # get all files in the tree matching the file pattern
                    if len(filepath) > 1:
                        for f in filepath:
                            if os.path.isdir(f):
                                getFilesWithPatternFromTree(f, filePattern)
                # file name or extension contains wildcard
                elif WILDCARD in filePattern:
                    getFilesWithPatternFromTree(path, filePattern)
                elif WILDCARD in filenameAndExtension:
                    filenameAndExtension = os.path.splitext(filePattern)
                    extension = WILDCARD + filenameAndExtension[1]
                    getFilesWithPatternFromTree(path, extension)
            # get files in the current folder and sub dirs w/o wildcard in path
            elif os.path.isdir(root):
                getFilesFromTree(root)
def checkOneFileData(fileName, filePath, fileSizeInBytes, fileLastModifiedDate, fileData):
    # Process the data from one file and return analysis result as element object

    # Create output elementtree object

    if config.inputRecursiveFlag or config.inputWrapperFlag:
        # Name space already declared in results element, so no need to do it
        # here
        root = ET.Element('jpylyzer')
    else:
        root = ET.Element(
            'jpylyzer', {'xmlns': 'http://openpreservation.org/ns/jpylyzer/',
                         'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
                         'xsi:schemaLocation': 'http://openpreservation.org/ns/jpylyzer/ http://jpylyzer.openpreservation.org/jpylyzer-v-1-1.xsd'})

    # Create elements for storing tool, file and status meta info
    toolInfo = ET.Element('toolInfo')
    fileInfo = ET.Element('fileInfo')
    statusInfo = ET.Element('statusInfo')

    # If file name / path contain any surrogate pairs, remove them to
    # avoid problems when writing to XML
    fileNameCleaned = stripSurrogatePairs(fileName)
    filePathCleaned = stripSurrogatePairs(filePath)


    # Produce some general tool and file meta info
    toolInfo.appendChildTagWithText("toolName", scriptName)
    toolInfo.appendChildTagWithText("toolVersion", __version__)
    fileInfo.appendChildTagWithText("fileName", fileNameCleaned)
    fileInfo.appendChildTagWithText("filePath", filePathCleaned)
    fileInfo.appendChildTagWithText(
        "fileSizeInBytes", str(fileSizeInBytes))
    fileInfo.appendChildTagWithText(
        "fileLastModified", fileLastModifiedDate)

    # Initialise success flag
    success = True
    
    try:
        # Contents of file to memory map object
        isValidJP2, tests, characteristics = BoxValidator("JP2", fileData).validate()
        
        # Generate property values remap table
        remapTable = generatePropertiesRemapTable()

        # Create printable version of tests and characteristics tree
        tests.makeHumanReadable()
        characteristics.makeHumanReadable(remapTable)
    except Exception as ex:    
        isValidJP2 = False
        success = False
        exceptionType = type(ex)

        if exceptionType == MemoryError:
            failureMessage = "memory error (file size too large)"
        elif exceptionType == IOError:
            failureMessage = "I/O error (cannot open file)"
        elif exceptionType == RuntimeError:
            failureMessage = "runtime error (please report to developers)"
        else:
            failureMessage = "unknown error (please report to developers)"

        printWarning(failureMessage)
        tests = ET.Element("tests")
        characteristics = ET.Element('properties')
 
    # Add status info
    statusInfo.appendChildTagWithText("success", str(success))
    if success == False:
        statusInfo.appendChildTagWithText("failureMessage",failureMessage)
  
    # Append all results to root
    root.append(toolInfo)
    root.append(fileInfo)
    root.append(statusInfo)
    root.appendChildTagWithText("isValidJP2", str(isValidJP2))
    root.append(tests)
    root.append(characteristics)

    return(root)
Beispiel #12
0
def checkOneFile(file):
    # Process one file and return analysis result as element object

    # Create output elementtree object

    if config.inputRecursiveFlag or config.inputWrapperFlag:
        # Name space already declared in results element, so no need to do it
        # here
        root = ET.Element('jpylyzer')
    else:
        root = ET.Element(
            'jpylyzer', {'xmlns': 'http://openpreservation.org/ns/jpylyzer/',
                         'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
                         'xsi:schemaLocation': 'http://openpreservation.org/ns/jpylyzer/ http://jpylyzer.openpreservation.org/jpylyzer-v-1-1.xsd'})

    # Create elements for storing tool, file and status meta info
    toolInfo = ET.Element('toolInfo')
    fileInfo = ET.Element('fileInfo')
    statusInfo = ET.Element('statusInfo')

    # File name and path 
    fileName = os.path.basename(file)
    filePath = os.path.abspath(file)

    # If file name / path contain any surrogate pairs, remove them to
    # avoid problems when writing to XML
    fileNameCleaned = stripSurrogatePairs(fileName)
    filePathCleaned = stripSurrogatePairs(filePath)


    # Produce some general tool and file meta info
    toolInfo.appendChildTagWithText("toolName", scriptName)
    toolInfo.appendChildTagWithText("toolVersion", __version__)
    fileInfo.appendChildTagWithText("fileName", fileNameCleaned)
    fileInfo.appendChildTagWithText("filePath", filePathCleaned)
    fileInfo.appendChildTagWithText(
        "fileSizeInBytes", str(os.path.getsize(file)))
    try:
        lastModifiedDate = time.ctime(os.path.getmtime(file))
    except ValueError:
        # Dates earlier than 1 Jan 1970 can raise ValueError on Windows
        # Workaround: replace by lowest possible value (typically 1 Jan 1970)
        lastModifiedDate = time.ctime(0)
    fileInfo.appendChildTagWithText(
        "fileLastModified", lastModifiedDate)

    # Initialise success flag
    success = True
    
    try:
        # Contents of file to memory map object
        fileData = fileToMemoryMap(file)
        isValidJP2, tests, characteristics = BoxValidator("JP2", fileData).validate()
        
        if fileData != "":
            fileData.close()

        # Generate property values remap table
        remapTable = generatePropertiesRemapTable()

        # Create printable version of tests and characteristics tree
        tests.makeHumanReadable()
        characteristics.makeHumanReadable(remapTable)
    except Exception as ex:    
        isValidJP2 = False
        success = False
        exceptionType = type(ex)

        if exceptionType == MemoryError:
            failureMessage = "memory error (file size too large)"
        elif exceptionType == IOError:
            failureMessage = "I/O error (cannot open file)"
        elif exceptionType == RuntimeError:
            failureMessage = "runtime error (please report to developers)"
        else:
            failureMessage = "unknown error (please report to developers)"

        printWarning(failureMessage)
        tests = ET.Element("tests")
        characteristics = ET.Element('properties')
 
    # Add status info
    statusInfo.appendChildTagWithText("success", str(success))
    if success == False:
        statusInfo.appendChildTagWithText("failureMessage",failureMessage)
  
    # Append all results to root
    root.append(toolInfo)
    root.append(fileInfo)
    root.append(statusInfo)
    root.appendChildTagWithText("isValidJP2", str(isValidJP2))
    root.append(tests)
    root.append(characteristics)

    return(root)
Beispiel #13
0
def convertOneImageToJP2(imageIn,imageOut,awareOptionsString,exifToolApp,j2kDriverApp,extractMetadataFlag):

    # Convert one image to JP2. Arguments:
    #
    # imageIn: name of input image
    # imageOut: name of output JP2 image
    # awareOptionsString: text string with Aware options
    # exifToolApp: path to ExifTool executable
    # j2kDriverApp: path to Aware executable
    # extractMetadataFlag: True if metadata extraction is needed, False otherwise

    # Initialise exit status flag + output strings for ExifTool /Aware
    # (in case extractMetadataFlag equals 0)
    exifExitStatus=0
    awareExitStatus=0
    exifStdOut=""
    exifStdErr=""
    awareStdOut=""
    awareStdErr=""

    # Does input image exist?
    imageInExists=os.path.isfile(imageIn)
       
    #if imageInExists==False:
   
    # If output image already exists, delete it! Why this? -->
    # 1. If the conversion proceeds as planned, it will be overwritten anyway, BUT ...
    # 2. If something goes wrong and the image cannot be created, the absence of 
    #    the output image may be the only way to find out about this afterwards
    #    (because we cannot rely on Aware's exit status for this, see below)
    shared.removeFile(imageOut)
   
    # Absolute path to output image (location also used for temporary file)
    pathOut=os.path.abspath(os.path.split(imageOut)[0])

    # Metadata extraction (optional)

    # Initialise Aware command line option for metadata embedding
    aMetadata=""

    if extractMetadataFlag==True:
        # Generate name for (temporary) xmp file
        fileXMP=shared.constructFileName(imageIn,pathOut,"xmp","")
        
        # If previous run of aWrapper was terminated by the user, a file with this
        # name could still be there (and this would raise an error in ExifTool)
        shared.removeFile(fileXMP)

        # Extract metadata from input image and store result in XMP format
        exifSysString=shared.quoteString(exifToolApp) + " " + imageIn + " -o " + fileXMP
        shared.printInfo("running ExifTool")
        
        exifExitStatus,exifStdOut,exifStdErr=launchSubProcess(exifSysString)
                
        # Generate Aware command line option for metadata embedding
        aMetadata=awareMetadataString(fileXMP)

    # Construct encoder command line 
    aInput=awareInputImageString(imageIn)
    aOutput=awareOutputImageString(imageOut)
    awareSysString=(shared.quoteString(j2kDriverApp) + " " + aInput + " " + awareOptionsString + " " +
                aMetadata + " " + aOutput)

    # Run encoder, but only if ExifTool didn't give any errors.
   
    if exifExitStatus==0:
        shared.printInfo("running Aware j2kdriver ...")
        awareExitStatus,awareStdOut,awareStdErr=launchSubProcess(awareSysString)
    else:
        shared.printWarning(imageIn + " not converted because ExifTool exited with errors! \
        See log file for details.")
        
    # Check if output image was really created and print info to screen
    # (Note that Aware can give exit code 0 even if things go seriously wrong!)
    jp2Created=jp2CreationInfo(imageOut)
    
    if extractMetadataFlag==True and exifExitStatus==0:
        # Clean up temporary file
        os.remove(fileXMP)
       
    # Create element object for storing conversion info
    conversionInfo=ET.Element('image')
    
    # Pre-format all output
    timeStr=time.asctime()  
    imageIn=shared.toUTF8(os.path.abspath(imageIn))
    imageOut=shared.toUTF8(os.path.abspath(imageOut))
    jp2Created=str(jp2Created)
    exifExitStatus=str(exifExitStatus)
    exifStdOut=shared.toUTF8(exifStdOut)
    exifStdErr=shared.toUTF8(exifStdErr)
    awareExitStatus=str(awareExitStatus)
    awareStdOut=shared.toUTF8(awareStdOut)
    awareStdErr=shared.toUTF8(awareStdErr)
    
    # Add to element  
    conversionInfo.appendChildTagWithText("time", timeStr)       
    conversionInfo.appendChildTagWithText("imageIn", imageIn)
    conversionInfo.appendChildTagWithText("imageOut", imageOut)
    conversionInfo.appendChildTagWithText("jp2Created", jp2Created)
    conversionInfo.appendChildTagWithText("exifExitStatus", exifExitStatus)
    conversionInfo.appendChildTagWithText("exifStdOut", exifStdOut)
    conversionInfo.appendChildTagWithText("exifStdErr", exifStdErr)
    conversionInfo.appendChildTagWithText("awareExitStatus", awareExitStatus)
    conversionInfo.appendChildTagWithText("awareStdOut", awareStdOut)
    conversionInfo.appendChildTagWithText("awareStdErr", awareStdErr)
        
    # Return conversion info
    return(conversionInfo)