def do_object_operation(args): """Implement object operations in a way that can be reused by ocfl-store.py.""" obj = ocfl.Object(identifier=args.id, digest_algorithm=args.digest, filepath_normalization=args.normalization, skips=args.skip, forward_delta=not args.no_forward_delta, dedupe=not args.no_dedupe, ocfl_version=args.ocfl_version, fixity=args.fixity) if args.create: if args.srcdir is None: raise Exception( "Must specify --srcdir containing v1 files when creating an OCFL object!" ) metadata = ocfl.VersionMetadata(args) obj.create(srcdir=args.srcdir, metadata=metadata, objdir=args.objdir) elif args.build: if args.srcdir is None: raise Exception( "Must specify --srcdir containing version directories when building an OCFL object!" ) metadata = ocfl.VersionMetadata(args) obj.build(srcdir=args.srcdir, metadata=metadata, objdir=args.objdir) elif args.show: obj.show(path=args.objdir) elif args.validate: obj.validate(path=args.objdir) elif args.extract: obj.extract(objdir=args.objdir, version=args.extract, dstdir=args.dstdir) else: raise Exception("Command argument not supported!")
def migrate(pid, storage_root): obj_root = os.path.join(storage_root, pid) if os.path.exists(obj_root): raise MigrationError(f'{obj_root} already exists') ocfl_obj = ocfl.Object(identifier=pid) version_metadata = ocfl.version.VersionMetadata() with tempfile.TemporaryDirectory() as src_dir: _get_fedora3_datastreams(src_dir, pid) ocfl_obj.create(srcdir=src_dir, metadata=version_metadata, objdir=obj_root)
def create_sidecar(dir): """Create sidecar for inventory in dir.""" inventory_path = os.path.join(dir, INVENTORY_NAME) if not os.path.isfile(inventory_path): logging.error("Ignoring path %s because there is not file %s." % (dir, inventory_path)) else: object = ocfl.Object() if args.digest is not None: object.digest_algorithm = args.digest else: # Read inventory in the hope of setting digest_algoritm try: object.parse_inventory(dir) except ocfl.ObjectException as e: logging.warning( "Failed to read inventory in directory %s (%s)" % (dir, str(e))) sidecar = object.write_inventory_sidecar(dir) logging.info("Written sidecar file %s" % (sidecar))
def create_sidecar(args, directory): """Create sidecar for inventory in dir.""" inventory_path = os.path.join(directory, INVENTORY_NAME) if not os.path.isfile(inventory_path): logging.error( "Ignoring path %s because there is no inventory file %s.", directory, inventory_path) else: obj = ocfl.Object(path=directory) if args.digest is not None: obj.digest_algorithm = args.digest else: # Read inventory in the hope of setting digest_algoritm try: obj.parse_inventory() except ocfl.ObjectException as e: logging.warning( "Failed to read inventory in directory %s (%s)", directory, e) sidecar = obj.write_inventory_sidecar() logging.info("Written sidecar file %s", sidecar)
def do_object_operation(args): """Implement object operations in a way that can be reused by ocfl-store.py.""" obj = ocfl.Object(identifier=args.id, digest_algorithm=args.digest, filepath_normalization=args.normalization, forward_delta=not args.no_forward_delta, dedupe=not args.no_dedupe, lax_digests=args.lax_digests, fixity=args.fixity) if args.create: srcdir = args.srcdir metadata = ocfl.VersionMetadata(args=args) if args.srcbag is not None: srcdir = ocfl.bag_as_source(args.srcbag, metadata) if metadata.id is not None: if obj.id: if obj.id != metadata.id: raise FatalError( "Identifier specified (%s) and identifier from Bagit bag (%s) do not match!" % (obj.id, metadata.id)) else: obj.id = metadata.id elif args.srcdir is None: raise FatalError( "Must specify either --srcdir or --srcbag containing v1 files when creating an OCFL object!" ) obj.create(srcdir=srcdir, metadata=metadata, objdir=args.objdir) elif args.build: if args.srcdir is None: raise FatalError( "Must specify --srcdir containing version directories when building an OCFL object!" ) metadata = ocfl.VersionMetadata(args=args) obj.build(srcdir=args.srcdir, metadata=metadata, objdir=args.objdir) elif args.update: srcdir = args.srcdir metadata = ocfl.VersionMetadata(args=args) if args.srcbag is not None: srcdir = ocfl.bag_as_source(args.srcbag, metadata) elif args.srcdir is None: raise FatalError( "Must specify either --srcdir or --srcbag containing new version files when updating an OCFL object!" ) obj.update(objdir=args.objdir, srcdir=srcdir, metadata=metadata) elif args.show: obj.show(objdir=args.objdir) elif args.validate: obj.validate(objdir=args.objdir) elif args.extract: if args.dstdir and args.dstbag: args.dstdir = None # Override dstdir if dstbag specified version = args.extract dst = args.dstdir or args.dstbag metadata = obj.extract(objdir=args.objdir, version=version, dstdir=dst) if args.dstdir: print("Extracted content for %s in %s" % (version, dst)) else: # args.dstbag ocfl.bag_extracted_version(dst, metadata) print("Extracted content for %s saved as Bagit bag in %s" % (version, dst)) else: raise FatalError("Command argument not supported!")
store.initialize() elif args.list: store.list() elif args.validate: store.validate(show_warnings=not args.quiet) elif args.add: if not args.src: raise ocfl.StoreException("Must specify object path with --src") store.add(object_path=args.src) elif args.purge: logging.error("purge not implemented") elif args.show_object or args.validate_object: if not args.id: raise ocfl.StoreException( "Must specify id to act on an object in the store") objdir = store.object_path(args.id) obj = ocfl.Object(identifier=args.id, digest_algorithm=args.digest, forward_delta=not args.no_forward_delta, dedupe=not args.no_dedupe, fixity=args.fixity) if args.show_object: obj.show(objdir) else: logging.error("create/build/validate not implemented") else: logging.warning("No command, nothing to do.") except (ocfl.StoreException, ocfl.ObjectException) as e: logging.error(str(e)) sys.exit(1)
log = logging.getLogger(name="ocfl-validate") log.setLevel(level=logging.INFO if args.verbose else logging.WARN) if len(args.path) == 0: print("No OCFL paths specified, nothing to do! (Use -h for help)") num = 0 num_good = 0 num_paths = len(args.path) show_warnings = not args.quiet and not args.very_quiet for path in args.path: num += 1 path_type = ocfl.find_path_type(path) if path_type == 'object': log.info("Validating OCFL Object at %s", path) obj = ocfl.Object(lax_digests=args.lax_digests) if obj.validate(path, show_warnings=show_warnings, show_errors=not args.very_quiet, check_digests=not args.no_check_digests): num_good += 1 elif path_type == 'root': log.info("Validating OCFL Storage Root at %s", path) store = ocfl.Store(root=path, lax_digests=args.lax_digests) if store.validate(show_warnings=show_warnings, show_errors=not args.very_quiet, check_digests=not args.no_check_digests): num_good += 1 elif path_type == 'file': log.info("Validating separate OCFL Inventory at %s", path) obj = ocfl.Object(lax_digests=args.lax_digests)
#!/usr/bin/env python """Validate an OCFL Object.""" import argparse import ocfl parser = argparse.ArgumentParser( description='Validate one or more OCFL objects.') parser.add_argument('objdir', type=str, nargs=1, help='OCFL object path') args = parser.parse_args() ocfl = ocfl.Object() for objdir in args.objdir: ocfl.validate(objdir)
store.list() elif args.validate: store.validate(show_warnings=not args.quiet) elif args.add: if not args.src: raise ocfl.StoreException("Must specify object path with --src") store.add(object_path=args.src) elif args.purge: logging.error("purge not implemented") elif args.show_object or args.validate_object: if not args.id: raise ocfl.StoreException( "Must specify id to act on an object in the store") objdir = store.object_path(args.id) obj = ocfl.Object(identifier=args.id, digest_algorithm=args.digest, forward_delta=not args.no_forward_delta, dedupe=not args.no_dedupe, skips=args.skip, ocfl_version=args.ocfl_version, fixity=args.fixity) if args.show_object: obj.show(objdir) else: logging.error("create/build/validate not implemented") else: logging.warn("Nuttin' happenin' 'round 'ere.") except (ocfl.StoreException, ocfl.ObjectException) as e: logging.error(str(e)) sys.exit(1)