Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support disabling telemetry with an environment variable, load dotenv on run_project #109

Merged
merged 7 commits into from
Dec 9, 2024
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
5 changes: 4 additions & 1 deletion agentstack/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import os
import importlib.resources
from cookiecutter.main import cookiecutter
from dotenv import load_dotenv
import subprocess

from .agentstack_data import (
FrameworkData,
Expand Down Expand Up @@ -184,8 +186,9 @@ def run_project(framework: str, path: str = ''):
print(e)
sys.exit(1)

load_dotenv(_path / '.env') # explicitly load the project's .env file
entrypoint = _path / frameworks.get_entrypoint_path(framework)
os.system(f'python {entrypoint}')
subprocess.run(['python', entrypoint], env=os.environ)


def ask_framework() -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import sys
from crew import {{cookiecutter.project_metadata.project_name|replace('-', '')|replace('_', '')|capitalize}}Crew
import agentops
from dotenv import load_dotenv
load_dotenv()

agentops.init(default_tags=['crewai', 'agentstack'])

Expand Down
9 changes: 8 additions & 1 deletion agentstack/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Optional
import sys
import os, sys
import json
from ruamel.yaml import YAML
import re
Expand Down Expand Up @@ -54,9 +54,16 @@ def get_framework(path: Optional[str] = None) -> str:


def get_telemetry_opt_out(path: Optional[str] = None) -> bool:
"""
Gets the telemetry opt out setting.
First checks the environment variable AGENTSTACK_TELEMETRY_OPT_OUT.
If that is not set, it checks the agentstack.json file.
"""
from agentstack.generation import ConfigFile

try:
return bool(os.environ['AGENTSTACK_TELEMETRY_OPT_OUT'])
except KeyError:
agentstack_config = ConfigFile(path)
return bool(agentstack_config.telemetry_opt_out)
except FileNotFoundError:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies = [
"packaging==23.2",
"requests>=2.32",
"appdirs>=1.4.4",
"python-dotenv>=1.0.1",
]

[project.optional-dependencies]
Expand Down
5 changes: 0 additions & 5 deletions tests/test_generation_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ def test_get_framework(self):
with self.assertRaises(SystemExit) as _:
get_framework(BASE_PATH / "missing")

def test_get_telemetry_opt_out(self):
assert get_telemetry_opt_out(BASE_PATH / "fixtures") is False
with self.assertRaises(SystemExit) as _:
get_telemetry_opt_out(BASE_PATH / "missing")

def test_read_env(self):
env = EnvFile(BASE_PATH / "fixtures")
assert env.variables == {"ENV_VAR1": "value1", "ENV_VAR2": "value2"}
Expand Down
46 changes: 46 additions & 0 deletions tests/test_project_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
from pathlib import Path
import shutil
import unittest
from parameterized import parameterized_class

from agentstack import frameworks
from agentstack.cli import run_project
from agentstack.generation.files import ConfigFile

BASE_PATH = Path(__file__).parent


@parameterized_class([{"framework": framework} for framework in frameworks.SUPPORTED_FRAMEWORKS])
class ProjectRunTest(unittest.TestCase):
def setUp(self):
self.project_dir = BASE_PATH / 'tmp' / self.framework

os.makedirs(self.project_dir)
os.makedirs(self.project_dir / 'src')
(self.project_dir / 'src' / '__init__.py').touch()

# set the framework in agentstack.json
shutil.copy(BASE_PATH / 'fixtures' / 'agentstack.json', self.project_dir / 'agentstack.json')
with ConfigFile(self.project_dir) as config:
config.framework = self.framework

# populate the entrypoint
entrypoint_path = frameworks.get_entrypoint_path(self.framework, self.project_dir)
shutil.copy(BASE_PATH / f"fixtures/frameworks/{self.framework}/entrypoint_max.py", entrypoint_path)

# write a basic .env file
shutil.copy(BASE_PATH / 'fixtures' / '.env', self.project_dir / '.env')

def tearDown(self):
shutil.rmtree(self.project_dir)

def test_run_project(self):
run_project(self.framework, self.project_dir)

def test_env_is_set(self):
"""
After running a project, the environment variables should be set from project_dir/.env.
"""
run_project(self.framework, self.project_dir)
assert os.getenv('ENV_VAR1') == 'value1'
12 changes: 12 additions & 0 deletions tests/test_telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
import unittest
from agentstack.utils import get_telemetry_opt_out


class TelemetryTest(unittest.TestCase):
def test_telemetry_opt_out_env_var_set(self):
AGENTSTACK_TELEMETRY_OPT_OUT = os.getenv("AGENTSTACK_TELEMETRY_OPT_OUT")
assert AGENTSTACK_TELEMETRY_OPT_OUT

def test_telemetry_opt_out_set_in_test_environment(self):
assert get_telemetry_opt_out()
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ deps =
mypy: mypy
commands =
pytest -v
mypy: mypy agentops
mypy: mypy agentops
setenv =
AGENTSTACK_TELEMETRY_OPT_OUT = 1
Loading