Skip to content

Commit d7d8b9c

Browse files
author
Yucong Sun
authored
Creating intitial tests for python (#5)
1 parent 3c9fb5e commit d7d8b9c

11 files changed

+142
-4
lines changed

.circleci/config.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ jobs:
1111
command: |
1212
sudo sh -c 'echo "deb http://deb.debian.org/debian stretch-backports main" > /etc/apt/sources.list.d/backports.list'
1313
sudo apt-get update
14-
sudo apt-get -t stretch-backports install cmake
14+
sudo apt-get -t stretch-backports install cmake python3-dev python3-venv
1515
- run:
1616
name: Build everything
1717
command: |
1818
./build.sh
19+
- run:
20+
name: Build Python stuff
21+
command: |
22+
cd python
23+
./test.sh

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ cmake_minimum_required(VERSION 3.7)
33
project(libra-client-dev)
44

55
add_subdirectory(c)
6-
add_subdirectory(cpp)
6+
add_subdirectory(cpp)

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/bash -xv
22
set -euo pipefail
33

44
# Build libra-dev first

python/.gitignore

+7-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
*.pyc
1+
*.pyc
2+
*.so
3+
/*.egg-info
4+
/_skbuild
5+
/.eggs
6+
/dist
7+
/venv

python/CMakeLists.txt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project(pylibra)
4+
5+
set(CMAKE_C_STANDARD 11)
6+
7+
find_package(PythonExtensions REQUIRED)
8+
9+
add_library(_pylibra MODULE "pylibra/_pylibra.c")
10+
python_extension_module(_pylibra)
11+
install(TARGETS _pylibra LIBRARY DESTINATION pylibra)
12+
13+
14+
target_include_directories(_pylibra PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/include")
15+
target_link_directories(_pylibra PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../libra-dev/target/debug")
16+
target_link_libraries(_pylibra "libra_dev")
17+
18+
IF(APPLE)
19+
target_link_libraries(_pylibra "-framework Security")
20+
elseif(WIN32)
21+
22+
else()
23+
target_link_libraries(_pylibra "pthread")
24+
target_link_libraries(_pylibra "dl")
25+
target_link_libraries(_pylibra "m")
26+
endif()

python/pylibra/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from ._pylibra import *

python/pylibra/_pylibra.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
#include "data.h"
5+
6+
static PyObject *f_account_resource_from_lcs(PyObject *self, PyObject *args) {
7+
PyBytesObject* bytes;
8+
9+
/* Parse arguments */
10+
if(!PyArg_ParseTuple(args, "S", &bytes)) {
11+
return NULL;
12+
}
13+
14+
struct CDevAccountResource result = account_resource_from_lcs((const uint8_t*) PyBytes_AsString(bytes), PyBytes_Size(bytes));
15+
16+
PyObject* obj = PyDict_New();
17+
PyDict_SetItem(obj, PyUnicode_FromString("balance"), PyLong_FromUnsignedLongLong(result.balance));
18+
19+
return obj;
20+
}
21+
22+
static PyMethodDef pyLibraMethods[] = {
23+
{"account_resource_from_lcs", f_account_resource_from_lcs, METH_VARARGS, "parse account_state_blob"},
24+
{NULL, NULL, 0, NULL}
25+
};
26+
27+
28+
static struct PyModuleDef pyLibraModule = {
29+
PyModuleDef_HEAD_INIT,
30+
"_pylibra",
31+
"Python interface for the libra-dev library",
32+
-1,
33+
pyLibraMethods
34+
};
35+
36+
PyMODINIT_FUNC PyInit__pylibra(void) {
37+
return PyModule_Create(&pyLibraModule);
38+
}
39+
40+
/*
41+
PyMODINIT_FUNC
42+
PyInit_pyLibra(void)
43+
{
44+
PyObject *m;
45+
if (PyType_Ready(&pyLibraAccountResourceType) < 0)
46+
return NULL;
47+
48+
m = PyModule_Create(&pyLibraModule);
49+
if (m == NULL)
50+
return NULL;
51+
52+
Py_INCREF(&pyLibraAccountResourceType);
53+
if (PyModule_AddObject(m, "Custom", (PyObject *) &pyLibraAccountResourceType) < 0) {
54+
Py_DECREF(&pyLibraAccountResourceType);
55+
PY_DECREF(m);
56+
return NULL;
57+
}
58+
59+
return m;
60+
}
61+
*/

python/pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel", "scikit-build", "cmake", "ninja"]

python/setup.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
3+
from skbuild import setup # This line replaces 'from setuptools import setup'
4+
5+
# Require pytest-runner only when running tests
6+
pytest_runner = (['pytest-runner']
7+
if any(arg in sys.argv for arg in ('pytest', 'test'))
8+
else [])
9+
10+
setup_requires = pytest_runner
11+
12+
setup(name="pylibra",
13+
version="1.0.0",
14+
description="Python interface for the libra-dev library function",
15+
author="Yucong Sun",
16+
author_email="[email protected]",
17+
packages=['pylibra'],
18+
tests_require=['pytest', 'pytest-runner'],
19+
setup_requires=setup_requires
20+
)

python/test.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash -xv
2+
set -euo pipefail
3+
4+
python3 -m venv ./venv
5+
6+
./venv/bin/pip3 install --upgrade pip
7+
./venv/bin/pip3 install --upgrade scikit-build pytest
8+
9+
./venv/bin/python3 setup.py develop
10+
./venv/bin/python3 setup.py pytest

python/test_pylibra.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pylibra
2+
3+
BLOB = bytes.fromhex("020000002100000001674deac5e7fca75f00ca92b1ba3697f5f01ef585011beea7b361150f4504638f0800000002000000000000002100000001a208df134fefed8442b1f01fab59071898f5a1af5164e12c594de55a7004a91c8e0000002000000036ccb9ba8b4f0cd1f3e2d99338806893dff7478c69acee9b8e1247c053783a4800e876481700000000000200000000000000200000000b14ed4f5af8f8f077c7ec4313c6d395b9a7eb5f41eab9ec15367215ca9e420a01000000000000002000000032f56f77b09773aa64c78ee39943da7ec73f91cd757e325098e11b3edc4eccb10100000000000000")
4+
5+
def test_account_state_blob():
6+
res = pylibra.account_resource_from_lcs(BLOB)
7+
assert res["balance"] == 100000000000

0 commit comments

Comments
 (0)