Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(component): Support run typescript in lyric #3

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bindings/python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
PY_WORKER_DIR = lyric-py-worker
JS_WORKER_DIR = lyric-js-worker
TASK_WORKER_DIR = lyric-task
COMPONENT_TS_TRANSPILED_DIR = lyric-component-ts-transpiling

.PHONY: all
all: build ## Build all subprojects

.PHONY: build
build: build-task build-py-worker build-js-worker ## Build all subprojects
build: build-task build-py-worker build-js-worker component-ts-tra ## Build all subprojects

.PHONY: build-task
build-task: ## Build the task dependency
Expand All @@ -27,6 +28,11 @@ build-js-worker: ## Build the JavaScript worker
@echo "Building JavaScript worker..."
$(MAKE) -C $(JS_WORKER_DIR) build

.PHONY: component-ts-tra
component-ts-tra: ## Build the component-ts-transpiling
@echo "Building component-ts-transpiling..."
$(MAKE) -C $(COMPONENT_TS_TRANSPILED_DIR) build

.PHONY: clean
clean: ## Clean all projects
@echo "Cleaning all projects..."
Expand Down
10 changes: 10 additions & 0 deletions bindings/python/lyric-component-ts-transpiling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# python generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info

# venv
.venv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.14
38 changes: 38 additions & 0 deletions bindings/python/lyric-component-ts-transpiling/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.DEFAULT_GOAL := help

SHELL=/bin/bash
VENV = .venv

# Detect the operating system and set the virtualenv bin directory
ifeq ($(OS),Windows_NT)
VENV_BIN=$(VENV)/Scripts
else
VENV_BIN=$(VENV)/bin
endif

setup: $(VENV)/bin/activate

$(VENV)/bin/activate: $(VENV)/.venv-timestamp

$(VENV)/.venv-timestamp:
# Create new virtual environment if setup.py has changed
python3 -m venv $(VENV)
$(VENV_BIN)/pip install --upgrade pip
touch $(VENV)/.venv-timestamp

.PYHONY: build-task
build-task: setup
# to src directory and run setup.py
cd ../lyric-task && rye build
$(VENV_BIN)/pip install --force-reinstall ../dist/lyric_task-*.whl

.PHONY: build-wasm
build-wasm: build-task
# Activate virtual environment and run setup.py
# to src directory and run setup.py
cd ../../../components/rust/component-ts-transpiling && cargo build --release --target wasm32-wasip1
cp ../../../target/wasm32-wasip1/release/component_ts_transpiling.wasm src/lyric_component_ts_transpiling

.PHONY: build
build: build-wasm
rye build
4 changes: 4 additions & 0 deletions bindings/python/lyric-component-ts-transpiling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# lyric-component-ts-transpiling

Python wrapper for the [component-ts-transpiling](../../../components/rust/component-ts-transpiling/README.md)
wasi library.
29 changes: 29 additions & 0 deletions bindings/python/lyric-component-ts-transpiling/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[project]
name = "lyric-component-ts-transpiling"
version = "0.1.0"
description = "Add your description here"
authors = [
{ name = "Fangyin Cheng", email = "staneyffer@gmail.com" }
]
dependencies = [
"lyric-task",
]
readme = "README.md"
requires-python = ">= 3.10"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = []

[tool.hatch.metadata]
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/lyric_component_ts_transpiling"]
include = [
"src/lyric_component_ts_transpiling/*.wasm",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Wrapper for the TypeScript transpiling WebAssembly module.

May be we should find a better way to install the wasm file, but for now we just pypi
install the package and the wasm file is included in the package.
"""

from importlib.resources import files
from lyric_task import WasmTaskSpec, Language

def get_wasm_path():
return files('lyric_component_ts_transpiling').joinpath('component_ts_transpiling.wasm')


class TypeScriptWasmTaskSpec(WasmTaskSpec):
def __init__(self):
super().__init__(str(get_wasm_path()), Language.WASI)
49 changes: 47 additions & 2 deletions bindings/python/lyric-js-worker/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.DEFAULT_GOAL := help

SHELL=/bin/bash
VENV = .venv

Expand All @@ -10,6 +9,9 @@ else
VENV_BIN=$(VENV)/bin
endif

# Function to check if a command exists
CHECK_CMD = command -v $(1) >/dev/null 2>&1

setup: $(VENV)/bin/activate

$(VENV)/bin/activate: $(VENV)/.venv-timestamp
Expand All @@ -20,10 +22,53 @@ $(VENV)/.venv-timestamp:
$(VENV_BIN)/pip install --upgrade pip
touch $(VENV)/.venv-timestamp

.PHONY: check-node
check-node:
@if ! $(call CHECK_CMD,node); then \
echo "Node.js not found. Checking version managers..."; \
if $(call CHECK_CMD,fnm); then \
echo "fnm found, checking installed versions..."; \
if fnm list | grep -q "v[0-9]"; then \
echo "Node.js versions found in fnm, using latest..."; \
LATEST_VERSION=$$(fnm list | grep "v[0-9]" | tail -n1 | awk '{print $$2}'); \
fnm use $$LATEST_VERSION; \
else \
echo "No Node.js versions found in fnm, installing latest..."; \
fnm install --latest && fnm use latest; \
fi; \
elif $(call CHECK_CMD,nvm); then \
echo "nvm found, checking installed versions..."; \
if [ -s "$$HOME/.nvm/nvm.sh" ]; then \
. "$$HOME/.nvm/nvm.sh"; \
if nvm ls | grep -q "v[0-9]"; then \
echo "Node.js versions found in nvm, using latest..."; \
nvm use node; \
else \
echo "No Node.js versions found in nvm, installing latest..."; \
nvm install node && nvm use node; \
fi; \
fi; \
else \
echo "Error: Neither Node.js nor a version manager (fnm/nvm) was found."; \
echo "Please install Node.js, fnm, or nvm to continue."; \
exit 1; \
fi \
else \
echo "Node.js is already installed."; \
fi

.PYHONY: build-task
build-task: setup
# to src directory and run setup.py
cd ../lyric-task && rye build
$(VENV_BIN)/pip install --force-reinstall ../dist/lyric_task-*.whl

.PHONY: build-wasm
build-wasm: setup
build-wasm: build-task check-node
# Activate virtual environment and run setup.py
# to src directory and run setup.py
# Print current directory
echo "Current directory: $(PWD)"
cd ../../javascript/lyric-js-worker && npm run build
cp ../../javascript/lyric-js-worker/javascript_worker.wasm src/lyric_js_worker/

Expand Down
4 changes: 3 additions & 1 deletion bindings/python/lyric-js-worker/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ description = "Add your description here"
authors = [
{ name = "Fangyin Cheng", email = "staneyffer@gmail.com" },
]
dependencies = []
dependencies = [
"lyric-task",
]
readme = "README.md"
requires-python = ">= 3.8"

Expand Down
12 changes: 9 additions & 3 deletions bindings/python/lyric-py/python/lyric/_py_lyric.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Generic, List, Literal, TypeVar, Union, final, Tuple
from typing import Any, Callable, Generic, List, Literal, Tuple, TypeVar, Union, final

from typing_extensions import Self

Expand Down Expand Up @@ -434,7 +434,6 @@ class PyTaskResourceConfig:
env_vars: Optional[List[Tuple[str, str]]] = None,
) -> None: ...


@final
class PyTaskHandle:
"""Handle for controlling and interacting with a running task.
Expand All @@ -447,6 +446,14 @@ class PyTaskHandle:
for task manipulation.
"""

def task_id(self) -> str:
"""Get the task ID for the handle.

Returns:
Task ID for the handle
"""
...

async def run(
self, args: PyTaskCallArgs, resources: Optional[PyTaskResourceConfig] = None
) -> PyDataObject:
Expand Down Expand Up @@ -583,7 +590,6 @@ class PyLyric:
If the node is a driver, this will stop the driver and all workers.
"""


def set_callback(self, callback: Callable[..., Any]) -> None: ...
"""Set a callback function for task worker.

Expand Down
1 change: 1 addition & 0 deletions bindings/python/lyric-py/python/lyric/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(
"PYTHON": default_worker,
"RUST": default_worker,
"JAVASCRIPT": default_worker,
"WASI": default_worker,
},
eventloop_worker_threads=eventloop_worker_threads,
log_level=log_level,
Expand Down
Loading
Loading