Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support python sdk for ormb #45

Merged
merged 2 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
support python sdk for ormb
  • Loading branch information
robertzhu committed Jun 14, 2020
commit fd8d8c7102b4afb7922c2182a8a614de8a8f4a34
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'
gaocegege marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Binary file not shown.
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add version for the requirement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

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.