예제 #1
0
def main(target, xml_file, date, event_uuid, file_uuid, file_grpuse):
    """
    Note: xml_file, date and event_uuid are not being used.
    """
    if file_grpuse in ("DSPACEMETS", "maildirFile"):
        logger.error("File's fileGrpUse in exclusion list, skipping")
        return 0

    if not FPCommandOutput.objects.filter(file=file_uuid).exists():
        logger.error("Warning: Fits has already run on this file. Not running again.")
        return 0

    _, temp_file = tempfile.mkstemp()
    args = ["fits.sh", "-i", target, "-o", temp_file]
    try:
        logger.info("Executing %s", args)
        retcode, stdout, stderr = executeOrRun(
            "command", args, printing=False, capture_output=True
        )

        if retcode != 0:
            logger.error(
                "fits.sh exited with status code %s, %s, %s", retcode, stdout, stderr
            )
            return retcode

        try:
            tree = etree.parse(temp_file)
        except:
            logger.exception("Failed to read Fits's XML.")
            return 2

        fits = tree.getroot()
        fits = exclude_jhove_properties(fits)

        # NOTE: This is hardcoded for now because FPCommandOutput references FPRule for future development,
        #       when characterization will become user-configurable and be decoupled from FITS specifically.
        #       Thus a stub rule must exist for FITS; this will be replaced with a real rule in the future.
        logger.info("Storing output of file characterization...")
        insertIntoFPCommandOutput(
            file_uuid,
            etree.tostring(fits, pretty_print=False),
            "3a19de70-0e42-4145-976b-3a248d43b462",
        )

    except (OSError, ValueError):
        logger.exception("Execution failed")
        return 1

    finally:
        # We are responsible for removing the temporary file and we do it here
        # to ensure that it's going to happen whatever occurs inside our try
        # block.
        os.remove(temp_file)

    return 0
예제 #2
0
def main(job, file_path, file_uuid, sip_uuid):
    setup_dicts(mcpclient_settings)

    failed = False

    # Check to see whether the file has already been characterized; don't try
    # to characterize it a second time if so.
    if FPCommandOutput.objects.filter(file_id=file_uuid).count() > 0:
        return 0

    try:
        format = FormatVersion.active.get(
            fileformatversion__file_uuid=file_uuid)
    except FormatVersion.DoesNotExist:
        rules = format = None

    if format:
        rules = FPRule.active.filter(format=format.uuid,
                                     purpose="characterization")

    # Characterization always occurs - if nothing is specified, get one or more
    # defaults specified in the FPR.
    if not rules:
        rules = FPRule.active.filter(purpose="default_characterization")

    for rule in rules:
        if (rule.command.script_type == "bashScript"
                or rule.command.script_type == "command"):
            args = []
            command_to_execute = replace_string_values(rule.command.command,
                                                       file_=file_uuid,
                                                       sip=sip_uuid,
                                                       type_="file")
        else:
            rd = ReplacementDict.frommodel(file_=file_uuid,
                                           sip=sip_uuid,
                                           type_="file")
            args = rd.to_gnu_options()
            command_to_execute = rule.command.command

        exitstatus, stdout, stderr = executeOrRun(
            rule.command.script_type,
            command_to_execute,
            arguments=args,
            capture_output=True,
        )

        job.write_output(stdout)
        job.write_error(stderr)

        if exitstatus != 0:
            job.write_error(
                "Command {} failed with exit status {}; stderr:".format(
                    rule.command.description, exitstatus))
            failed = True
            continue
        # fmt/101 is XML - we want to collect and package any XML output, while
        # allowing other commands to execute without actually collecting their
        # output in the event that they are writing their output to disk.
        # FPCommandOutput can have multiple rows for a given file,
        # distinguished by the rule that produced it.
        if (rule.command.output_format
                and rule.command.output_format.pronom_id == "fmt/101"):
            try:
                etree.fromstring(stdout)
                insertIntoFPCommandOutput(file_uuid, stdout, rule.uuid)
                job.write_output(
                    'Saved XML output for command "{}" ({})'.format(
                        rule.command.description, rule.command.uuid))
            except etree.XMLSyntaxError:
                failed = True
                job.write_error(
                    'XML output for command "{}" ({}) was not valid XML; not saving to database'
                    .format(rule.command.description, rule.command.uuid))
        else:
            job.write_error(
                'Tool output for command "{}" ({}) is not XML; not saving to database'
                .format(rule.command.description, rule.command.uuid))

    if failed:
        return 255
    else:
        return 0
예제 #3
0
            print output[0]
        if output[1] != "":
            print >> sys.stderr, output[1]

        #it executes check for errors
        if retcode != 0:
            print >> sys.stderr, "error code:" + retcode.__str__()
            print output[1]  # sError
            quit(retcode)
        try:
            tree = etree.parse(tempFile)
        except:
            os.remove(tempFile)
            print >> sys.stderr, "Failed to read Fits's xml."
            exit(2)
        fits = tree.getroot()
        os.remove(tempFile)
        if excludeJhoveProperties:
            fits = excludeJhoveProperties(fits)
        # NOTE: This is hardcoded for now because FPCommandOutput references FPRule for future development,
        #       when characterization will become user-configurable and be decoupled from FITS specifically.
        #       Thus a stub rule must exist for FITS; this will be replaced with a real rule in the future.
        insertIntoFPCommandOutput(fileUUID,
                                  etree.tostring(fits, pretty_print=False),
                                  '3a19de70-0e42-4145-976b-3a248d43b462')

    except OSError as ose:
        print >> sys.stderr, "Execution failed:", ose
        exit(1)
    exit(exitCode)