def test_validate_serialization(self): # Test on unzipped Bag. self.assertTrue( self.profile.validate_serialization(os.path.abspath("test-bar"))) # Test on zipped Bag. self.profile = bagit_profile.Profile( 'https://raw.github.com/ruebot/bagit-profiles/master/bagProfileFoo.json' ) self.assertTrue( self.profile.validate_serialization( os.path.abspath("test-foo.zip")))
def validate_metadata(self, bag): """Validates the bag-info.txt file against the bagit profile""" new_bag = bagit.Bag(bag.bag_path) bagit_profile_json = "zorya_bagit_profile.json" with open(join(settings.BASE_DIR, "package_bag", bagit_profile_json), "r") as fp: data = json.load(fp) profile = bagit_profile.Profile(bagit_profile_json, profile=data) if not profile.validate(new_bag): raise TypeError(profile.report.errors) else: return new_bag.info
def validate_bag(target): bag = bagit.Bag(target) profile = bagit_profile.Profile( 'https://raw.githubusercontent.com/RockefellerArchiveCenter/project_electron/master/transfer/organizational-bag-profile.json' ) # Validate bag against BagIt specification using bagit library try: bag.validate() print "Bag valid according to BagIt specification" except bagit.BagValidationError, e: print "Bag invalid according to BagIt specification" print e exit()
def validate_bag_serialization(bag_path, bag_profile=None, bag_profile_path=None): if not bag_profile: if not bag_profile_path: raise bagit_profile.ProfileValidationError( "Unable to instantiate profile, no bag profile or profile path found" ) logger.info("Retrieving profile: %s" % bag_profile_path) bag_profile = bagit_profile.Profile(bag_profile_path) # Validate 'Serialization' and 'Accept-Serialization'. logger.info("Validating bag serialization: %s" % bag_path) try: bag_profile.validate_serialization(bag_path) logger.info("Bag serialization conforms to specified profile") except Exception as e: logger.error( "Bag serialization does not conform to specified profile. Error: %s" % e) raise e
def validate_bag_profile(bag_path, profile_path=None): logger.info("Validating bag profile: %s" % bag_path) bag = bagit.Bag(bag_path) # Instantiate a profile, supplying its URI. if not profile_path: profile_path = bag.info.get(bdbag.BAG_PROFILE_TAG, None) if not profile_path: raise bagit_profile.ProfileValidationError( "Bag does not contain a BagIt-Profile-Identifier") logger.info("Retrieving profile: %s" % profile_path) profile = bagit_profile.Profile(profile_path) # Validate the profile. if profile.validate(bag): logger.info("Bag structure conforms to specified profile") else: raise bagit_profile.ProfileValidationError( "Bag structure does not conform to specified profile") return profile
def setUp(self): self.bag = bagit.Bag('test-bar') self.profile = bagit_profile.Profile( 'https://raw.github.com/ruebot/bagit-profiles/master/bagProfileBar.json' ) self.retrieved_profile = self.profile.get_profile()
import bagit import bagit_profile # Instantiate an existing Bag using https://github.com/edsu/bagit. bag = bagit.Bag('test-bar') # Instantiate a profile, supplying its URI. my_profile = bagit_profile.Profile( 'https://raw.github.com/ruebot/bagit-profiles/master/bagProfileBar.json') # Validate 'Serialization' and 'Accept-Serialization'. This must be done # before .validate(bag) is called. 'mydir' is the path to the Bag. if my_profile.validate_serialization('test-bar'): print "Serialization validates" else: print "Serialization does not validate" # Validate the rest of the profile. if my_profile.validate(bag): print "Validates" else: print "Does not validate"
def _is_rac_bag(self): """Assumes a valid bag/bag info; returns true if passes rac profile""" if "BagIt_Profile_Identifier" not in self.bag_info_data: self.bag_exception = "No BagIt Profile to validate against" return False else: try: profile = bagit_profile.Profile( self.bag_info_data["BagIt_Profile_Identifier"] ) except BaseException: self.bag_exception = "Cannot retrieve BagIt Profile from URL {}".format( self.bag_info_data["BagIt_Profile_Identifier"] ) return False else: # RE IMPLEMENTING validate() SINCE VALIDATION MESSAGES ARE PRINTED # https://github.com/ruebot/bagit-profiles-validator/blob/master/bagit_profile.py # line 76 try: profile.validate_bag_info(self.bag) except Exception as e: self.bag_exception = "Error in bag-info.txt: {}".format(e.value) return False try: profile.validate_payload_manifests_allowed(self.bag) except Exception as e: self.bag_exception = "An unallowed manifest was found: {}".format( e.value ) return False try: profile.validate_manifests_required(self.bag) except Exception as e: self.bag_exception = "Required manifests not found: {}".format( e.value ) return False try: profile.validate_tag_manifests_required(self.bag) except Exception as e: self.bag_exception = "Required tag manifests not found: {}".format( e.value ) return False try: profile.validate_tag_files_required(self.bag) except Exception as e: self.bag_exception = "Required tag files not found: {}".format( e.value ) return False try: profile.validate_allow_fetch(self.bag) except Exception as e: self.bag_exception = "fetch.txt is present but is not allowed: {}".format( e.value ) return False try: profile.validate_accept_bagit_version(self.bag) except Exception as e: self.bag_exception = "Required BagIt version not found: {}".format( e.value ) return False return True return False