Skip to content

Commit

Permalink
Fix bug in agentstack init on python3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
tcdent committed Nov 28, 2024
1 parent 33d8a4e commit df31b5e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
23 changes: 12 additions & 11 deletions agentstack/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .agentstack_data import FrameworkData, ProjectMetadata, ProjectStructure, CookiecutterData
from agentstack.logger import log
from agentstack.utils import get_package_path
from agentstack.generation.tool_generation import get_all_tools
from .. import generation
from ..utils import open_json_file, term_color, is_snake_case
Expand Down Expand Up @@ -278,20 +279,20 @@ def insert_template(project_details: dict, framework_name: str, design: dict):
structure=project_structure,
framework=framework_name.lower())

with importlib.resources.path(f'agentstack.templates', str(framework.name)) as template_path:
with open(f"{template_path}/cookiecutter.json", "w") as json_file:
json.dump(cookiecutter_data.to_dict(), json_file)
template_path = get_package_path() / f'templates/{framework.name}'
with open(f"{template_path}/cookiecutter.json", "w") as json_file:
json.dump(cookiecutter_data.to_dict(), json_file)

# copy .env.example to .env
shutil.copy(
f'{template_path}/{"{{cookiecutter.project_metadata.project_slug}}"}/.env.example',
f'{template_path}/{"{{cookiecutter.project_metadata.project_slug}}"}/.env')
# copy .env.example to .env
shutil.copy(
f'{template_path}/{"{{cookiecutter.project_metadata.project_slug}}"}/.env.example',
f'{template_path}/{"{{cookiecutter.project_metadata.project_slug}}"}/.env')

if os.path.isdir(project_details['name']):
print(term_color(f"Directory {template_path} already exists. Please check this and try again", "red"))
return
if os.path.isdir(project_details['name']):
print(term_color(f"Directory {template_path} already exists. Please check this and try again", "red"))
return

cookiecutter(str(template_path), no_input=True, extra_context=None)
cookiecutter(str(template_path), no_input=True, extra_context=None)

# TODO: inits a git repo in the directory the command was run in
# TODO: not where the project is generated. Fix this
Expand Down
6 changes: 1 addition & 5 deletions agentstack/generation/tool_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import fileinput
from pydantic import BaseModel, ValidationError

from agentstack.utils import get_package_path
from .gen_utils import insert_code_after_tag, string_in_file
from ..utils import open_json_file, get_framework, term_color

Expand All @@ -17,11 +18,6 @@
'crewai': 'src/crew.py',
}

def get_package_path() -> Path:
if sys.version_info <= (3, 9):
return Path(sys.modules['agentstack'].__path__[0])
return importlib.resources.files('agentstack')

def get_framework_filename(framework: str, path: str = ''):
if path:
path = path.endswith('/') and path or path + '/'
Expand Down
9 changes: 9 additions & 0 deletions agentstack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import json
import re
from importlib.metadata import version
from pathlib import Path
import importlib.resources


def get_version():
Expand All @@ -23,6 +25,13 @@ def verify_agentstack_project():
sys.exit(1)


def get_package_path() -> Path:
"""This is the Path where agentstack is installed."""
if sys.version_info <= (3, 9):
return Path(sys.modules['agentstack'].__path__[0])
return importlib.resources.files('agentstack')


def get_framework(path: Optional[str] = None) -> str:
try:
file_path = 'agentstack.json'
Expand Down

0 comments on commit df31b5e

Please sign in to comment.