Skip to content

Commit 3ff798c

Browse files
feature: riftlib runtime acquisition
1 parent 7184404 commit 3ff798c

7 files changed

Lines changed: 116 additions & 63 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![PyPI version](https://img.shields.io/badge/rift--framework-0.8.5-informational?style=flat-square&color=FFFF91&labelColor=360825)](https://pypi.org/project/rift-framework/)
66
[![Telegram](https://img.shields.io/badge/Telegram-@skyring__org-informational?style=flat-square&color=0088cc&labelColor=360825)](https://t.me/skyring_org)
77
[![Telegram](https://img.shields.io/badge/Docs-docs.skyring.io/rift-informational?style=flat-square&color=6A0F49&labelColor=360825)](https://docs.skyring.io/rift/)
8-
> _A magical **Python3** -> **FunC** portal_
8+
> _A magical **Python3** -> **TON** portal_
99
1010
Rift is smart contract development framework in Python for [TON (The Open Network)](https://ton.org). Its purpose is to make the development, testing, and deployment procedures much easier!
1111

build.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

rift/fift/fift.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from rift.fift.types.util import create_entry
1414
from rift.fift.utils import calc_method_id
1515
from rift.native import NativeLib, native_call
16+
from rift.runtime.riftlib import RiftLibSetup
1617

1718

1819
class FiftError(RuntimeError):
@@ -32,6 +33,7 @@ def __init__(
3233
load_utils=False,
3334
):
3435
if not lib_path:
36+
lib_path = RiftLibSetup.acquire_lib(ensure=True)
3537
p = Path(sys.modules["rift"].__file__).parent.parent
3638
lib_path = os.path.join(
3739
p.absolute(),

rift/fift/test_result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def __init__(self, res: TVMError | TVMResult | ExecutionError) -> None:
3838
def expect_ok(self) -> TVMResult:
3939
if self.state != State.Ok:
4040
raise TestError(
41-
"Expected successful exit but got error!", self.curr_error(),
41+
"Expected successful exit but got error!",
42+
self.curr_error(),
4243
)
4344
return self.result
4445

rift/runtime/riftlib.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import os
2+
import platform
3+
import subprocess
4+
5+
import requests
6+
from tqdm import tqdm
7+
8+
from rift.runtime.config import Config
9+
10+
11+
class RiftLibSetup:
12+
urls = {
13+
"windows": "https://github.com/AminRezaei0x443/ton/releases/download/v0.1.0/rift-lib-win64.dll",
14+
"darwin": "https://github.com/AminRezaei0x443/ton/releases/download/v0.1.0/rift-lib-macOS.dylib",
15+
"linux-u18": "https://github.com/AminRezaei0x443/ton/releases/download/v0.1.0/rift-lib-ubuntu-18.04.so",
16+
"linux-u20": "https://github.com/AminRezaei0x443/ton/releases/download/v0.1.0/rift-lib-ubuntu-20.04.so",
17+
"linux-u22": "https://github.com/AminRezaei0x443/ton/releases/download/v0.1.0/rift-lib-ubuntu-22.04.so",
18+
}
19+
20+
@classmethod
21+
def acquire_lib(cls, ensure=False) -> str:
22+
if ensure:
23+
cls.setup_riftlib()
24+
return os.path.join(Config.dirs.user_data_dir, "_riftlib.pyd")
25+
26+
@classmethod
27+
def is_setup(cls) -> bool:
28+
return os.path.exists(cls.acquire_lib())
29+
30+
@classmethod
31+
def _ubuntu_version(cls) -> int:
32+
try:
33+
if os.path.exists("/etc/lsb-release"):
34+
with open("/etc/lsb-release") as f:
35+
c = f.read().strip()
36+
lines = c.split("\n")
37+
info = dict(map(lambda x: x.split("="), lines))
38+
if info["DISTRIB_ID"].lower() == "ubuntu":
39+
return int(info["DISTRIB_RELEASE"].split(".")[0])
40+
return -1
41+
except Exception:
42+
return -1
43+
44+
@classmethod
45+
def determine_lib(cls) -> str:
46+
s = platform.uname().system.lower()
47+
if s == "windows" or s == "darwin":
48+
return cls.urls[s]
49+
uv = cls._ubuntu_version()
50+
if uv == -1:
51+
raise RuntimeError("Unsupported os")
52+
return cls.urls[f"linux-u{uv}"]
53+
54+
@classmethod
55+
def _call_get(cls, command):
56+
p = subprocess.Popen(
57+
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
58+
)
59+
out, err = p.communicate()
60+
if p.returncode == 0:
61+
return True, out.decode("utf-8")
62+
return False, err.decode("utf-8")
63+
64+
@classmethod
65+
def get_shared_libs(cls):
66+
ok, res = cls._call_get("ldconfig -p")
67+
if not ok:
68+
raise RuntimeError("Error fetching shared libraries!")
69+
res = filter(lambda x: "=>" in x, res.split("\n"))
70+
res = map(
71+
lambda x: list(map(lambda f: f.strip(), x.split("=>"))), res
72+
)
73+
libs = dict(res)
74+
return libs
75+
76+
@classmethod
77+
def get_glibc_version(cls):
78+
ok, res = cls._call_get("ldconfig --version")
79+
if not ok:
80+
raise RuntimeError("Error fetching glibc version!")
81+
return res.split("\n")[0].split(" ")[-1].strip()
82+
83+
@classmethod
84+
def get_env_info(cls):
85+
return {
86+
"libs": cls.get_shared_libs(),
87+
"glibc": cls.get_glibc_version(),
88+
}
89+
90+
@classmethod
91+
def _download(cls, addr: str, to: str, buffer=1024):
92+
response = requests.get(addr, stream=True)
93+
file_size = int(response.headers.get("Content-Length"))
94+
pbar = tqdm(total=file_size, unit="B", unit_scale=True)
95+
with open(to, "wb") as f:
96+
for chunk in response.iter_content(chunk_size=buffer):
97+
if chunk: # filter out keep-alive new chunks
98+
f.write(chunk)
99+
pbar.update(buffer)
100+
pbar.close()
101+
102+
@classmethod
103+
def setup_riftlib(cls):
104+
lib_path = cls.acquire_lib()
105+
if cls.is_setup():
106+
return
107+
url = cls.determine_lib()
108+
cls._download(url, lib_path, buffer=4096)

setup.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
from setuptools import setup, find_packages
2-
from build import build_kwargs
1+
from setuptools import find_packages, setup
32

43
setup(
54
name="rift-framework",
65
version="0.9.5",
7-
description="A magical Python3 -> FunC portal",
6+
description="A magical Python3 -> TON portal",
87
license="MIT",
98
packages=find_packages(exclude=["test"]),
109
author="Amin Rezaei",
@@ -18,5 +17,4 @@
1817
url="https://github.com/sky-ring/rift",
1918
install_requires=[],
2019
extras_require={},
21-
**build_kwargs(),
2220
)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[flake8]
22
ignore=D100, D102, N802, DUO105, DUO110, S102, Q000, F401, F403, E203, I001,
3-
W503, I005, E731, B010, B009, N804, D401, DAR101, I003, S101, RST301, B016, N801, E721, B023, F405, N805, DAR201, E800, DUO102, E501, S110, D200, D400
3+
W503, I005, E731, B010, B009, N804, D401, DAR101, I003, S101, RST301, B016, N801, E721, B023, F405, N805, DAR201, E800, DUO102, E501, S110, D200, D400, S603, S404
44
extend-ignore=WPS, D1
55
exclude =
66
.git,

0 commit comments

Comments
 (0)