Skip to content
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
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ DEV_MODE ?= 1

# Rust crate path
FASTLY_COMPUTE_PY_MANIFEST := $(abspath crates/fastly-compute-py/Cargo.toml)
FASTLY_COMPUTE_PY_BIN := target/release/fastly_compute_py_build

# Select build tool based on DEV_MODE
ifeq ($(DEV_MODE),1)
Expand All @@ -28,7 +27,7 @@ EXAMPLES_DIR := examples
COMPUTE_WIT := wit/deps/fastly/compute.wit

# Define all available examples (add new ones here)
EXAMPLES := bottle-app flask-app backend-requests game-of-life config-store logging
EXAMPLES := bottle-app flask-app backend-requests game-of-life logging

# Default example for serve target
EXAMPLE ?= bottle-app
Expand Down
5 changes: 5 additions & 0 deletions crates/fastly-compute-py/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ pub enum Command {
/// Entry point module (default: main or auto-detect)
#[arg(short, long)]
entry: Option<String>,

/// Virtual environment in which to look for modules (default:
/// VIRTUAL_ENV env var or .venv)
#[arg(short, long)]
virtualenv: Option<PathBuf>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I understand, this was needed due to the acrobatics we're doing in the test harness and probably not likely to be needed by users normally (it is not for componentize-py which is the functionality I was largely reproducing).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. It allows us to build against the venv the testrunner is running under, which elegantly has exactly the packages we need.

},
}
19 changes: 15 additions & 4 deletions crates/fastly-compute-py/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::cli::Command;
pub struct ConfigSource {
pub entry: Option<String>,
pub output: Option<PathBuf>,
pub virtualenv: Option<PathBuf>,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -61,12 +62,17 @@ impl ConfigBuilder {
/// Add CLI command arguments as a configuration source
pub fn with_command(mut self, command: &Command) -> Self {
match command {
Command::Build { entry, output } => {
if entry.is_some() || output.is_some() {
log::debug!("Config from CLI: entry={entry:?}, output={output:?}");
Command::Build {
entry,
output,
virtualenv,
} => {
if entry.is_some() || output.is_some() || virtualenv.is_some() {
log::debug!("Config from CLI: entry={entry:?}, output={output:?}, virtualenv={virtualenv:?}");
}
self.cli.entry = entry.clone();
self.cli.output = output.clone();
self.cli.virtualenv = virtualenv.clone();
}
}
self
Expand All @@ -89,12 +95,17 @@ impl ConfigBuilder {
PathBuf::from("bin/main.wasm")
});

Config { entry, output }
Config {
entry,
output,
virtualenv: self.cli.virtualenv,
}
}
}

#[derive(Debug)]
pub struct Config {
pub entry: String,
pub output: PathBuf,
pub virtualenv: Option<PathBuf>,
}
6 changes: 3 additions & 3 deletions crates/fastly-compute-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn run_main(cli: &Cli) -> Result<()> {
log::info!(" Entry point: {}", config.entry);
log::info!(" Output: {}", config.output.display());

build(config.output.clone(), config.entry)?;
build(config.output.clone(), config.entry, config.virtualenv)?;

log::info!("✓ Build complete: {}", config.output.display());

Expand All @@ -118,7 +118,7 @@ fn _fastly_compute_py(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
Ok(())
}

pub fn build(output: PathBuf, entry_name: String) -> Result<()> {
pub fn build(output: PathBuf, entry_name: String, virtualenv: Option<PathBuf>) -> Result<()> {
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path();

Expand All @@ -132,7 +132,7 @@ pub fn build(output: PathBuf, entry_name: String) -> Result<()> {
let temp_component_wasm_path = temp_path.join("component.wasm");

log::info!(" Resolving Python dependencies...");
let python_path = site_packages::build_python_path()?;
let python_path = site_packages::build_python_path(&virtualenv)?;
log::debug!("Using python_path: {:?}", python_path);

let python_path_refs: Vec<&str> = python_path.iter().map(|s| s.as_str()).collect();
Expand Down
8 changes: 4 additions & 4 deletions crates/fastly-compute-py/src/site_packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use std::path::{Path, PathBuf};

/// Build a python_path list suitable for componentize-py.
/// This includes the current directory and all site-packages paths.
pub fn build_python_path() -> Result<Vec<String>> {
pub fn build_python_path(virtualenv: &Option<PathBuf>) -> Result<Vec<String>> {
let cwd = env::current_dir()?;
log::debug!("Current directory: {}", cwd.display());

let mut python_path = vec![cwd.to_string_lossy().to_string()];

if let Some(site_packages) = find_site_packages()? {
if let Some(site_packages) = find_site_packages(virtualenv)? {
log::debug!(
"Adding site-packages to python_path: {}",
site_packages.display()
Expand All @@ -38,8 +38,8 @@ pub fn build_python_path() -> Result<Vec<String>> {
}

/// Find the site-packages directory within a virtualenv.
pub fn find_site_packages() -> Result<Option<PathBuf>> {
let venv_path = find_venv()?;
pub fn find_site_packages(virtualenv: &Option<PathBuf>) -> Result<Option<PathBuf>> {
let venv_path = virtualenv.to_owned().or(find_venv()?);

if let Some(venv) = venv_path {
log::debug!("Found virtualenv: {}", venv.display());
Expand Down
1 change: 1 addition & 0 deletions examples/backend-requests/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/bottle-app/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 0 additions & 65 deletions examples/config-store/config-store.py

This file was deleted.

15 changes: 0 additions & 15 deletions examples/config-store/pyproject.toml

This file was deleted.

49 changes: 0 additions & 49 deletions examples/config-store/uv.lock

This file was deleted.

1 change: 1 addition & 0 deletions examples/flask-app/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/game-of-life/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion examples/logging/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions fastly_compute/config_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
api_url = config.get("api_url", "https://api.example.com")
"""

from typing import Self

from wit_world.imports import config_store as wit_config_store

# The maximum value for a u32, used to signal that we don't want to cap
Expand All @@ -36,7 +38,7 @@ def __init__(self, store: wit_config_store.Store):
self._store = store

@classmethod
def open(cls, name: str) -> "ConfigStore":
def open(cls, name: str) -> Self:
"""Open a config store by name.

:param name: The name of the config store
Expand Down Expand Up @@ -101,7 +103,7 @@ def close(self) -> None:
"""
self._store.__exit__(None, None, None)

def __enter__(self) -> "ConfigStore":
def __enter__(self) -> Self:
"""Context manager entry.

Allows use of ConfigStore in a 'with' statement.
Expand Down
6 changes: 2 additions & 4 deletions fastly_compute/requests/timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
requests-compatible timeouts and Fastly-specific granular timeout controls.
"""

from typing import override
from typing import Self, override


class TimeoutConfig:
Expand Down Expand Up @@ -52,9 +52,7 @@ def between_bytes_ms(self) -> int:
return int(self.between_bytes * 1000)

@classmethod
def from_requests_timeout(
cls, timeout: None | float | tuple[float, float]
) -> "TimeoutConfig":
def from_requests_timeout(cls, timeout: None | float | tuple[float, float]) -> Self:
"""Create TimeoutConfig from requests-compatible timeout parameter.

Args:
Expand Down
Loading
Loading