Skip to content

Commit

Permalink
support python sdk for ormb (#45)
Browse files Browse the repository at this point in the history
* support python sdk for ormb

* feat: Update gitignore and requirements

Co-authored-by: robertzhu <[email protected]>
  • Loading branch information
robertzhu-ol and robertzhu authored Jun 14, 2020
1 parent 8150ac1 commit beb0756
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 1 deletion.
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Output folder of build
bin

# Related files of Mac and ide
.idea
.DS_Store

# Middle files of python
__pycache__
*.pyc

# Output file of unit test coverage
coverage.out

Expand All @@ -10,4 +18,4 @@ coverage.out
dist/
.vscode/

training-serving.ipynb
training-serving.ipynb
2 changes: 2 additions & 0 deletions extern-sdk/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## python sdk for ormb

58 changes: 58 additions & 0 deletions extern-sdk/python/git_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import json
import requests
import platform
import sys
import tarfile
import os


REPOS = "caicloud/ormb"
VERSION = "latest"
BIN_PATH = './ormb/bin'

OS_LIST = ["Linux", "Darwin"]
ARCH_LIST = ["x86_64", "i386"]


def untar(fname, dirs):
t = tarfile.open(fname)
t.extractall(path=dirs)


def download():
url = 'https://api.github.com/repos/%s/releases/%s' % (REPOS, VERSION)
r = requests.get(url)

if r.status_code != 200:
raise Exception("get assets info err, ret code: %s" % r.status_code)

json_info = json.loads(r.text)

cur_version = json_info["tag_name"][1:]

asset_name = "ormb_%s_Linux_x86_64.tar.gz" % cur_version
for os_name in OS_LIST:
for arch_name in ARCH_LIST:
if arch_name in platform.platform().lower() and os_name.lower() in sys.platform:
asset_name = "ormb_%s_%s_%s.tar.gz" % (cur_version, os_name, arch_name)

asset_url = ""

for asset in json_info["assets"]:
if asset_name in asset["browser_download_url"]:
asset_url = asset["url"]

# download the url contents in binary format
headers = {'Accept': 'application/octet-stream'}
r = requests.get(asset_url, headers=headers)

# open method to open a file on your system and write the contents
with open(asset_name, "wb") as code:
code.write(r.content)

if not os.path.exists(BIN_PATH):
os.mkdir(BIN_PATH)
untar(asset_name, BIN_PATH)

os.remove(asset_name)

4 changes: 4 additions & 0 deletions extern-sdk/python/ormb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from ormb.api import *


__version__ = '0.0.1'
41 changes: 41 additions & 0 deletions extern-sdk/python/ormb/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from subprocess import Popen
from os.path import abspath, join, dirname

BIN_PATH = join(abspath(dirname(__file__)), 'bin')


def login(hostname: str, username: str, password: str, insecure_opt: str):
ex = Popen([join(BIN_PATH, "ormb"), "login", hostname, "--username", username, "--password", password, "--insecure",
insecure_opt])
status = ex.wait()
return status


def push(ref: str):
ex = Popen([join(BIN_PATH, "ormb"), "push", ref])
status = ex.wait()
return status


def pull(ref: str):
ex = Popen([join(BIN_PATH, "ormb"), "pull", ref])
status = ex.wait()
return status


def export(ref: str, dst: str):
ex = Popen([join(BIN_PATH, "ormb"), "export", ref, dst])
status = ex.wait()
return status


def save(src: str, ref: str):
ex = Popen([join(BIN_PATH, "ormb"), "save", src, ref])
status = ex.wait()
return status


def remove(ref: str):
ex = Popen([join(BIN_PATH, "ormb"), "remove", ref])
status = ex.wait()
return status
1 change: 1 addition & 0 deletions extern-sdk/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests==2.23.0
38 changes: 38 additions & 0 deletions extern-sdk/python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import re
import os
from setuptools import find_packages
from setuptools import setup

from git_release import download
download()


def read_version():
regexp = re.compile(r"^__version__\W*=\W*'([\d.abrc]+)'")
init_py = os.path.join(os.path.dirname(__file__), "ormb", "__init__.py")
with open(init_py) as f:
for line in f:
match = regexp.match(line)
if match is not None:
return match.group(1)
raise RuntimeError("Cannot find version in {}".format(init_py))


setup(
name="ormb",
version=read_version(),
url="https://github.com/caicloud/ormb",
project_urls={
"Documentation": "https://github.com/caicloud/ormb/wikis/home",
"Code": "",
"Issue tracker": "https://github.com/caicloud/ormb/issues",
},
maintainer="gaocegege, ZhuYuJin",
description="ormb warehouse",
python_requires=">=3.6",
install_requires=[
"requests"
],
packages=find_packages(include=("ormb", "ormb.*")),
package_data={'ormb': ['bin/*']},
)
Empty file.
Empty file.

0 comments on commit beb0756

Please sign in to comment.