Skip to content

Commit 42f6bfb

Browse files
authored
feat: add pip install pycasbin (#175)
* feat: add pip install pycasbin Signed-off-by: stonex <[email protected]> * fix: fix casbin-cpp url and remove pytest requirement Signed-off-by: stonex <[email protected]> * docs: remove the cmake install pycasbin and add pip install pycasbin module Signed-off-by: stonex <[email protected]>
1 parent 00d8414 commit 42f6bfb

26 files changed

+969
-49
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ jobs:
2727
- name: Configuring CMake files
2828
id: building-files
2929
run: |
30-
mkdir build && cd build && cmake ..
30+
mkdir build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release
3131
- name: Building library
3232
id: building-lib
3333
run: |
34-
cd build && cmake --build . --config Debug --target all -j 10 --
34+
cd build && cmake --build . --config Release --target all -j 10 --
3535
- name: Tests
3636
id: test-lib
3737
run: |
3838
cd build
39-
ctest -j10 -C Debug -T test --output-on-failure -T test --output-on-failure
39+
ctest -j10 -C Release -T test --output-on-failure -T test --output-on-failure
4040
- name: Cleanup
4141
id: clean-up
4242
run: |

.github/workflows/python_binding.yml

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,27 @@ name: Python Bindings Test
1717
on: [push, pull_request]
1818

1919
jobs:
20-
benchmark:
21-
name: Python Bindings Test
22-
runs-on: macos-latest
20+
build:
21+
strategy:
22+
fail-fast: false
23+
matrix:
24+
platform: [windows-latest, macos-latest, ubuntu-latest]
25+
python-version: ["3.6", "3.8", "3.10"]
26+
27+
runs-on: ${{ matrix.platform }}
28+
2329
steps:
24-
- name: Checkout
25-
id: checkout
26-
uses: actions/checkout@v2
27-
- name: Configuring CMake files
28-
id: building-files
29-
run: |
30-
mkdir build && cd build && cmake .. -DCMAKE_BUILD_TYPE:STRING=Release
31-
- name: Building library
32-
id: building-lib
33-
run: |
34-
cd build && cmake --build . --config Release --target all -j 10 --
35-
- name: Installing pycasbin
36-
id: installing-pycasbin
37-
run: |
38-
cd build && sudo cmake --build . --config Release --target install -j 10 --
39-
- name: Run Tests
40-
id: run-tests
41-
run: |
42-
cd tests/python && python3 pycasbin_test_suite.py
43-
- name: Cleanup
44-
id: clean-up
45-
run: |
46-
rm -r build
47-
rm tests/python/pycasbin.so
30+
- uses: actions/checkout@v2
31+
with:
32+
submodules: true
33+
- uses: actions/setup-python@v2
34+
with:
35+
python-version: ${{ matrix.python-version }}
36+
- name: Add requirements
37+
run: python -m pip install --upgrade wheel setuptools
38+
39+
- name: Build and install
40+
run: pip install --verbose .
41+
42+
- name: Test
43+
run: cd pycasbin/tests && python pycasbin_test_suite.py

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,6 @@ MigrationBackup/
361361
cmake-build/
362362
xcode-build/
363363
cmake-build*/
364+
365+
# pip
366+
*.egg-info

CMakeLists.txt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endif()
3333

3434
project(
3535
casbin
36-
VERSION 1.43.0
36+
VERSION 1.44.0
3737
DESCRIPTION "An authorization library that supports access control models like ACL, RBAC, ABAC in C/C++"
3838
HOMEPAGE_URL https://github.com/casbin/casbin-cpp
3939
LANGUAGES CXX C
@@ -54,7 +54,6 @@ endif()
5454
option(CASBIN_BUILD_TEST "State whether to build test" ON)
5555
option(CASBIN_BUILD_BENCHMARK "State whether to build benchmarks" ON)
5656
option(INTENSIVE_BENCHMARK "State whether to build intensive benchmarks" OFF)
57-
option(CASBIN_BUILD_BINDINGS "State whether to build language bindings" ON)
5857
option(CASBIN_BUILD_PYTHON_BINDINGS "State whether to build python bindings" ON)
5958
option(CASBIN_INSTALL "State whether to install casbin targets on the current system" ON)
6059

@@ -68,15 +67,6 @@ if(NOT DEFINED CMAKE_INSTALL_MESSAGE)
6867
set(CMAKE_INSTALL_MESSAGE "LAZY")
6968
endif()
7069

71-
if(CASBIN_BUILD_BINDINGS)
72-
add_subdirectory(bindings)
73-
endif()
74-
75-
if(CASBIN_BUILD_TEST)
76-
enable_testing()
77-
add_subdirectory(tests)
78-
endif()
79-
8070
# Change the path max size to avoid problem on Windows.
8171
if(NOT DEFINED CMAKE_OBJECT_PATH_MAX)
8272
set(CMAKE_OBJECT_PATH_MAX 300)
@@ -95,6 +85,15 @@ include(FindExtPackages)
9585

9686
add_subdirectory(casbin)
9787

88+
if(CASBIN_BUILD_PYTHON_BINDINGS)
89+
add_subdirectory(pycasbin)
90+
endif()
91+
92+
if(CASBIN_BUILD_TEST)
93+
enable_testing()
94+
add_subdirectory(tests)
95+
endif()
96+
9897
if(CASBIN_INSTALL)
9998
message(CHECK_START "[casbin]: Installing casbin ...")
10099
export(

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include README.md LICENSE
2+
graft include
3+
graft casbin
4+
graft pycasbin/
5+
global-include CMakeLists.txt *.cmake

cmake/modules/FindExtPackages.cmake

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ if(CASBIN_BUILD_TEST)
3838
endif()
3939
endif()
4040

41-
if(CASBIN_BUILD_BINDINGS)
42-
if(CASBIN_BUILD_PYTHON_BINDINGS)
43-
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
44-
# pybind11
45-
# https://github.com/pybind/pybind11
46-
find_package(pybind11 2.7.0 REQUIRED)
47-
endif()
41+
if(CASBIN_BUILD_PYTHON_BINDINGS)
42+
# pybind11
43+
# https://github.com/pybind/pybind11
44+
find_package(pybind11 2.8.0 REQUIRED)
4845
endif()

cmake/modules/Findpybind11.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ include(FetchContent)
1717
FetchContent_Declare(
1818
pybind11
1919
GIT_REPOSITORY https://github.com/pybind/pybind11.git
20-
GIT_TAG v2.7.1
20+
GIT_TAG v2.8.0
2121
)
2222

2323
FetchContent_MakeAvailable(pybind11)

pycasbin/CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2021 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
set(SOURCES
16+
src/main.cpp
17+
src/py_cached_enforcer.cpp
18+
src/py_enforcer.cpp
19+
src/py_model.cpp
20+
src/py_config.cpp
21+
src/py_synced_enforcer.cpp
22+
src/py_adapter.cpp
23+
)
24+
25+
set(HEADERS
26+
src/py_casbin.h
27+
)
28+
29+
add_library(pycasbin MODULE ${SOURCES} ${HEADERS})
30+
31+
target_include_directories(pycasbin PUBLIC ${CASBIN_INCLUDE_DIR})
32+
33+
set_target_properties(pycasbin PROPERTIES
34+
CXX_STANDARD 17
35+
)
36+
37+
# For in-source versioning macro
38+
add_definitions(-DPY_CASBIN_VERSION=${PY_CASBIN_VERSION})
39+
40+
target_link_libraries(pycasbin
41+
PRIVATE
42+
pybind11::module
43+
pybind11::lto
44+
pybind11::windows_extras
45+
casbin
46+
nlohmann_json::nlohmann_json
47+
)
48+
49+
pybind11_extension(pycasbin)
50+
pybind11_strip(pycasbin)
51+
# For testing
52+
# install(
53+
# TARGETS pycasbin
54+
# LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/tests/python
55+
# )

pycasbin/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
## Language Bindings for `casbin-cpp`
2+
3+
At present, `casbin-cpp` provides language bindings for Python, we named it `pycasbin`.
4+
5+
## Python Bindings
6+
7+
### Use `pip` install the `pycasbin` module
8+
9+
It is assumed you have `CMake >=v3.19` and `Python >= 3.6` installed. Current `pycasbin` only support `pip` install in local machine.
10+
11+
1. Clone/download the project:
12+
```bash
13+
git clone https://github.com/casbin/casbin-cpp.git
14+
```
15+
16+
2. Update `wheel setuptools`:
17+
```bash
18+
python -m pip install --upgrade wheel setuptools
19+
```
20+
21+
3. Install the `pycasbin` module:
22+
```bash
23+
cd casbin-cpp && pip install --verbose .
24+
```
25+
26+
Now, you're ready to go!
27+
28+
### Usage
29+
30+
It is assumed that you have `pycasbin` module correctly installed on your system.
31+
32+
First, we import the `pycasbin` module to a python source file:
33+
34+
```python
35+
import pycasbin as casbin
36+
```
37+
38+
Suppose we want a function to check authorization of a request:
39+
40+
```python
41+
def isAuthorized(req):
42+
result = True
43+
if result:
44+
print('Authorized')
45+
else
46+
print('Not authorized!')
47+
```
48+
49+
Here, the request can be a list or a dictionary in the forms:
50+
51+
```python
52+
req = ['subject1', 'object1', 'action1'] # and so on..
53+
54+
req = {
55+
"sub": "subject1",
56+
"obj": "object1",
57+
"act": "action1" # ... and so on
58+
}
59+
```
60+
61+
We can Enforce this request (or compute the `result` of this request) through `casbin.Enforce()`.
62+
For that, we need to create a `casbin.Enforcer`:
63+
64+
```python
65+
e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv')
66+
```
67+
Make sure that the paths are relative to the current python source file or an absolute path.
68+
69+
Apart from the regular `Enforcer`, you may also use `CachedEnforcer`
70+
depending on your use case.
71+
72+
Incorporating the `Enforcer` in our example gives us:
73+
74+
```python
75+
def isAuthorized(req):
76+
result = e.Enforce(req)
77+
if result:
78+
print('Authorized')
79+
else
80+
print('Not authorized!')
81+
```
82+
83+
Rest of the method's name is on par with `casbin-cpp`.
84+
85+
#### Summary
86+
87+
This sums up the basic usage of `pycasbin` module:
88+
89+
```python
90+
import pycasbin as casbin
91+
92+
e = casbin.Enforcer('path/to/model.conf', 'path/to/policy.csv')
93+
94+
def isAuthorized(req):
95+
result = e.Enforce(req)
96+
if result:
97+
print('Authorized')
98+
else
99+
print('Not authorized!')
100+
101+
isAuthorized(['subject1', 'object1', 'action1'])
102+
isAuthorized(['subject2', 'object2', 'action2'])
103+
# ... and so on
104+
```
105+
106+
If you've done everything right, you'll see your output
107+
without any errors.

pycasbin/src/main.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* This is the main file for python bindings workflow
17+
*/
18+
19+
#include <pybind11/pybind11.h>
20+
#include "py_casbin.h"
21+
22+
namespace py = pybind11;
23+
24+
PYBIND11_MODULE(pycasbin, m) {
25+
m.doc() = R"pbdoc(
26+
Casbin Authorization Library
27+
-----------------------
28+
29+
.. currentmodule:: pycasbin
30+
31+
.. autosummary::
32+
:toctree: _generate
33+
34+
Enforcer
35+
)pbdoc";
36+
37+
bindPyEnforcer(m);
38+
bindPyCachedEnforcer(m);
39+
bindPyModel(m);
40+
bindPyConfig(m);
41+
bindPySyncedEnforcer(m);
42+
bindPyAdapter(m);
43+
44+
m.attr("__version__") = PY_CASBIN_VERSION;
45+
}

0 commit comments

Comments
 (0)