def publish_model(args: argparse.Namespace, backend: StorageBackend, log: logging.Logger): """ Pushes the model to Google Cloud Storage and updates the index file. :param args: :class:`argparse.Namespace` with "model", "backend", "args", "force" and \ "update_default". :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 meta = model.meta with backend.lock(): model_url = backend.upload_model(path, meta, args.force) log.info("Uploaded as %s", model_url) log.info("Updating the models index...") index = backend.fetch_index() index["models"].setdefault(meta["model"], {})[meta["uuid"]] = \ extract_index_meta(meta, model_url) if args.update_default: index["models"][meta["model"]][Model.DEFAULT_NAME] = meta["uuid"] backend.upload_index(index)
def initialize_registry(args: argparse.Namespace, backend: StorageBackend, log: logging.Logger): """ Initialize the registry - list and publish will fail otherwise. :param args: :class:`argparse.Namespace` with "backend", "args" and "force". :return: None """ try: backend.fetch_index() if not args.force: log.warning("Registry is already initialized") return except FileNotFoundError: pass # The lock is not needed here, but upload_index() will raise otherwise with backend.lock(): backend.upload_index({"models": {}}) log.info("Successfully initialized")