-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an activity to merge AIS metadata files
Fixes #77. Concatenate the Arelda metadata file from the original package and the METS file created by Archivematica into a single "AIS" metadata file.
- Loading branch information
Showing
6 changed files
with
1,051 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package ais | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/antchfx/xmlquery" | ||
|
||
"github.com/artefactual-sdps/preprocessing-sfa/internal/fsutil" | ||
) | ||
|
||
const CombineMDActivityName = "combine-metadata-files" | ||
|
||
type ( | ||
CombineMDActivity struct{} | ||
CombineMDActivityParams struct { | ||
AreldaRelPath string | ||
METSRelPath string | ||
WorkingDir string | ||
} | ||
CombineMDActivityResult struct { | ||
Path string | ||
} | ||
) | ||
|
||
func NewCombineMDActivity() *CombineMDActivity { | ||
return &CombineMDActivity{} | ||
} | ||
|
||
func (a *CombineMDActivity) Execute( | ||
ctx context.Context, | ||
params CombineMDActivityParams, | ||
) (*CombineMDActivityResult, error) { | ||
areldaPath := filepath.Join(params.WorkingDir, params.AreldaRelPath) | ||
if !fsutil.FileExists(areldaPath) { | ||
return nil, fmt.Errorf("missing Arelda file: %s", areldaPath) | ||
} | ||
|
||
metsPath := filepath.Join(params.WorkingDir, params.METSRelPath) | ||
if !fsutil.FileExists(metsPath) { | ||
return nil, fmt.Errorf("missing METS file: %s", metsPath) | ||
} | ||
|
||
aisName, err := aisFilename(areldaPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("name AIS file: %v", err) | ||
} | ||
|
||
dest := filepath.Join(params.WorkingDir, aisName) | ||
|
||
// Combine metadata files into AIS file. | ||
w, err := os.Create(dest) // #nosec G304 -- generated path. | ||
if err != nil { | ||
return nil, fmt.Errorf("create AIS file: %v", err) | ||
} | ||
defer w.Close() | ||
|
||
if err = concat(w, filepath.Join(areldaPath), filepath.Join(metsPath)); err != nil { | ||
return nil, fmt.Errorf("concat: %v", err) | ||
} | ||
|
||
// Delete original metadata files. | ||
if err = removePaths(areldaPath, metsPath); err != nil { | ||
return nil, fmt.Errorf("removePaths: %v", err) | ||
} | ||
|
||
return &CombineMDActivityResult{Path: dest}, nil | ||
} | ||
|
||
func aisFilename(mdpath string) (string, error) { | ||
id, err := parseAccessionID(mdpath) | ||
if err != nil { | ||
return "", fmt.Errorf("get accession number: %v", err) | ||
} | ||
|
||
id = strings.ReplaceAll(id, "/", "_") | ||
|
||
return fmt.Sprintf("AIS_%s", id), nil | ||
} | ||
|
||
func parseAccessionID(path string) (string, error) { | ||
f, err := os.Open(path) // #nosec G304 -- trusted path. | ||
if err != nil { | ||
return "", fmt.Errorf("open metadata file: %v", err) | ||
} | ||
defer f.Close() | ||
|
||
sp, err := xmlquery.CreateStreamParser(f, "//paket/ablieferung/ablieferungsnummer") | ||
if err != nil { | ||
return "", fmt.Errorf("create XML parser: %v", err) | ||
} | ||
|
||
n, err := sp.Read() | ||
if err == io.EOF { | ||
return "", fmt.Errorf("can't find ablieferungsnummer in %q", filepath.Base(path)) | ||
} | ||
if err != nil { | ||
return "", fmt.Errorf("read XML stream: %v", err) | ||
} | ||
return n.InnerText(), nil | ||
} | ||
|
||
func concat(w io.Writer, paths ...string) error { | ||
for i := range paths { | ||
r, err := os.Open(paths[i]) // #nosec G304 -- trusted path. | ||
if err != nil { | ||
return fmt.Errorf("read: %v", err) | ||
} | ||
defer r.Close() | ||
|
||
if _, err := io.Copy(w, r); err != nil { | ||
return fmt.Errorf("copy: %v", err) | ||
} | ||
_ = r.Close() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func removePaths(paths ...string) error { | ||
var err error | ||
for i := range paths { | ||
if e := os.Remove(paths[i]); e != nil { | ||
err = errors.Join(err, fmt.Errorf("remove: %v", e)) | ||
} | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package ais_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
temporalsdk_activity "go.temporal.io/sdk/activity" | ||
temporalsdk_testsuite "go.temporal.io/sdk/testsuite" | ||
"gotest.tools/v3/assert" | ||
"gotest.tools/v3/fs" | ||
|
||
"github.com/artefactual-sdps/preprocessing-sfa/internal/ais" | ||
) | ||
|
||
const ( | ||
arelda = `<?xml version="1.0" encoding="UTF-8"?> | ||
<paket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:xip="http://www.tessella.com/XIP/v4" | ||
xmlns="http://bar.admin.ch/arelda/v4" | ||
xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
xmlns:submissionTests="http://bar.admin.ch/submissionTestResult" | ||
xsi:type="paketAIP" | ||
schemaVersion="5.0"> | ||
<ablieferung xsi:type="ablieferungFilesAIP"> | ||
<ablieferungstyp>FILES</ablieferungstyp> | ||
<ablieferndeStelle>Bundesverwaltung (Bern)</ablieferndeStelle> | ||
<ablieferungsnummer>1000/893_3251903</ablieferungsnummer> | ||
</ablieferung> | ||
</packet> | ||
` | ||
|
||
mets = `<?xml version='1.0' encoding='UTF-8'?> | ||
<mets:mets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mets="http://www.loc.gov/METS/" xsi:schemaLocation="http://www.loc.gov/METS/ http://www.loc.gov/standards/mets/version1121/mets.xsd"> | ||
</mets:mets> | ||
` | ||
) | ||
|
||
func testDir(t *testing.T) string { | ||
td := fs.NewDir(t, "ppsfa", | ||
fs.WithFile("arelda.xml", arelda), | ||
fs.WithFile("mets.xml", mets), | ||
) | ||
|
||
return td.Path() | ||
} | ||
|
||
func TestExecute(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
params ais.CombineMDActivityParams | ||
want ais.CombineMDActivityResult | ||
wantErr string | ||
wantManifest fs.Manifest | ||
}{ | ||
{ | ||
name: "Returns the combined metadata", | ||
params: ais.CombineMDActivityParams{ | ||
AreldaRelPath: "arelda.xml", | ||
METSRelPath: "mets.xml", | ||
WorkingDir: testDir(t), | ||
}, | ||
want: ais.CombineMDActivityResult{Path: "{{wd}}/AIS_1000_893_3251903"}, | ||
wantManifest: fs.Expected(t, | ||
fs.WithFile("AIS_1000_893_3251903", arelda+mets, fs.WithMode(0o664)), | ||
), | ||
}, | ||
{ | ||
name: "Errors if the Arelda file doesn't exist", | ||
params: ais.CombineMDActivityParams{ | ||
AreldaRelPath: "missing.xml", | ||
WorkingDir: testDir(t), | ||
}, | ||
wantErr: "activity error (type: combine-metadata-files, scheduledEventID: 0, startedEventID: 0, identity: ): missing Arelda file: {{wd}}/missing.xml", | ||
}, | ||
{ | ||
name: "Errors if the METS file doesn't exist", | ||
params: ais.CombineMDActivityParams{ | ||
AreldaRelPath: "arelda.xml", | ||
METSRelPath: "missing.xml", | ||
WorkingDir: testDir(t), | ||
}, | ||
wantErr: "activity error (type: combine-metadata-files, scheduledEventID: 0, startedEventID: 0, identity: ): missing METS file: {{wd}}/missing.xml", | ||
}, | ||
{ | ||
name: "Errors when the Arelda file is invalid", | ||
params: ais.CombineMDActivityParams{ | ||
AreldaRelPath: "mets.xml", | ||
WorkingDir: testDir(t), | ||
}, | ||
wantErr: "activity error (type: combine-metadata-files, scheduledEventID: 0, startedEventID: 0, identity: ): name AIS file: get accession number: can't find ablieferungsnummer in \"mets.xml\"", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ts := &temporalsdk_testsuite.WorkflowTestSuite{} | ||
env := ts.NewTestActivityEnvironment() | ||
env.RegisterActivityWithOptions( | ||
ais.NewCombineMDActivity().Execute, | ||
temporalsdk_activity.RegisterOptions{Name: ais.CombineMDActivityName}, | ||
) | ||
|
||
tt.want.Path = strings.ReplaceAll(tt.want.Path, "{{wd}}", tt.params.WorkingDir) | ||
tt.wantErr = strings.ReplaceAll(tt.wantErr, "{{wd}}", tt.params.WorkingDir) | ||
|
||
future, err := env.ExecuteActivity(ais.CombineMDActivityName, tt.params) | ||
if tt.wantErr != "" { | ||
if err == nil { | ||
t.Errorf("error is nil, expecting: %q", tt.wantErr) | ||
} else { | ||
assert.ErrorContains(t, err, tt.wantErr) | ||
} | ||
|
||
return | ||
} | ||
assert.NilError(t, err) | ||
|
||
var got ais.CombineMDActivityResult | ||
future.Get(&got) | ||
assert.DeepEqual(t, got, tt.want) | ||
assert.Assert(t, fs.Equal(tt.params.WorkingDir, tt.wantManifest)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.