|
| 1 | +#! /usr/bin/python3 |
| 2 | +"""Ingest metadata into ICAT. |
| 3 | +
|
| 4 | +This scripts demonstrates how to use class IngestReader from the |
| 5 | +icat.ingest module to read metadata from a file and add that to ICAT. |
| 6 | +The script intents to model the use case of ingesting raw datasets |
| 7 | +from the experiment. |
| 8 | +
|
| 9 | +The script expects an input directory containing one metadata input |
| 10 | +file and one or more subdirectories for each dataset respectively, |
| 11 | +e.g. something like:: |
| 12 | +
|
| 13 | + input_dir |
| 14 | + ├── metadata.xml |
| 15 | + ├── dataset_1 |
| 16 | + │ ├── datafile_a.dat |
| 17 | + │ ├── datafile_b.dat |
| 18 | + │ └── datafile_c.dat |
| 19 | + └── dataset_2 |
| 20 | + ├── datafile_d.dat |
| 21 | + ├── datafile_e.dat |
| 22 | + └── datafile_f.dat |
| 23 | +
|
| 24 | +The script takes the name of an investigation as argument. The |
| 25 | +investigation MUST exist in ICAT beforehand and all datasets in the |
| 26 | +input directory MUST belong to this investigation. The script will |
| 27 | +create tha datasets in ICAT, e.g. they MUST NOT exist in ICAT |
| 28 | +beforehand. The metadata input file may contain attributes and |
| 29 | +related objects (datasetInstrument, datasetTechnique, |
| 30 | +datasetParameter) for the datasets provided in the input directory. |
| 31 | +The metadata input is restricted in that sense, e.g. this script |
| 32 | +enforces that the metadata does not contain any other input. |
| 33 | +
|
| 34 | +The XML Schema Definition and XSL Transformation files (ingest.xsd and |
| 35 | +ingest.xslt) provided by python-icat (or customized versions thereof) |
| 36 | +need to be installed so that class IngestReader will find them |
| 37 | +(e.g. in the IngestReader.SchemaDir directory). |
| 38 | +
|
| 39 | +There are some limitations to keep things simple: |
| 40 | +
|
| 41 | +* the script creates the dataset and datafile objects in ICAT, but |
| 42 | + does not upload the file content to IDS. In a real production |
| 43 | + workflow, you'd probably have a separate step that copies the files |
| 44 | + to the storage managed by IDS while creating the dataset and |
| 45 | + datafile objects in ICAT at the same time. |
| 46 | +
|
| 47 | +* the script does not care to add a datafileFormat or any descriptive |
| 48 | + attributes (fileSize, checksum, datafileModTime) to the datafiles it |
| 49 | + creates. |
| 50 | +
|
| 51 | +* it is assumed that the investigation can be unambiguously found by |
| 52 | + its name. |
| 53 | +
|
| 54 | +* a real production workflow would probably apply much stricter |
| 55 | + conformance checks on the input (e.g. restrictions on allowed |
| 56 | + dataset or datafile names, make sure not to follow any symlinks from |
| 57 | + the input directory) and have a more elaborated error handling. |
| 58 | +
|
| 59 | +""" |
| 60 | + |
| 61 | +import logging |
| 62 | +from pathlib import Path |
| 63 | +import icat |
| 64 | +import icat.config |
| 65 | +from icat.ingest import IngestReader |
| 66 | +from icat.query import Query |
| 67 | + |
| 68 | + |
| 69 | +logging.basicConfig(level=logging.DEBUG) |
| 70 | +# Silence some rather chatty modules. |
| 71 | +logging.getLogger('suds.client').setLevel(logging.CRITICAL) |
| 72 | +logging.getLogger('suds').setLevel(logging.ERROR) |
| 73 | + |
| 74 | +logger = logging.getLogger(__name__) |
| 75 | + |
| 76 | + |
| 77 | +config = icat.config.Config(ids=False) |
| 78 | +config.add_variable('investigation', ("investigation",), |
| 79 | + dict(help="name of the investigation")) |
| 80 | +config.add_variable('inputdir', ("inputdir",), |
| 81 | + dict(help="path to the input directory"), |
| 82 | + type=Path) |
| 83 | +client, conf = config.getconfig() |
| 84 | +client.login(conf.auth, conf.credentials) |
| 85 | + |
| 86 | +query = Query(client, "Investigation", conditions={ |
| 87 | + "name": "= '%s'" % conf.investigation |
| 88 | +}) |
| 89 | +investigation = client.assertedSearch(query)[0] |
| 90 | + |
| 91 | + |
| 92 | +class ContentError(RuntimeError): |
| 93 | + """Some invalid content in the input directory. |
| 94 | + """ |
| 95 | + def __init__(self, base, p, msg): |
| 96 | + p = p.relative_to(base) |
| 97 | + super().__init__("%s: %s" % (p, msg)) |
| 98 | + |
| 99 | + |
| 100 | +def check(client, path, investigation): |
| 101 | + """Verify the content of the input directory. |
| 102 | +
|
| 103 | + The idea is to check the input directory for conformance as much |
| 104 | + as possible and to fail early if anything is not as required, |
| 105 | + before having committed anything to ICAT. |
| 106 | +
|
| 107 | + Returns a tuple with two items: a list of datasets and an |
| 108 | + IngestReader. |
| 109 | + """ |
| 110 | + datasets = [] |
| 111 | + metadata_path = path / "metadata.xml" |
| 112 | + for p0 in path.iterdir(): |
| 113 | + if p0.name.startswith('.') or p0 == metadata_path: |
| 114 | + continue |
| 115 | + elif p0.is_dir(): |
| 116 | + is_empty = True |
| 117 | + dataset = client.new("dataset") |
| 118 | + dataset.name = p0.name |
| 119 | + dataset.complete = False |
| 120 | + for p1 in p0.iterdir(): |
| 121 | + if p1.is_file(): |
| 122 | + is_empty = False |
| 123 | + datafile = client.new("datafile") |
| 124 | + datafile.name = p1.name |
| 125 | + dataset.datafiles.append(datafile) |
| 126 | + else: |
| 127 | + raise ContentError(path, p1, 'unexpected item') |
| 128 | + if is_empty: |
| 129 | + raise ContentError(path, p0, 'empty dataset directory') |
| 130 | + datasets.append(dataset) |
| 131 | + else: |
| 132 | + raise ContentError(path, p0, 'unexpected item') |
| 133 | + try: |
| 134 | + reader = IngestReader(client, metadata_path, investigation) |
| 135 | + reader.ingest(datasets, dry_run=True, update_ds=True) |
| 136 | + except (icat.InvalidIngestFileError, icat.SearchResultError) as e: |
| 137 | + raise ContentError(path, metadata_path, |
| 138 | + "%s: %s" % (type(e).__name__, e)) |
| 139 | + return (datasets, reader) |
| 140 | + |
| 141 | +logger.info("ingesting from directory %s into investigation %s", |
| 142 | + conf.inputdir, investigation.name) |
| 143 | +datasets, reader = check(client, conf.inputdir, investigation) |
| 144 | +logger.debug("input directory checked, found %d datasets", len(datasets)) |
| 145 | +for ds in datasets: |
| 146 | + ds.create() |
| 147 | + ds.truncateRelations(keepInstRel=True) |
| 148 | + logger.debug("created dataset %s", ds.name) |
| 149 | +reader.ingest(datasets) |
| 150 | +for ds in datasets: |
| 151 | + ds.complete = True |
| 152 | + ds.update() |
| 153 | +logger.debug("ingest done") |
0 commit comments