Exemple #1
0
    def test_json_schema__raises_error__when_version_from_path_is_not_found(
            self):
        # given
        schema_path = 'type/project/project'
        schema_json = load_json(f'{self.test_dir}/files/project.json')

        # when/then
        with self.assertRaises(MetadataSchemaError):
            schema = MetadataSchema(schema_json, schema_path)
            schema.get_json_schema({'version_numbers': {}},
                                   self.schema_base_url)
def _process_directory(repo,
                       branch_name,
                       base_server_path,
                       server_path,
                       version_numbers,
                       context,
                       dryrun=False):
    print("Processing " + server_path + " in " + branch_name + " branch of " +
          repo.name)
    created_list = []
    contents = repo.get_dir_contents(server_path, branch_name)
    for content in contents:
        if content.type == 'dir':
            created_list.extend(
                _process_directory(repo, branch_name, base_server_path,
                                   content.path, version_numbers, context,
                                   dryrun))
        else:
            try:
                path = content.path
                file_root, file_extension = os.path.splitext(path)
                if file_extension == '.json' and not path.endswith(
                        'versions.json'):
                    print("- processing: " + path)
                    file_content = repo.get_contents(path, branch_name)
                    data = base64.b64decode(file_content.content)
                    json_data = json.loads(data.decode('utf8'))
                    relative_path = path.replace(base_server_path + "/", "")
                    relative_path = relative_path.replace(".json", "")
                    key = None

                    if relative_path in UNVERSIONED_FILES:
                        key = relative_path
                    else:
                        schema_base_url = SCHEMA_URL.get(branch_name)
                        metadata_schema = MetadataSchema(
                            json_data, relative_path)
                        json_data = metadata_schema.get_json_schema(
                            version_numbers, schema_base_url)
                        key = get_relative_url(relative_path, version_numbers)

                    if key is None:
                        print("- could not find key for: " + path)
                    else:
                        created = _upload(key, branch_name, json_data, context,
                                          dryrun)
                        if created:
                            created_list.append(key)
                else:
                    print("- skipping: " + path)
            except (GithubException, IOError) as e:
                print('Error processing %s: %s', content.path, e)
    return created_list
Exemple #3
0
    def test_json_schema__returns_dict_with_schema_urls_in_id_and_refs__when_version_from_path_is_found(
            self):
        # given
        schema_path = 'type/project/project'
        schema_json = load_json(f'{self.test_dir}/files/project.json')

        # when
        schema = MetadataSchema(schema_json, schema_path)

        # then
        expected = load_json(f'{self.test_dir}/files/project-schema.json')
        self.assertEqual(
            schema.get_json_schema(self.version_map, self.schema_base_url),
            expected)
Exemple #4
0
    def test_update_refs_urls__raises_error__when_not_all_ref_versions_found(
            self):
        # given
        schema_json = {
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'properties': {
                'provenance': {
                    'description':
                    'Provenance information provided by the system.',
                    'type': 'object',
                    '$ref': 'system/provenance.json'
                },
            }
        }
        version_map = {
            'version_numbers': {
                'type': {
                    'biomaterial': {
                        'donor': '1.2.3'
                    }
                },
            }
        }

        schema_path = 'type/biomaterial/donor'

        # when / then
        with self.assertRaises(MetadataSchemaError):
            MetadataSchema(schema_json, schema_path).update_refs_urls(
                version_map, self.schema_base_url)
Exemple #5
0
    def test_get_relative_url__returns_path__id_is_found(self):
        # given
        schema_json = {'$schema': 'http://json-schema.org/draft-07/schema#'}
        version_map = {
            'version_numbers': {
                'type': {
                    'biomaterial': {
                        'donor': '1.2.3'
                    }
                }
            }
        }
        schema_path = 'type/biomaterial/donor'
        # when
        metadata_schema = MetadataSchema(schema_json, schema_path)
        json_schema = metadata_schema.get_json_schema(version_map,
                                                      self.schema_base_url)

        # then
        self.assertEqual(get_relative_url(schema_path, version_map),
                         'type/biomaterial/1.2.3/donor')
Exemple #6
0
 def test_insert_id_url__inserts_old_id_key__when_draft_04(self):
     # given
     schema_json = {
         '$schema': 'http://json-schema.org/draft-04/schema#',
     }
     version_map = {
         'version_numbers': {
             'type': {
                 'biomaterial': {
                     'donor': '1.2.3'
                 }
             }
         }
     }
     schema_path = 'type/biomaterial/donor'
     # when
     schema = MetadataSchema(schema_json, schema_path).insert_id_url(
         version_map, self.schema_base_url)
     # then
     self.assertEqual(
         schema.get('id'),
         'https://schema.humancellatlas.org/type/biomaterial/1.2.3/donor')
Exemple #7
0
    def test_update_refs_urls__returns_json_with_updated_refs__when_all_ref_versions_found(
            self):
        # given
        schema_json = {
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'properties': {
                'provenance': {
                    'description':
                    'Provenance information provided by the system.',
                    'type': 'object',
                    '$ref': 'system/provenance.json'
                },
            }
        }
        version_map = {
            'version_numbers': {
                'type': {
                    'biomaterial': {
                        'donor': '1.2.3'
                    }
                },
                'system': {
                    'provenance': '1.0.0'
                }
            }
        }
        schema_path = 'type/biomaterial/donor'

        # when
        schema = MetadataSchema(schema_json, schema_path).update_refs_urls(
            version_map, self.schema_base_url)

        # then
        expected = {
            '$schema': 'http://json-schema.org/draft-04/schema#',
            'properties': {
                'provenance': {
                    'description':
                    'Provenance information provided by the system.',
                    'type':
                    'object',
                    '$ref':
                    'https://schema.humancellatlas.org/system/1.0.0/provenance'
                },
            }
        }

        self.assertEqual(expected, schema)
Exemple #8
0
    def test_insert_id_url__raises_error__when_version_from_path_is_not_found(
            self):
        # given
        schema_json = {
            '$schema': 'http://json-schema.org/draft-07/schema#',
        }
        version_map = {
            'version_numbers': {
                'type': {
                    'biomaterial': {
                        'donor': '1.2.3'
                    }
                }
            }
        }
        schema_path = 'type/biomaterial/cell_suspension'

        # when/then
        with self.assertRaises(MetadataSchemaError):
            MetadataSchema(schema_json, schema_path).get_json_schema(
                version_map, self.schema_base_url)
Exemple #9
0

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument('path',
                        help="Base path to the HCA metadata schemas",
                        metavar="FILE")
    parser.add_argument('schema_base_url',
                        help="Base url of the schema server")

    args = parser.parse_args()

    if "~" in args.path:
        path = os.path.expanduser(args.path)
    else:
        path = args.path

    version_map = load_json(path + "/versions.json")

    schemas = get_schema_paths(path)

    for schema in schemas:
        raw_schema_json = load_json(schema)
        metadata_schema = MetadataSchema(path, raw_schema_json)
        new_json = metadata_schema.get_json_schema(version_map,
                                                   args.schema_base_url)
        dump_json(schema, new_json)