Skip to content

Commit

Permalink
Make requirements in deploy_to_gcp optional (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Feb 14, 2024
1 parent 33a2194 commit e6a0d7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 14 additions & 4 deletions prediction_market_agent_tooling/deploy/gcp/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import subprocess
import tempfile
import typing as t
from prediction_market_agent_tooling.deploy.agent import DeployableAgent
from prediction_market_agent_tooling.tools.utils import export_requirements_from_toml
from prediction_market_agent_tooling.deploy.gcp.utils import (
Expand All @@ -20,13 +21,13 @@

def deploy_to_gcp(
function_file: str,
requirements_file: str,
requirements_file: t.Optional[str],
extra_deps: list[str],
api_keys: dict[str, str],
market_type: MarketType,
memory: int, # in MB
) -> str:
if not os.path.exists(requirements_file):
if requirements_file and not os.path.exists(requirements_file):
raise ValueError(f"File {requirements_file} does not exist")

if not os.path.exists(function_file):
Expand All @@ -40,11 +41,20 @@ def deploy_to_gcp(
shutil.copy(function_file, f"{tempdir}/main.py")

# If the file is a .toml file, convert it to a requirements.txt file
if requirements_file.endswith(".toml"):
export_requirements_from_toml(output_dir=tempdir, extra_deps=extra_deps)
if requirements_file is None:
# Just create an empty file.
with open(f"{tempdir}/requirements.txt", "w"):
pass
elif requirements_file.endswith(".toml"):
export_requirements_from_toml(output_dir=tempdir)
else:
shutil.copy(requirements_file, f"{tempdir}/requirements.txt")

if extra_deps:
with open(f"{tempdir}/requirements.txt", "a") as f:
for dep in extra_deps:
f.write(f"{dep}\n")

# Create the topic used to trigger the function. Note we use the
# convention that the topic name is the same as the function name
subprocess.run(gcloud_create_topic_cmd(gcp_fname), shell=True)
Expand Down
6 changes: 1 addition & 5 deletions prediction_market_agent_tooling/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,12 @@ def should_not_happen(
raise exp(msg)


def export_requirements_from_toml(output_dir: str, extra_deps: list[str] = []) -> None:
def export_requirements_from_toml(output_dir: str) -> None:
if not os.path.exists(output_dir):
raise ValueError(f"Directory {output_dir} does not exist")
output_file = f"{output_dir}/requirements.txt"
subprocess.run(
f"poetry export -f requirements.txt --without-hashes --output {output_file}",
shell=True,
)
if extra_deps:
with open(output_file, "a") as f:
for dep in extra_deps:
f.write(f"{dep}\n")
print(f"Saved requirements to {output_dir}/requirements.txt")

0 comments on commit e6a0d7f

Please sign in to comment.