示例#1
0
    def test_extract_index_meta(self):
        self.maxDiff = None
        dt = met.get_datetime_now()
        base_meta = {
            "created_at": dt,
            "model": "docfreq",
            "series": "pga-2018",
            "uuid": "12345678-9abc-def0-1234-56789abcdef0",
            "version": [1, 0, 2],
            "parent": "f64bacd4-67fb-4c64-8382-399a8e7db52a",
            "dependencies": ["1e3da42a-28b6-4b33-94a2-a5671f4102f4"],
            "description": "model_description",
            "license": "MIT",
        }
        extra_meta = {
            "code": "model_code %s",
            "datasets": [["any", "https://any"]],
            "description": "override",
            "references": [["any", "ref"]],
            "tags": ["one", "two"],
            "extra": {
                "feature": "value"
            },
        }

        def route(url):
            self.assertEqual("https://xxx", url)
            return b"content"

        met.requests = FakeRequests(route)
        model_meta = met.extract_model_meta(base_meta, extra_meta,
                                            "https://xxx")
        self.assertIsInstance(model_meta, dict)
        self.assertGreater(len(met.format_datetime(dt)), 0)
        self.assertDictEqual(
            model_meta, {
                "default": {
                    "default": "12345678-9abc-def0-1234-56789abcdef0",
                    "description": "model_description",
                    "code": "model_code %s"
                },
                "model": {
                    "created_at": met.format_datetime(dt),
                    "code": "model_code %s",
                    "description": "model_description",
                    "dependencies": ["1e3da42a-28b6-4b33-94a2-a5671f4102f4"],
                    "license": "MIT",
                    "parent": "f64bacd4-67fb-4c64-8382-399a8e7db52a",
                    "datasets": [["any", "https://any"]],
                    "references": [["any", "ref"]],
                    "size": "7 Bytes",
                    "series": "pga-2018",
                    "url": "https://xxx",
                    "tags": ["one", "two"],
                    "version": [1, 0, 2],
                    "extra": {
                        "feature": "value"
                    }
                }
            })
示例#2
0
    def test_extract_index_meta(self):
        self.maxDiff = None
        dt = str(datetime.now())
        base_meta = {
            "created_at": dt,
            "model": "docfreq",
            "uuid": "12345678-9abc-def0-1234-56789abcdef0",
            "version": [1, 0, 2]
        }
        extra_meta = {
            "code": "readme_code %s",
            "description": "readme_description",
            "model": {
                "code": "model_code %s",
                "description": "model_description",
                "dependencies": ["1e3da42a-28b6-4b33-94a2-a5671f4102f4"],
                "extra": {
                    "ex": "tra"
                },
                "license": ["", "undecided"],
                "parent": "f64bacd4-67fb-4c64-8382-399a8e7db52a",
                "references": [["any", "ref"]]
            }
        }

        def route(url):
            self.assertEqual("https://xxx", url)
            return b"content"

        met.requests = FakeRequests(route)
        model_meta = met.extract_model_meta(base_meta, extra_meta,
                                            "https://xxx")
        self.assertIsInstance(model_meta, dict)
        self.assertDictEqual(
            model_meta, {
                "default": {
                    "code": "readme_code %s",
                    "default": "12345678-9abc-def0-1234-56789abcdef0",
                    "description": "readme_description"
                },
                "model": {
                    "created_at": dt,
                    "code": "model_code %s",
                    "description": "model_description",
                    "dependencies": ["1e3da42a-28b6-4b33-94a2-a5671f4102f4"],
                    "extra": {
                        "ex": "tra"
                    },
                    "license": ["", "undecided"],
                    "parent": "f64bacd4-67fb-4c64-8382-399a8e7db52a",
                    "references": [["any", "ref"]],
                    "size": "7 Bytes",
                    "url": "https://xxx",
                    "version": [1, 0, 2],
                }
            })
示例#3
0
def publish_model(args: argparse.Namespace, backend: StorageBackend,
                  log: logging.Logger):
    """
    Push the model to Google Cloud Storage and updates the index file.

    :param args: :class:`argparse.Namespace` with "model", "backend", "args", "force", "meta" \
                 "update_default", "username", "password", "remote_repo", "template_model", \
                 "template_readme" and "log_level".
    :param backend: Backend which is responsible for working with model files.
    :param log: Logger supplied by supply_backend
    :return: None if successful, 1 otherwise.
    """
    path = os.path.abspath(args.model)
    try:
        model = GenericModel(source=path, dummy=True)
    except ValueError as e:
        log.critical('"model" must be a path: %s', e)
        return 1
    except Exception as e:
        log.critical("Failed to load the model: %s: %s" %
                     (type(e).__name__, e))
        return 1
    base_meta = model.meta
    try:
        model_url = backend.upload_model(path, base_meta, args.force)
    except ModelAlreadyExistsError:
        return 1

    log.info("Uploaded as %s", model_url)
    with open(os.path.join(args.meta), encoding="utf-8") as _in:
        extra_meta = json.load(_in)
    model_type, model_uuid = base_meta["model"], base_meta["uuid"]
    meta = extract_model_meta(base_meta, extra_meta, model_url)
    log.info("Updating the models index...")
    try:
        template_model = backend.index.load_template(args.template_model)
        template_readme = backend.index.load_template(args.template_readme)
    except ValueError:
        return 1
    backend.index.add_model(model_type, model_uuid, meta, template_model,
                            args.update_default)
    backend.index.update_readme(template_readme)
    try:
        backend.index.upload("add", {"model": model_type, "uuid": model_uuid})
    except ValueError:  # TODO: replace with PorcelainError, see related TODO in index.py:181
        return 1
    log.info("Successfully published.")