-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI tool to import AIP from METS file (#355)
Added a CLI tool to import AIP-related data from a METS file.
- Loading branch information
Showing
4 changed files
with
180 additions
and
67 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
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,84 @@ | ||
#!/usr/bin/env python3 | ||
import logging | ||
import os | ||
import pathlib | ||
import sys | ||
import uuid | ||
from datetime import datetime | ||
|
||
import click | ||
from app import cli | ||
|
||
from AIPscan import db | ||
from AIPscan.Aggregator import database_helpers | ||
from AIPscan.Aggregator.mets_parse_helpers import import_from_mets | ||
from config import CONFIGS | ||
|
||
|
||
@click.command() | ||
@click.option("--ss-id", "-s", required=True, help="Storage service ID.", type=int) | ||
@click.option( | ||
"--location-id", "-l", required=True, help="Storage location ID.", type=int | ||
) | ||
@click.option("--aip-size", "-a", required=True, help="AIP size.", type=int) | ||
@click.option( | ||
"--origin-pipeline-id", "-o", required=True, help="Origin pipeline UUID.", type=str | ||
) | ||
@click.option("--package-uuid", "-u", help="Package UUID.", type=str) | ||
@click.option("--verbose", "-v", is_flag=True, help="Show debug messages.", type=bool) | ||
@click.argument("filename") | ||
def main(ss_id, location_id, aip_size, origin_pipeline_id, package_uuid, verbose, filename): | ||
# Check if METS file exists | ||
if not pathlib.Path(filename).exists(): | ||
cli.raise_click_error("METS file does not exist.") | ||
|
||
# Log to screen | ||
logger_name = pathlib.PurePosixPath(sys.argv[0]).name | ||
logger = logging.getLogger(logger_name) | ||
|
||
if verbose: | ||
logging.basicConfig(level=logging.DEBUG) | ||
else: | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
# Try to parse package UUID from METS filename, if not specified | ||
if not package_uuid: | ||
try: | ||
package_uuid = str(uuid.UUID(os.path.basename(filename)[5:41])) | ||
logger.info(f"Parsed UUID {package_uuid} from filename") | ||
except ValueError: | ||
cli.raise_click_error("No package UUID in filename.") | ||
|
||
# Initialize Flask app context | ||
app = cli.create_app_instance(CONFIGS[cli.config_name], db) | ||
|
||
with app.app_context(): | ||
# Create a fetch_job and take note of its ID | ||
datetime_obj_start = datetime.now().replace(microsecond=0) | ||
session_id = str(uuid.uuid4()) | ||
|
||
fetch_job = database_helpers.create_fetch_job( | ||
datetime_obj_start, session_id, ss_id | ||
) | ||
fetch_job_id = fetch_job.id | ||
|
||
# Import METS file | ||
logger.info("Importing...") | ||
|
||
import_from_mets( | ||
filename, | ||
aip_size, | ||
package_uuid, | ||
ss_id, | ||
location_id, | ||
fetch_job_id, | ||
origin_pipeline_id, | ||
logger, | ||
delete_file=False, | ||
) | ||
|
||
logger.info("Done.") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |