Skip to content
Open
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
123 changes: 123 additions & 0 deletions pkg_helper/package_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"""
Unit test for package building and installation.
Run with: python test_package_build.py
"""
import subprocess
import sys
import tempfile
import shutil
from pathlib import Path


def run_command(cmd, cwd=None):
"""Run shell command and return result."""
result = subprocess.run(
cmd,
shell=True,
capture_output=True,
text=True,
cwd=cwd
)
return result.returncode, result.stdout, result.stderr


def test_package_build_and_install():
"""Test that package builds and installs correctly."""
print("Testing package build and installation...\n")

# Get repo root (parent of tests/ directory)
repo_root = Path(__file__).parent.parent

# Step 1: Clean previous builds
print("1. Cleaning previous builds...")
for path in ["dist", "build", "*.egg-info"]:
for item in repo_root.glob(path):
if item.is_dir():
shutil.rmtree(item)
print(" Cleaned\n")

# Step 2: Build package
print("2. Building package...")
returncode, stdout, stderr = run_command("python -m build", cwd=repo_root)
if returncode != 0:
print(f" ❌ Build failed:")
print(f" stdout: {stdout}")
print(f" stderr: {stderr}")
return False
print(" Package built successfully")
if stdout:
print(f" {stdout.strip()}")
print()

# Step 3: Check dist files exist
print("3. Checking distribution files...")
dist_dir = repo_root / "dist"
if not dist_dir.exists():
print(" ❌ dist/ directory not found")
return False

whl_files = list(dist_dir.glob("*.whl"))
tar_files = list(dist_dir.glob("*.tar.gz"))

if not whl_files:
print(" ❌ No .whl file found")
return False
if not tar_files:
print(" ❌ No .tar.gz file found")
return False

print(f" Found: {whl_files[0].name}")
print(f" Found: {tar_files[0].name}\n")

# Step 4: Test installation in virtual environment
print("4. Testing installation in temp venv...")
with tempfile.TemporaryDirectory() as tmpdir:
venv_path = Path(tmpdir) / "test_venv"

# Create venv
returncode, _, stderr = run_command(f"python -m venv {venv_path}")
if returncode != 0:
print(f" ❌ venv creation failed:\n{stderr}")
return False

# Get pip path
pip_path = venv_path / "bin" / "pip"
if not pip_path.exists():
pip_path = venv_path / "Scripts" / "pip.exe" # Windows

# Install package
whl_path = whl_files[0].absolute()
returncode, _, stderr = run_command(f"{pip_path} install {whl_path}")
if returncode != 0:
print(f" ❌ Installation failed:\n{stderr}")
return False

print(" Package installed successfully\n")

# Step 5: Verify import
print("5. Verifying package import...")
python_path = venv_path / "bin" / "python"
if not python_path.exists():
python_path = venv_path / "Scripts" / "python.exe" # Windows

returncode, stdout, stderr = run_command(
f"{python_path} -c 'import sam2; print(\"sam2 imported successfully\")'")

if returncode != 0:
print(f" ❌ Import failed:\n{stderr}")
return False

print(f" {stdout.strip()}\n")

print("All tests passed! Package is ready for publishing.\n")
return True


if __name__ == "__main__":
try:
success = test_package_build_and_install()
sys.exit(0 if success else 1)
except Exception as e:
print(f"❌ Test failed with exception: {e}")
sys.exit(1)

41 changes: 41 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,44 @@ requires = [
"torch>=2.3.1",
]
build-backend = "setuptools.build_meta"

[project]
#package name rai-sam2 is taken on Test PyPi
name = "rai-sam2"
version = "1.1.2"
description = "SAM 2: Segment Anything in Images and Videos (RobotecAI fork)"
authors = [
{name = "Meta AI", email = "segment-anything@meta.com"},
{name = "Maciej Majek", email = "maciej.majek@robotec.ai"},
{name = "Bartłomiej Boczek", email = "bartlomiej.boczek@robotec.ai"}
]

license = {text = "Apache-2.0"}
readme = "README.md"
requires-python = ">=3.10"

dependencies = [
"torch>=2.3.1",
"torchvision>=0.18.1",
"numpy>=1.24.4",
"tqdm>=4.66.1",
"hydra-core>=1.3.2",
"iopath>=0.1.10",
"pillow>=9.4.0",
"opencv-python",
"supervision",
"transformers", # for Florence-2 and Grounding DINO HF models
"timm", # for Grounding DINO
]

[project.urls]
Homepage = "https://github.com/RobotecAI/Grounded-SAM-2"
Original = "https://github.com/IDEA-Research/Grounded-SAM-2"

[tool.setuptools.packages.find]
where = ["."]
include = ["sam2*", "sam2_configs*", "grounding_dino*", "utils*"]
exclude = ["notebooks*", "tests*", "sav_dataset*"]

[tool.setuptools.package-data]
"*" = ["*.yaml"]