Skip to content

Commit 6a82f76

Browse files
committed
WIP: buf build
1 parent bf48a48 commit 6a82f76

File tree

11 files changed

+463
-257
lines changed

11 files changed

+463
-257
lines changed

.github/workflows/build-golang-macos.yaml

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
name: Ubuntu build - buf edition
3+
on: [push]
4+
jobs:
5+
6+
build:
7+
runs-on: ubuntu-22.04
8+
permissions:
9+
contents: write
10+
11+
strategy:
12+
fail-fast: true
13+
matrix:
14+
go-version: [1.24.x]
15+
python3_version: [ "3.11", "3.12", "3.13" ]
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Setup Go
21+
uses: actions/setup-go@v4
22+
with:
23+
go-version: ${{ matrix.go-version }}
24+
cache-dependency-path: go.sum
25+
26+
# - name: Set up Python
27+
# uses: actions/setup-python@v4
28+
# with:
29+
# python-version: ${{ matrix.python3_version }}
30+
31+
- name: Install the latest version of uv
32+
uses: astral-sh/setup-uv@v6
33+
with:
34+
version: "latest"
35+
python-version: ${{ matrix.python3_version }}
36+
37+
# Download a release like https://github.com/bufbuild/buf/releases/download/v1.53.0/buf-Linux-x86_64
38+
- name: Install buf
39+
run: |
40+
BUF_VERSION="1.53.0"
41+
BUF_URL="https://github.com/bufbuild/buf/releases/download/v1.53.0/buf-Linux-x86_64"
42+
curl -sSL "$BUF_URL" -o /usr/local/bin/buf
43+
chmod +x /usr/local/bin/buf
44+
45+
- name: Add buf to PATH
46+
run: echo "/usr/local/bin" >> $GITHUB_PATH
47+
48+
- name: Check buf version
49+
run: buf --version
50+
51+
- name: Build using 'uv_buf_build_script.sh'
52+
run: |
53+
cd buf-build/
54+
chmod +x ./uv_buf_build_script.sh
55+
./uv_buf_build_script.sh

.github/workflows/build-golang-ubuntu.yaml

Lines changed: 0 additions & 65 deletions
This file was deleted.

.python-version

Lines changed: 0 additions & 1 deletion
This file was deleted.
File renamed without changes.

buf-build/list_wheel_modules.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import zipfile
2+
import sys
3+
import argparse # For better command-line argument handling
4+
5+
6+
def list_modules_in_wheel(wheel_path):
7+
"""
8+
Lists all Python modules (.py files) contained within a wheel file.
9+
10+
Args:
11+
wheel_path (str): The path to the .whl file.
12+
13+
Returns:
14+
list: A list of strings, where each string is the path to a .py file
15+
within the wheel archive. Returns None if an error occurs.
16+
"""
17+
modules = []
18+
try:
19+
# Check if the file exists and is a file
20+
# (zipfile might raise different errors depending on the OS for directories)
21+
import os
22+
23+
if not os.path.isfile(wheel_path):
24+
print(
25+
f"Error: Path '{wheel_path}' does not exist or is not a file.",
26+
file=sys.stderr,
27+
)
28+
return None
29+
30+
# Open the wheel file (which is essentially a zip archive) in read mode ('r')
31+
with zipfile.ZipFile(wheel_path, "r") as zf:
32+
# Get a list of all archive members (files and directories)
33+
all_files = zf.namelist()
34+
35+
# Filter the list to include only files ending with '.py'
36+
modules = [name for name in all_files if name.endswith(".py")]
37+
38+
return modules
39+
40+
except zipfile.BadZipFile:
41+
print(
42+
f"Error: '{wheel_path}' is not a valid zip file or wheel file.",
43+
file=sys.stderr,
44+
)
45+
return None
46+
except FileNotFoundError:
47+
# This might be redundant due to the os.path.isfile check, but good practice
48+
print(f"Error: File not found at '{wheel_path}'", file=sys.stderr)
49+
return None
50+
except Exception as e:
51+
print(f"An unexpected error occurred: {e}", file=sys.stderr)
52+
return None
53+
54+
55+
# --- Example Usage (when running the script directly) ---
56+
if __name__ == "__main__":
57+
# Set up command-line argument parsing
58+
parser = argparse.ArgumentParser(
59+
description="List Python modules (.py files) found inside a wheel (.whl) file."
60+
)
61+
parser.add_argument("wheel_file", help="The path to the .whl file to inspect.")
62+
args = parser.parse_args()
63+
64+
# Get the list of modules
65+
module_list = list_modules_in_wheel(args.wheel_file)
66+
67+
# Print the results
68+
if module_list is not None: # Check if the function execution was successful
69+
if module_list:
70+
print(f"Found {len(module_list)} module(s) in '{args.wheel_file}':")
71+
for module_path in module_list:
72+
print(f"- {module_path}")
73+
else:
74+
print(f"No .py modules found in '{args.wheel_file}'.")
75+
sys.exit(0) # Exit with success code
76+
else:
77+
sys.exit(1) # Exit with error code

buf-build/setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name="otdf_python",
5+
version="0.2.11",
6+
author="b-long",
7+
author_email="[email protected]",
8+
description="Buf generated SDK for OpenTDF",
9+
packages=find_packages("gen"),
10+
package_dir={"": "gen"},
11+
)

build-scripts/uv_buf_build_script.sh renamed to buf-build/uv_buf_build_script.sh

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
# Based on the excellent work of the sift-stack team:
4+
# https://github.com/sift-stack/sift/blob/main/docs/python.md
35
set -x
46
set -eou pipefail
57

@@ -17,13 +19,15 @@ loud_print(){
1719

1820
# Based on: https://stackoverflow.com/a/246128
1921
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
20-
BUILD_ROOT="${SCRIPT_DIR}/buf-build"
22+
BUILD_ROOT="${SCRIPT_DIR}/buf-build-generated"
23+
TEST_ROOT="${SCRIPT_DIR}/buf-build-test"
2124

2225
# Cleanup previous builds
2326
rm -rf .venv-wheel/
2427
rm -rf .venv/
2528
rm -rf dist/
2629
rm -rf "${BUILD_ROOT}"
30+
rm -rf "${TEST_ROOT}"
2731

2832
mkdir -p "${BUILD_ROOT}" || { echo "Unable to create build root directory" ; exit 1; }
2933
cd "${BUILD_ROOT}" || { echo "Unable to change to build root directory" ; exit 1; }
@@ -43,12 +47,29 @@ if ! command -v go &> /dev/null; then
4347
fi
4448

4549

50+
# Shallow clone the 'https://github.com/opentdf/platform.git' repo
51+
git clone --depth 1 https://github.com/opentdf/platform.git
52+
53+
cd platform || { echo "Unable to change to platform directory" ; exit 1; }
54+
buf export --output=$BUILD_ROOT/protos --config buf.yaml
55+
56+
cd $SCRIPT_DIR || { echo "Unable to change to script directory" ; exit 1; }
57+
cp buf.gen.python.yaml $BUILD_ROOT/buf.gen.yaml
58+
cp setup.py $BUILD_ROOT/setup.py
59+
60+
cd "${BUILD_ROOT}" || { echo "Unable to change to build root directory" ; exit 1; }
61+
buf generate protos || { echo "buf generate failed" ; exit 1; }
62+
63+
for dir in $(find gen -type d); do
64+
touch $dir/__init__.py
65+
done
66+
4667
# PY_TYPE="--python-preference=only-system"
4768
PY_TYPE="--python-preference=only-managed"
4869

4970
loud_print "Creating virtual environment"
5071
# Install python deps
51-
uv venv .venv --python 3.12 "$PY_TYPE"
72+
uv venv .venv "$PY_TYPE"
5273

5374
if ! [ -d "${BUILD_ROOT}/.venv" ]; then
5475
echo "Unable to locate virtual environment directory"
@@ -58,9 +79,26 @@ fi
5879
loud_print "Activating virtual environment"
5980
source "${BUILD_ROOT}/.venv/bin/activate"
6081

82+
uv pip install build protobuf grpcio
83+
python -m build --sdist || { echo "Failed to build source distribution" ; exit 1; }
84+
python -m build --wheel || { echo "Failed to build wheel distribution" ; exit 1; }
6185

62-
# Shallow clone the 'https://github.com/opentdf/platform.git' repo
63-
git clone --depth 1 https://github.com/opentdf/platform.git
86+
echo "Build completed successfully."
87+
echo "Directory contents:"
88+
ls -lart
89+
echo "Dist directory contents:"
90+
ls -lart dist/
6491

65-
cd platform || { echo "Unable to change to platform directory" ; exit 1; }
66-
buf export --output=$BUILD_ROOT/protos --config buf.yaml
92+
93+
loud_print "Testing new wheel"
94+
95+
mkdir -p "${TEST_ROOT}" || { echo "Unable to create test root directory" ; exit 1; }
96+
cd "${TEST_ROOT}" || { echo "Unable to change to test root directory" ; exit 1; }
97+
uv venv .venv-wheel
98+
source "${TEST_ROOT}/.venv-wheel/bin/activate"
99+
100+
echo "Ensuring wheel can be installed"
101+
uv pip install ${BUILD_ROOT}/dist/*.whl || { echo "Failed to install wheel" ; exit 1; }
102+
103+
loud_print "Listing all wheel modules"
104+
uvx python ${SCRIPT_DIR}/list_wheel_modules.py ${BUILD_ROOT}/dist/*.whl || { echo "Failed to list wheel modules" ; exit 1; }

0 commit comments

Comments
 (0)