Skip to content

Commit

Permalink
chore: package lib in python package
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps committed Dec 7, 2023
1 parent 3d60f96 commit 8e263a6
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 52 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ flipt_engine.*
__pycache__/

# Node
node_modules/
node_modules/

.DS_Store
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ node: build ## Build the node client package
cp target/release/$(LIB).* flipt-client-node/ext/
cd flipt-client-node && npm install && npm run build && npm pack

python: build ## Prepare the python client for building
cp target/release/$(LIB).* flipt-client-python/
python: build ## Build the python client package
cp target/release/$(LIB).* flipt-client-python/ext/
cd flipt-client-python && poetry build

ruby: build ## Build the ruby client gem
cp target/release/$(LIB).* flipt-client-ruby/lib/ext/
Expand All @@ -26,7 +27,7 @@ clean: ## Clean up build artifacts
rm -rf flipt-client-go/$(LIB).* flipt-client-go/$(HEADER)
rm -rf flipt-client-node/ext/$(LIB).*
rm -rf flipt-client-node/*.tgz
rm -rf flipt-client-python/$(LIB).*
rm -rf flipt-client-python/ext/$(LIB).*
rm -rf flipt-client-ruby/lib/ext/$(LIB).*
rm -rf flipt-client-ruby/pkg/

Expand Down
2 changes: 1 addition & 1 deletion build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ In the `build/` directory we will use [Dagger](https://dagger.io/) to orchestrat

Make sure you have `dagger` installed. This module is pinned to `v0.9.3` currently.

Here are the [installation instructions](https://docs.dagger.io/quickstart/729236/cli) for `dagger`.
Here are the [Dagger Installation Instructions](https://docs.dagger.io/quickstart/729236/cli).

## Running Tests

Expand Down
3 changes: 1 addition & 2 deletions build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ func pythonTests(ctx context.Context, client *dagger.Client, flipt *dagger.Conta
WithExec([]string{"pip", "install", "poetry==1.7.0"}).
WithWorkdir("/app").
WithDirectory("/app", hostDirectory.Directory("flipt-client-python")).
WithFile("/app/libfliptengine.so", dynamicLibrary).
WithFile("/app/ext/libfliptengine.so", dynamicLibrary).
WithServiceBinding("flipt", flipt.WithExec(nil).AsService()).
WithEnvVariable("FLIPT_URL", "http://flipt:8080").
WithEnvVariable("FLIPT_AUTH_TOKEN", "secret").
WithEnvVariable("FLIPT_ENGINE_LIB_PATH", "/app/libfliptengine.so").
WithExec([]string{"poetry", "install", "--without=dev"}).
WithExec([]string{"poetry", "run", "test"}).
Sync(ctx)
Expand Down
14 changes: 7 additions & 7 deletions diagrams/architecture.d2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
golang: {
go: {
shape: image
icon: https://icon.icepanel.io/Technology/svg/Go.svg
}
Expand All @@ -18,7 +18,7 @@ typescript: {
icon: https://icon.icepanel.io/Technology/svg/TypeScript.svg
}

dynamic library: {
flipt-engine: {
shape: square
rust: {
shape: image
Expand All @@ -34,12 +34,12 @@ flipt-service: {
}
}

golang -> dynamic library: FFI
ruby -> dynamic library: FFI
python -> dynamic library: FFI
typescript -> dynamic library: FFI
go <-> flipt-engine: FFI
ruby <-> flipt-engine: FFI
python <-> flipt-engine: FFI
typescript <-> flipt-engine: FFI

dynamic library <-> flipt-service: Flag state sync (default 2 mins) {
flipt-engine <-> flipt-service: Evaluation State Sync {
style: {
opacity: 0.9
stroke-dash: 3
Expand Down
Binary file modified diagrams/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions flipt-client-node/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flipt Client Node

The `flipt-client-node` directory contains the TypeScript source code for a Flipt evaluation client.
The `flipt-client-node` directory contains the TypeScript source code for the Flipt client-side evaluation client.

## Installation

Expand All @@ -25,7 +25,7 @@ The dynamic library will contain the functionality necessary for the client to m
2. Install the package locally. You can do this by running the following command from the `flipt-client-node` directory:

```bash
npm install -g flipt-client-node-{version}.tgz
npm install -g flipt-client-{version}.tgz
```

### Manual Build
Expand All @@ -52,15 +52,15 @@ The `path/to/lib` will be the path to the dynamic library which will have the fo
npm install
npm run build
npm pack
npm install -g flipt-client-node-{version}.tgz
npm install -g flipt-client-{version}.tgz
```
## Usage
In your Node code you can import this client and use it as so:
```typescript
import { FliptEvaluationClient } from 'flipt-client-node';
import { FliptEvaluationClient } from 'flipt-client';
// namespace is the first positional argument and is optional here and will have a value of "default" if not specified.
// engine_opts is the second positional argument and is also optional, the structure is:
Expand Down
2 changes: 1 addition & 1 deletion flipt-client-node/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "flipt-client-node",
"name": "flipt-client",
"version": "0.0.1",
"description": "Flipt Node Client for client-side evaluation",
"main": "dist/index.js",
Expand Down
23 changes: 11 additions & 12 deletions flipt-client-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ import {

let libfile = '';

switch (os.platform()) {
case 'darwin':
libfile = 'libfliptengine.dylib';
break;
case 'win32':
libfile = 'libfliptengine.dll';
break;
default:
libfile = 'libfliptengine.so';
break;
}

// get absolute path to libfliptengine
if (process.env.FLIPT_ENGINE_LIB_PATH) {
libfile = process.env.FLIPT_ENGINE_LIB_PATH;
} else {
const path = require('path');
switch (os.platform()) {
case 'darwin':
libfile = 'libfliptengine.dylib';
break;
case 'win32':
libfile = 'libfliptengine.dll';
break;
default:
libfile = 'libfliptengine.so';
break;
}
libfile = path.join(__dirname, '..', 'ext', libfile);
}

Expand Down
48 changes: 40 additions & 8 deletions flipt-client-python/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,57 @@
# Flipt Client Python

The `flipt-client-python` directory contains the Python source code for a Flipt evaluation client.
The `flipt-client-python` directory contains the Python source code for the Flipt client-side evaluation client.

## Instructions
## Installation

To use this client, you can run the following command from the root of the repository:
Currently, to use this client, you'll need to build the dynamic library and the gem locally and install it. This is a temporary solution until we can figure out a better way to package and distribute the libraries.

```bash
cargo build --release
```
The dynamic library will contain the functionality necessary for the client to make calls to the Flipt engine via FFI. See [flipt-engine](../flipt-engine) for more information on the Flipt engine and FFI.

### Prerequisites

- [Rust](https://www.rust-lang.org/tools/install)
- [Python](https://www.python.org/downloads/)
- [Make](https://www.gnu.org/software/make/)
- [poetry](https://python-poetry.org/docs/#installation)

### Automated Build

1. Build and copy the dynamic library to the `flipt-client-python/ext` directory. This will also build and install the `flipt-client` Python package. You can do this by running the following command from the root of the repository:

```bash
make python
```

### Manual Build

This should generate a `target/` directory in the root of this repository which contains the dynamic linking library built for your platform. This dynamic library will contain the functionality necessary for the Python client to make FFI calls. You'll need to set the `FLIPT_ENGINE_LIB_PATH` environment variable depending on your platform:
1. Build the Rust dynamic library

```bash
cargo build --release
This should generate a `target/` directory in the root of this repository, which contains the dynamically linked library built for your platform.
2. You'll need to copy the dynamic library to the `flipt-client-python/ext` directory. This is a temporary solution until we can figure out a better way to package the libraries with the package.
The `path/to/lib` will be the path to the dynamic library which will have the following paths depending on your platform.
- **Linux**: `{REPO_ROOT}/target/release/libfliptengine.so`
- **Windows**: `{REPO_ROOT}/target/release/libfliptengine.dll`
- **MacOS**: `{REPO_ROOT}/target/release/libfliptengine.dylib`
3. You can then build the package and install it locally. You can do this by running the following command from the `flipt-client-python` directory:
```bash
poetry install
```
## Usage
In your Python code you can import this client and use it as so:
```python
from flipt_client_python import FliptEvaluationClient
from flipt_client import FliptEvaluationClient
# "namespace" and "engine_opts" are two keyword arguments that this constructor accepts
# namespace: which namespace to fetch flag state from
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ctypes
import json
import os

from .models import (
Expand All @@ -12,9 +11,21 @@

class FliptEvaluationClient:
def __init__(self, namespace: str = "default", engine_opts: EngineOpts = {}):
# first try to load the engine library from FLIPT_ENGINE_LIB_PATH env var if it exists
engine_library_path = os.environ.get("FLIPT_ENGINE_LIB_PATH")

if engine_library_path is None:
raise Exception("FLIPT_ENGINE_LIB_PATH not set")
# if not set, get the absolute path to the engine library from the ../ext directory
engine_library_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "../ext/libfliptengine.so"
)

if not os.path.exists(engine_library_path):
raise Exception(
"The engine library could not be found at the path: {}".format(
engine_library_path
)
)

self.namespace_key = namespace

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion flipt-client-python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[tool.poetry]
name = "flipt-client-python"
include = [{ path = "ext/libfliptengine.*" }]
name = "flipt-client"
version = "0.0.1"
description = ""
authors = ["Flipt Devs <[email protected]>"]
Expand Down
4 changes: 2 additions & 2 deletions flipt-client-python/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest
from flipt_client_python import FliptEvaluationClient
from flipt_client_python.models import EngineOpts
from flipt_client import FliptEvaluationClient
from flipt_client.models import EngineOpts


class TestFliptEvaluationClient(unittest.TestCase):
Expand Down
14 changes: 6 additions & 8 deletions flipt-client-ruby/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flipt Client Ruby

The `flipt-client-ruby` directory contains the Ruby source code for a Flipt evaluation client.
The `flipt-client-ruby` directory contains the Ruby source code for the Flipt client-side evaluation client.

## Installation

Expand All @@ -16,9 +16,7 @@ The dynamic library will contain the functionality necessary for the client to m

### Automated Build

1. Build and copy the dynamic library to the `flipt-client-ruby/lib/ext` directory. You can do this by running the following command from the root of the repository:

```bash
1. Build and copy the dynamic library to the `flipt-client-ruby/lib/ext` directory. This will also build and install the `flipt_client` gem on your local machine. You can do this by running the following command from the root of the repository:

```bash
make ruby
Expand All @@ -44,10 +42,10 @@ The `path/to/lib` will be the path to the dynamic library which will have the fo
3. You can then build the gem and install it locally. You can do this by running the following command from the `flipt-client-ruby` directory:
```bash
rake build
gem install pkg/flipt_client-{version}.gem
```
```bash
rake build
gem install pkg/flipt_client-{version}.gem
```
## Usage
Expand Down

0 comments on commit 8e263a6

Please sign in to comment.