Skip to content

Commit

Permalink
Merge pull request #192 from zabirauf/u/zabirauf/pip-reqs
Browse files Browse the repository at this point in the history
Adding ability to install requirements from frontmatter through the /pipelines/add API
  • Loading branch information
tjbck authored Aug 11, 2024
2 parents 272cf8c + 042a3f8 commit 45e3a21
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import json
import uuid
import sys
import subprocess


from config import API_KEY, PIPELINES_DIR
Expand Down Expand Up @@ -105,12 +106,45 @@ def get_all_pipelines():

return pipelines

def parse_frontmatter(content):
frontmatter = {}
for line in content.split('\n'):
if ':' in line:
key, value = line.split(':', 1)
frontmatter[key.strip().lower()] = value.strip()
return frontmatter

def install_frontmatter_requirements(requirements):
if requirements:
req_list = [req.strip() for req in requirements.split(',')]
for req in req_list:
print(f"Installing requirement: {req}")
subprocess.check_call([sys.executable, "-m", "pip", "install", req])
else:
print("No requirements found in frontmatter.")

async def load_module_from_path(module_name, module_path):
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)

try:
# Read the module content
with open(module_path, 'r') as file:
content = file.read()

# Parse frontmatter
frontmatter = {}
if content.startswith('"""'):
end = content.find('"""', 3)
if end != -1:
frontmatter_content = content[3:end]
frontmatter = parse_frontmatter(frontmatter_content)

# Install requirements if specified
if 'requirements' in frontmatter:
install_frontmatter_requirements(frontmatter['requirements'])

# Load the module
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
print(f"Loaded module: {module.__name__}")
if hasattr(module, "Pipeline"):
Expand Down

0 comments on commit 45e3a21

Please sign in to comment.