Skip to content

Commit

Permalink
adding code
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgard Marx committed Apr 8, 2024
1 parent 43dd9de commit 1685f72
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 15 deletions.
12 changes: 5 additions & 7 deletions README-public.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# cmem-plugin-imr
# imr (Inteligent Model Registry)

Inteligent Model Registry
This is a command-line interface and library for managing inteligent models.

This is a plugin for [eccenca](https://eccenca.com) [Corporate Memory](https://documentation.eccenca.com).

You can install it with the [cmemc](https://eccenca.com/go/cmemc) command line
clients like this:
You can install the [imr](https://github.com/eccenca/imr) command-line and library
with pip as following:

```
cmemc admin workspace python install cmem-plugin-imr
pip install imr
```

98 changes: 98 additions & 0 deletions imr/imr-cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from os.path import expanduser

import click
import yaml

from imr import IMRLocal, IMRRemote

home = expanduser("~")
imr_dir = home + "/.imr"
imr_local: IMRLocal = IMRLocal(imr_dir)
imr_remote: IMRRemote = None


def loadParams() -> None:
with open(imr_dir + "/config.yaml") as stream:
imrConfig = yaml.safe_load(stream)


@click.group()
def cli() -> None:
# loadParams()
pass


@cli.group()
def local() -> None:
pass


@local.command()
def list_local() -> None:
for package in imr_local.list():
print(package)


@local.command()
@click.argument("package")
@click.option(
"-v", "--version", type=str, default="latest", help="version of the model.", show_default=True
)
def remove(package: str, version: str) -> None:
list = local.rm(package, version)


@cli.group()
@click.argument("host")
@click.argument("user")
@click.argument("password")
def remote(host: str, user: str, password: str) -> None:
global imr_remote
imr_remote = IMRRemote(host, user, password)


@remote.command()
def list_remote() -> None:
list = imr_remote.list()
for p in list:
print(p)


@remote.command()
@click.argument("package")
@click.option(
"-v", "--version", type=str, default="latest", help="version of the model.", show_default=True
)
def rm(package: str, version: str) -> None:
imr_remote.rm(package, version)


@remote.command()
@click.argument("model_directory")
@click.argument("package")
@click.option(
"-v", "--version", type=str, default="latest", help="version of the model.", show_default=True
)
def push(model_directory: str, package: str, version: str) -> None:
imr_remote.push(model_directory, package, version)


@remote.command()
@click.argument("package")
@click.option(
"-d",
"--dir",
type=str,
default=imr_dir,
help="directory to pull the model in.",
show_default=True,
)
@click.option(
"-v", "--version", type=str, default="latest", help="version of the model.", show_default=True
)
def pull(package :str, dir :str, version :str) -> None:
imr_remote.pull(dir, package, version)


if __name__ == "__main__":
cli()
80 changes: 80 additions & 0 deletions imr/imr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Inteligent Model Registry (imr)
import os
import shutil
from pathlib import Path

from artifactory import ArtifactoryPath
from requests.auth import HTTPBasicAuth


class IMRRemote:
def __init__(self, repo: str, user: str, password: str):
self.repo = repo
self.user = user
self.password = password
self.home = str(Path.home())

def list(self) -> list:
packages = []
path = ArtifactoryPath(self.repo, auth=(self.user, self.password), auth_type=HTTPBasicAuth)
for package in path.glob("*/*"):
packages.append(str(package).replace(self.repo + "/", ""))
return packages

def push(self, directory: str, package: str, version :str="latest") -> None:
path = ArtifactoryPath(
self.repo + "/" + package + "/" + version,
auth=(self.user, self.password),
auth_type=HTTPBasicAuth,
)
path.mkdir()
shutil.make_archive("model", "zip", directory)
path.deploy_file("model.zip")

def pull(self, directory: str, package: str, version :str="latest") -> None:
path = ArtifactoryPath(
self.repo + "/" + package + "/" + version + "/model.zip",
auth=(self.user, self.password),
auth_type=HTTPBasicAuth,
)
file_path = directory + "/" + package + "/" + version
if not Path(file_path).exists():
Path.mkdir(file_path, parents=True)
with path.open() as fd, Path.open(file_path + "/" + "model.zip", "wb") as out:
out.write(fd.read())
shutil.unpack_archive(file_path + "/" + "model.zip", file_path + "/model")

def rm(self, package: str, version :str="latest") -> None:
artefact = self.repo + "/" + package
if version is not None:
artefact = self.repo + "/" + package + "/" + version
path = ArtifactoryPath(artefact, auth=(self.user, self.password), auth_type=HTTPBasicAuth)
if path.exists():
path.unlink()


class IMRLocal:
def __init__(self, repo: str | None = None):
self.home = Path.home()
if repo is None:
self.repo = str(self.home) + "/.imr"
else:
self.repo = repo
if not Path(self.repo).exists():
Path.mkdir(self.repo, parents=True)

def list(self) -> list:
list = []
for entry in os.walk(self.repo):
if len(entry[1]) == 0 and entry[0] not in self.repo:
list.append(entry[0].replace(self.repo + "/", ""))
return list

def push(self, directory: str, package: str, version: str="latest") -> None:
shutil.copytree(directory, self.repo + "/" + package + "/" + version)

def rm(self, package: str, version :str="latest") -> None:
if version is None:
shutil.rmtree(self.repo + "/" + package)
else:
shutil.rmtree(self.repo + "/" + package + "/" + version)
Loading

0 comments on commit 1685f72

Please sign in to comment.