Exemplo n.º 1
0
 def test_revert_bag(self):
     logger.info(self.getTestHeader('revert bag'))
     try:
         bdb.revert_bag(self.test_bag_dir)
         self.assertFalse(ospif(ospj(self.test_bag_dir, 'bag-info.txt')))
         self.assertFalse(ospif(ospj(self.test_bag_dir, 'bagit.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'manifest-sha1.txt')))
         self.assertFalse(ospif(ospj(self.test_bag_dir,
                                     'manifest-md5.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'manifest-sha1.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'manifest-sha256.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'manifest-sha512.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'tagmanifest-md5.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'tagmanifest-sha1.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'tagmanifest-sha256.txt')))
         self.assertFalse(
             ospif(ospj(self.test_bag_dir, 'tagmanifest-sha512.txt')))
         self.assertTrue(ospif(ospj(self.test_bag_dir, 'README.txt')))
         self.assertTrue(
             ospif(ospj(self.test_bag_dir, ospj('test1', 'test1.txt'))))
         self.assertTrue(
             ospif(ospj(self.test_bag_dir, ospj('test2', 'test2.txt'))))
         self.assertFalse(ospe(ospj(self.test_bag_dir, 'data')))
     except Exception as e:
         self.fail(bdbag.get_named_exception(e))
Exemplo n.º 2
0
 def test_revert_non_bag(self):
     logger.info(self.getTestHeader('revert bag'))
     try:
         bdb.revert_bag(self.test_data_dir)
         output = self.stream.getvalue()
         self.assertExpectedMessages(["Cannot revert the bag %s because it is not a bag directory!"], output)
     except Exception as e:
         self.fail(get_typed_exception(e))
Exemplo n.º 3
0
def main():

    args, is_bag, is_file = parse_cli()
    path = os.path.abspath(args.path)

    archive = None
    temp_path = None
    error = None
    result = 0

    if not args.quiet:
        sys.stderr.write('\n')

    try:
        if not is_file:
            # do not try to create or update the bag if the user just wants to validate or complete an existing bag
            if not (
                (args.validate or args.validate_profile or args.resolve_fetch)
                    and not (args.update and bdb.is_bag(path))):
                if args.checksum and 'all' in args.checksum:
                    args.checksum = ['md5', 'sha1', 'sha256', 'sha512']
                # create or update the bag depending on the input arguments
                bdb.make_bag(path,
                             algs=args.checksum,
                             update=args.update,
                             save_manifests=not args.skip_manifests,
                             prune_manifests=args.prune_manifests,
                             metadata=BAG_METADATA if BAG_METADATA else None,
                             metadata_file=args.metadata_file,
                             remote_file_manifest=args.remote_file_manifest,
                             config_file=args.config_file,
                             ro_metadata_file=args.ro_metadata_file)

        # otherwise just extract the bag if it is an archive and no other conflicting options specified
        elif not (args.validate or args.validate_profile
                  or args.resolve_fetch):
            bdb.extract_bag(path)
            if not args.quiet:
                sys.stderr.write('\n')
            return result

        if args.ro_manifest_generate:
            bdb.generate_ro_manifest(
                path,
                True if args.ro_manifest_generate == "overwrite" else False,
                config_file=args.config_file)

        if args.resolve_fetch:
            if args.validate == 'full':
                sys.stderr.write(ASYNC_TRANSFER_VALIDATION_WARNING)
            bdb.resolve_fetch(
                path,
                force=True if args.resolve_fetch == 'all' else False,
                keychain_file=args.keychain_file,
                config_file=args.config_file,
                filter_expr=args.fetch_filter)

        if args.validate:
            if is_file:
                temp_path = bdb.extract_bag(path, temp=True)
            if args.validate == 'structure':
                bdb.validate_bag_structure(temp_path if temp_path else path)
            else:
                bdb.validate_bag(
                    temp_path if temp_path else path,
                    fast=True if args.validate == 'fast' else False,
                    config_file=args.config_file)

        if args.archiver:
            archive = bdb.archive_bag(path, args.archiver)

        if archive is None and is_file:
            archive = path

        if args.validate_profile:
            if is_file:
                if not temp_path:
                    temp_path = bdb.extract_bag(path, temp=True)
            profile = bdb.validate_bag_profile(
                temp_path if temp_path else path)
            bdb.validate_bag_serialization(archive if archive else path,
                                           profile)

        if args.revert:
            bdb.revert_bag(path)

    except Exception as e:
        result = 1
        error = "Error: %s" % get_typed_exception(e)

    finally:
        if temp_path:
            bdb.cleanup_bag(os.path.dirname(temp_path))
        if result != 0:
            sys.stderr.write("\n%s" % error)

    if not args.quiet:
        sys.stderr.write('\n')

    return result