Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

identifyFileFormat: Write identification into Events #15

Merged
merged 3 commits into from Mar 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 0 additions & 85 deletions src/MCPClient/lib/clientScripts/archivematicaFITS.py
Expand Up @@ -32,7 +32,6 @@
from archivematicaFunctions import escapeForCommand
from databaseFunctions import insertIntoFPCommandOutput
from databaseFunctions import insertIntoEvents
from databaseFunctions import insertIntoFilesIDs
import databaseInterface

databaseInterface.printSQL = False
Expand Down Expand Up @@ -101,94 +100,10 @@ def formatValidationFITSAssist(fits):
return tuple([eventDetailText, eventOutcomeText, eventOutcomeDetailNote]) #tuple([1, 2, 3]) returns (1, 2, 3).


def formatIdentificationFITSAssist(fits, fileUUID):
global exitCode
prefix = "{http://www.nationalarchives.gov.uk/pronom/FileCollection}"
formatIdentification = None

tools = getTagged(getTagged(fits, FITSNS + "toolOutput")[0], FITSNS + "tool")
for tool in tools:
if tool.get("name") == "Droid":
formatIdentification = tool
break
if formatIdentification == None:
print >>sys.stderr, "No format identification tool output (Droid)."
exitCode += 5
raise Exception('Droid', 'not present')

# <eventDetail>program="DROID"; version="3.0"</eventDetail>
eventDetailText = 'program="%s"; version="%s"' % (formatIdentification.get("name"), formatIdentification.get("version"))


fileCollection = getTagged(formatIdentification, prefix + "FileCollection")[0]
IdentificationFile = getTagged(fileCollection, prefix + "IdentificationFile")[0]
eventOutcomeText = IdentificationFile.get( "IdentQuality")

# <eventOutcomeDetailNote>fmt/116</eventOutcomeDetailNote>
# <FileFormatHit />
fileFormatHits = getTagged(IdentificationFile, prefix + "FileFormatHit")
eventOutcomeDetailNotes = []
eventOutcomeDetailNote = ""
for fileFormatHit in fileFormatHits:
format = etree.Element("format")
if len(fileFormatHit):
formatIDSQL = {
"fileUUID": fileUUID,
"formatName": "",
"formatVersion": "",
"formatRegistryName": "PRONOM",
"formatRegistryKey": "",
}
eventOutcomeDetailNote = getTagged(fileFormatHit, prefix + "PUID")[0].text

formatName = getTagged(fileFormatHit, prefix + "Name")
formatVersion = getTagged(fileFormatHit, prefix + "Version")

if len(formatName):
formatIDSQL["formatName"] = formatName[0].text
if len(formatVersion):
formatIDSQL["formatVersion"] = formatVersion[0].text

PUID = getTagged(fileFormatHit, prefix + "PUID")
if len(PUID):
formatIDSQL["formatRegistryKey"] = PUID[0].text
formats.append(format)
print formatIDSQL
insertIntoFilesIDs(fileUUID=fileUUID,
formatName=formatIDSQL["formatName"],
formatVersion=formatIDSQL["formatVersion"],
formatRegistryName=formatIDSQL["formatRegistryName"],
formatRegistryKey=formatIDSQL["formatRegistryKey"])
else:
eventOutcomeDetailNote = "No Matching Format Found"
formatDesignation = etree.SubElement(format, "formatDesignation")
etree.SubElement(formatDesignation, "formatName").text = "Unknown"
formats.append(format)
eventOutcomeDetailNotes.append(eventOutcomeDetailNote)
return eventDetailText, eventOutcomeText, eventOutcomeDetailNotes

def includeFits(fits, xmlFile, date, eventUUID, fileUUID):
global exitCode
#TO DO... Gleam the event outcome information from the output


# Use the FITS output to populate format identification events, as well as
# the format identification information from DROID. This information will
# be used in the METS file.
# FIXME: FITS should not be used for file identification unless it is
# selected by the user, but removing this breaks the METS output (see #6160,
# #6161).
eventDetailText, eventOutcomeText, eventOutcomeDetailNotes = formatIdentificationFITSAssist(fits, fileUUID)

for eventOutcomeDetailNote in eventOutcomeDetailNotes:
insertIntoEvents(fileUUID=fileUUID,
eventIdentifierUUID=str(uuid.uuid4()),
eventType="format identification",
eventDateTime=date,
eventDetail=eventDetailText,
eventOutcome=eventOutcomeText,
eventOutcomeDetailNote=eventOutcomeDetailNote)

try:
eventDetailText, eventOutcomeText, eventOutcomeDetailNote = formatValidationFITSAssist(fits)
except:
Expand Down
50 changes: 50 additions & 0 deletions src/MCPClient/lib/clientScripts/identifyFileFormat.py
Expand Up @@ -3,9 +3,12 @@
import argparse
import os
import sys
import uuid

sys.path.append("/usr/lib/archivematica/archivematicaCommon")
from executeOrRunSubProcess import executeOrRun
from databaseFunctions import insertIntoEvents, insertIntoFilesIDs
from databaseInterface import getUTCDate

path = '/usr/share/archivematica/dashboard'
if path not in sys.path:
Expand Down Expand Up @@ -41,6 +44,47 @@ def save_idtool(file_, value):

UnitVariable.objects.create(unituuid=unit.pk, variable='replacementDict', variablevalue=str(rd))


def write_identification_event(file_uuid, command, format=None, success=True):
event_detail_text = 'program="{}"; version="{}"'.format(
command.tool.description, command.tool.version)
if success:
event_outcome_text = "Positive"
else:
event_outcome_text = "Not identified"

if not format:
format = 'No Matching Format'

date = getUTCDate()

insertIntoEvents(fileUUID=file_uuid,
eventIdentifierUUID=str(uuid.uuid4()),
eventType="format identification",
eventDateTime=date,
eventDetail=event_detail_text,
eventOutcome=event_outcome_text,
eventOutcomeDetailNote=format)


def write_file_id(file_uuid, format=None, output=''):
if format.pronom_id:
format_registry = 'PRONOM'
key = format.pronom_id
else:
format_registry = 'Archivematica Format Policy Registry'
key = output

# Sometimes, this is null instead of an empty string
version = format.version or ''

insertIntoFilesIDs(fileUUID=file_uuid,
formatName=format.description,
formatVersion=version,
formatRegistryName=format_registry,
formatRegistryKey=key)


def main(command_uuid, file_path, file_uuid):
print "IDCommand UUID:", command_uuid
print "File: ({}) {}".format(file_uuid, file_path)
Expand Down Expand Up @@ -75,12 +119,15 @@ def main(command_uuid, file_path, file_uuid):
version = rule.format
except IDRule.DoesNotExist:
print >>sys.stderr, 'Error: No FPR identification rule for tool output "{}" found'.format(output)
write_identification_event(file_uuid, command, success=False)
return -1
except IDRule.MultipleObjectsReturned:
print >>sys.stderr, 'Error: Multiple FPR identification rules for tool output "{}" found'.format(output)
write_identification_event(file_uuid, command, success=False)
return -1
except FormatVersion.DoesNotExist:
print >>sys.stderr, 'Error: No FPR format record found for PUID {}'.format(output)
write_identification_event(file_uuid, command, success=False)
return -1

(ffv, created) = FileFormatVersion.objects.get_or_create(file_uuid=file_, defaults={'format_version': version})
Expand All @@ -89,6 +136,9 @@ def main(command_uuid, file_path, file_uuid):
ffv.save()
print "{} identified as a {}".format(file_path, version.description)

write_identification_event(file_uuid, command, format=version.pronom_id)
write_file_id(file_uuid, format=version, output=output)

return 0


Expand Down