Skip to content

Commit

Permalink
feat: add justfile, setup CI
Browse files Browse the repository at this point in the history
This is a WIP commit.
  • Loading branch information
chrisdickinson committed Sep 14, 2023
1 parent f710c1a commit 7c39919
Show file tree
Hide file tree
Showing 6 changed files with 560 additions and 58 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: PHP CI

on:
push:
branches: [ main 'chris/20230914-python-ci' ]
pull_request:
branches: [ main, 'chris/20230914-python-ci' ]

jobs:
prepare:
name: Install Extism Runtime
runs-on: ubuntu-latest
steps:
# for now, we lean on php-sdk's download. eventually we should
# split this out
- uses: extism/php-sdk/.github/actions/libextism
python:
name: Python Test
runs-on: ${{ matrix.os }}
needs: prepare
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
rust:
- stable
steps:
- name: Checkout sources
uses: actions/checkout@v3

- name: Download libextism.so
uses: actions/download-artifact@v2
with:
name: libextism-so-artifact

- name: Download libextism.h
uses: actions/download-artifact@v2
with:
name: libextism-h-artifact

- uses: extractions/setup-just@v1

- name: Move the files because download can't sudo
run: |
sudo mv libextism.so /usr/local/lib/
sudo mv extism.h /usr/local/include/
- name: Run Python tests
run: |
just test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
html
__pycache__
poetry-installer-error-*.log
59 changes: 1 addition & 58 deletions extism/extism.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,7 @@
from typing import Union
from enum import Enum
from uuid import UUID


class Error(Exception):
"""Extism error type"""

pass


_search_dirs = ["/usr/local", "/usr", os.path.join(os.getenv("HOME"), ".local"), "."]


def _check_for_header_and_lib(p):
def _exists(a, *b):
return os.path.exists(os.path.join(a, *b))

if _exists(p, "extism.h"):
if _exists(p, "libextism.so"):
return os.path.join(p, "extism.h"), os.path.join(p, "libextism.so")

if _exists(p, "libextism.dylib"):
return os.path.join(p, "extism.h"), os.path.join(p, "libextism.dylib")

if _exists(p, "include", "extism.h"):
if _exists(p, "lib", "libextism.so"):
return os.path.join(p, "include", "extism.h"), os.path.join(
p, "lib", "libextism.so"
)

if _exists(p, "lib", "libextism.dylib"):
return os.path.join(p, "include", "extism.h"), os.path.join(
p, "lib", "libextism.dylib"
)


def _locate():
"""Locate extism library and header"""
script_dir = os.path.dirname(__file__)
env = os.getenv("EXTISM_PATH")
if env is not None:
r = _check_for_header_and_lib(env)
if r is not None:
return r

r = _check_for_header_and_lib(script_dir)
if r is not None:
return r

r = _check_for_header_and_lib(".")
if r is not None:
return r

for d in _search_dirs:
r = _check_for_header_and_lib(d)
if r is not None:
return r

raise Error("Unable to locate the extism library and header file")

from .utils import _locate, Error

# Initialize the C library
_ffi = FFI()
Expand Down
60 changes: 60 additions & 0 deletions extism/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
_search_dirs = ["/usr/local", "/usr", os.path.join(os.getenv("HOME"), ".local"), "."]


class Error(Exception):
"""Extism error type"""

pass


def _check_for_header_and_lib(p):
def _exists(a, *b):
return os.path.exists(os.path.join(a, *b))

if _exists(p, "extism.h"):
if _exists(p, "libextism.so"):
return os.path.join(p, "extism.h"), os.path.join(p, "libextism.so")

if _exists(p, "libextism.dylib"):
return os.path.join(p, "extism.h"), os.path.join(p, "libextism.dylib")

if _exists(p, "include", "extism.h"):
if _exists(p, "lib", "libextism.so"):
return os.path.join(p, "include", "extism.h"), os.path.join(
p, "lib", "libextism.so"
)

if _exists(p, "lib", "libextism.dylib"):
return os.path.join(p, "include", "extism.h"), os.path.join(
p, "lib", "libextism.dylib"
)


def _locate():
"""Locate extism library and header"""
script_dir = os.path.dirname(__file__)
env = os.getenv("EXTISM_PATH")
if env is not None:
r = _check_for_header_and_lib(env)
if r is not None:
return r

r = _check_for_header_and_lib(script_dir)
if r is not None:
return r

r = _check_for_header_and_lib(".")
if r is not None:
return r

for d in _search_dirs:
r = _check_for_header_and_lib(d)
if r is not None:
return r

raise Error("Unable to locate the extism library and header file")


if __name__ == '__main__':
print(_locate())
42 changes: 42 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export PATH := env_var("PATH") + ":" + env_var("HOME") + "/.local/bin"

_check:
#!/bin/bash
if ! &>/dev/null which poetry; then
curl -sSL https://install.python-poetry.org | sed -e 's|symlinks=False|symlinks=True|' | python3 -
fi

# check for presence of the extism shared library and headers, installing
# then if necessary
if ! &>/dev/null poetry run python -m extism.utils; then
if ! &>/dev/null which extism; then
pip3 install git+https://github.com/extism/cli
fi

extism install git
fi

prepare: _check
poetry install

test: prepare
poetry run python -m unittest discover

clean:
rm -rf dist/*

publish: clean prepare
poetry build
poetry run twine upload dist/extism-*.tar.gz

format:
poetry run black extism/ tests/ example.py

lint:
poetry run black --check extism/ tests/ example.py

docs:
poetry run pycco extism/*.py

show-docs: docs
open docs/extism.html
Loading

0 comments on commit 7c39919

Please sign in to comment.