Skip to content

Commit 8ccc868

Browse files
committed
fix linting
1 parent 9959e34 commit 8ccc868

8 files changed

+116
-61
lines changed

examples/anthropic_tool_use.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
from __future__ import annotations
44

5+
from typing import cast
6+
57
from anthropic import Anthropic
68
from anthropic.types import ToolParam, MessageParam
79

810
import gitpod.lib as util
911
from gitpod import AsyncGitpod
12+
from gitpod.types.environment_initializer_param import Spec
1013

1114
gpclient = AsyncGitpod()
1215
llmclient = Anthropic()
@@ -38,29 +41,40 @@ async def create_environment(args: dict[str, str], cleanup: util.Disposables) ->
3841
env_class = await util.find_most_used_environment_class(gpclient)
3942
if not env_class:
4043
raise Exception("No environment class found. Please create one first.")
41-
environment_id = (await gpclient.environments.create(
44+
env_class_id = env_class.id
45+
assert env_class_id is not None
46+
47+
environment = (await gpclient.environments.create(
4248
spec={
4349
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
4450
"content": {
45-
"initializer": {"specs": [{"contextUrl": {"url": args["context_url"]}}]},
51+
"initializer": {"specs": [Spec(
52+
context_url={
53+
"url": args["context_url"]
54+
}
55+
)]},
4656
},
47-
"machine": {"class": env_class.id},
57+
"machine": {"class": env_class_id},
4858
}
49-
)).environment.id
50-
cleanup.add(lambda: asyncio.run(gpclient.environments.delete(environment_id=environment_id)))
59+
)).environment
60+
assert environment is not None
61+
environment_id = environment.id
62+
assert environment_id is not None
63+
cleanup.add(lambda: asyncio.run(gpclient.environments.delete(environment_id=environment_id)))
64+
5165
print(f"\nCreated environment: {environment_id} - waiting for it to be ready...")
5266
await util.wait_for_environment_ready(gpclient, environment_id)
5367
print(f"\nEnvironment is ready: {environment_id}")
5468
return environment_id
5569

5670
async def execute_command(args: dict[str, str]) -> str:
5771
lines_iter = await util.run_command(gpclient, args["environment_id"], args["command"])
58-
lines = []
72+
lines: list[str] = []
5973
async for line in lines_iter:
6074
lines.append(line)
6175
return "\n".join(lines)
6276

63-
async def main(cleanup: util.Disposables):
77+
async def main(cleanup: util.Disposables) -> None:
6478
messages = [user_message]
6579
while True:
6680
message = llmclient.messages.create(
@@ -83,7 +97,8 @@ async def main(cleanup: util.Disposables):
8397
for tool in (c for c in message.content if c.type == "tool_use"):
8498
try:
8599
if tool.name == "create_environment":
86-
environment_id = await create_environment(tool.input, cleanup)
100+
args = cast(dict[str, str], tool.input)
101+
environment_id = await create_environment(args, cleanup)
87102
messages.append({
88103
"role": "user",
89104
"content": [{
@@ -93,7 +108,8 @@ async def main(cleanup: util.Disposables):
93108
}],
94109
})
95110
elif tool.name == "execute_command":
96-
output = await execute_command(tool.input)
111+
args = cast(dict[str, str], tool.input)
112+
output = await execute_command(args)
97113
messages.append({
98114
"role": "user",
99115
"content": [{

examples/fs_access.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import gitpod.lib as util
1010
from gitpod import AsyncGitpod
1111
from gitpod.types.environment_spec_param import EnvironmentSpecParam
12+
from gitpod.types.environment_initializer_param import Spec
1213

1314

1415
# Examples:
@@ -24,6 +25,8 @@ async def main(cleanup: util.Disposables) -> None:
2425
print("Error: No environment class found. Please create one first.")
2526
sys.exit(1)
2627
print(f"Found environment class: {env_class.display_name} ({env_class.description})")
28+
env_class_id = env_class.id
29+
assert env_class_id is not None
2730

2831
print("Generating SSH key pair")
2932
key = paramiko.RSAKey.generate(2048)
@@ -36,22 +39,26 @@ async def main(cleanup: util.Disposables) -> None:
3639
key_id = "fs-access-example"
3740
spec: EnvironmentSpecParam = {
3841
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
39-
"machine": {"class": env_class.id},
42+
"machine": {"class": env_class_id},
4043
"ssh_public_keys": [{
4144
"id": key_id,
4245
"value": public_key
4346
}]
4447
}
4548
if context_url:
4649
spec["content"] = {
47-
"initializer": {"specs": [{
48-
"contextUrl": {
50+
"initializer": {"specs": [Spec(
51+
context_url={
4952
"url": context_url
5053
}
51-
}]}
54+
)]}
5255
}
5356

54-
environment_id = (await client.environments.create(spec=spec)).environment.id
57+
print("Creating environment")
58+
environment = (await client.environments.create(spec=spec)).environment
59+
assert environment is not None
60+
environment_id = environment.id
61+
assert environment_id is not None
5562
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
5663

5764
env = util.EnvironmentState(client, environment_id)
@@ -70,8 +77,8 @@ async def main(cleanup: util.Disposables) -> None:
7077
# Parse ssh://username@host:port format
7178
url_parts = ssh_url.split('://')[-1]
7279
username, rest = url_parts.split('@')
73-
host, port = rest.split(':')
74-
port = int(port)
80+
host, port_str = rest.split(':')
81+
port = int(port_str)
7582

7683
ssh = paramiko.SSHClient()
7784
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
@@ -94,7 +101,7 @@ async def main(cleanup: util.Disposables) -> None:
94101

95102
with sftp.file('test.txt', 'r') as f:
96103
content = f.read()
97-
print(f"File content: {content}")
104+
print(f"File content: {content.decode()}")
98105

99106
if __name__ == "__main__":
100107
disposables = util.Disposables()

examples/run_command.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import gitpod.lib as util
77
from gitpod import AsyncGitpod
88
from gitpod.types.environment_spec_param import EnvironmentSpecParam
9+
from gitpod.types.environment_initializer_param import Spec
910

1011

1112
# Examples:
@@ -26,22 +27,27 @@ async def main(cleanup: util.Disposables) -> None:
2627
print("Error: No environment class found. Please create one first.")
2728
sys.exit(1)
2829
print(f"Found environment class: {env_class.display_name} ({env_class.description})")
30+
env_class_id = env_class.id
31+
assert env_class_id is not None
2932

3033
spec: EnvironmentSpecParam = {
3134
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
32-
"machine": {"class": env_class.id},
35+
"machine": {"class": env_class_id},
3336
}
3437
if context_url:
3538
spec["content"] = {
36-
"initializer": {"specs": [{
37-
"contextUrl": {
39+
"initializer": {"specs": [Spec(
40+
context_url={
3841
"url": context_url
3942
}
40-
}]}
43+
)]}
4144
}
4245

4346
print("Creating environment")
44-
environment_id = (await client.environments.create(spec=spec)).environment.id
47+
environment = (await client.environments.create(spec=spec)).environment
48+
assert environment is not None
49+
environment_id = environment.id
50+
assert environment_id is not None
4551
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
4652

4753
print("Waiting for environment to be ready")

examples/run_service.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import gitpod.lib as util
77
from gitpod import AsyncGitpod
88
from gitpod.types.environment_spec_param import EnvironmentSpecParam
9+
from gitpod.types.environment_initializer_param import Spec
910

1011

1112
# Examples:
@@ -21,11 +22,13 @@ async def main(cleanup: util.Disposables) -> None:
2122
print("Error: No environment class found. Please create one first.")
2223
sys.exit(1)
2324
print(f"Found environment class: {env_class.display_name} ({env_class.description})")
24-
25+
env_class_id = env_class.id
26+
assert env_class_id is not None
27+
2528
port = 8888
2629
spec: EnvironmentSpecParam = {
2730
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
28-
"machine": {"class": env_class.id},
31+
"machine": {"class": env_class_id},
2932
"ports": [{
3033
"name": "Lama Service",
3134
"port": port,
@@ -34,15 +37,18 @@ async def main(cleanup: util.Disposables) -> None:
3437
}
3538
if context_url:
3639
spec["content"] = {
37-
"initializer": {"specs": [{
38-
"contextUrl": {
39-
"url": context_url
40-
}
41-
}]}
42-
}
40+
"initializer": {"specs": [Spec(
41+
context_url={
42+
"url": context_url
43+
}
44+
)]}
45+
}
4346

4447
print("Creating environment")
45-
environment_id = (await client.environments.create(spec=spec)).environment.id
48+
environment = (await client.environments.create(spec=spec)).environment
49+
assert environment is not None
50+
environment_id = environment.id
51+
assert environment_id is not None
4652
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
4753

4854
print("Waiting for environment to be ready")

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dev-dependencies = [
5858
"nest_asyncio==1.6.0",
5959
"paramiko>=3.5.1",
6060
"anthropic>=0.45.2",
61+
"types-paramiko>=3.5.0.20240928",
6162
]
6263

6364
[tool.rye.scripts]

requirements-dev.lock

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ colorlog==6.7.0
3030
# via nox
3131
cryptography==44.0.1
3232
# via paramiko
33+
# via types-paramiko
3334
dirty-equals==0.6.0
3435
distlib==0.3.7
3536
# via virtualenv
@@ -111,6 +112,7 @@ time-machine==2.9.0
111112
tomli==2.0.2
112113
# via mypy
113114
# via pytest
115+
types-paramiko==3.5.0.20240928
114116
typing-extensions==4.12.2
115117
# via anthropic
116118
# via anyio

src/gitpod/lib/automation.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def run_service(
1414
metadata: service_create_params.ServiceMetadataParam,
1515
spec: service_create_params.ServiceSpecParam
1616
) -> AsyncIterator[str]:
17-
reference = metadata["reference"]
17+
reference = metadata.get("reference")
1818
if not reference:
1919
raise ValueError("metadata.reference is required")
2020

@@ -25,15 +25,18 @@ async def run_service(
2525
}
2626
)).services
2727

28-
service_id = ""
28+
service_id: Optional[str] = None
2929
if not services:
30-
service_id = (await client.environments.automations.services.create(
30+
service = (await client.environments.automations.services.create(
3131
environment_id=environment_id,
3232
spec=spec,
3333
metadata=metadata
34-
)).service.id
34+
)).service
35+
assert service is not None
36+
service_id = service.id
3537
else:
3638
service_id = services[0].id
39+
assert service_id is not None
3740

3841
await client.environments.automations.services.start(id=service_id)
3942
log_url = await wait_for_service_log_url(client, environment_id, service_id)
@@ -47,9 +50,9 @@ async def run_command(client: AsyncGitpod, environment_id: str, command: str) ->
4750
}
4851
)).tasks
4952

50-
task_id = ""
53+
task_id: Optional[str] = None
5154
if not tasks:
52-
task_id = (await client.environments.automations.tasks.create(
55+
task = (await client.environments.automations.tasks.create(
5356
spec={
5457
"command": command,
5558
},
@@ -59,30 +62,40 @@ async def run_command(client: AsyncGitpod, environment_id: str, command: str) ->
5962
"description": "Gitpod Python SDK Task",
6063
"reference": TASK_REFERENCE,
6164
},
62-
)).task.id
65+
)).task
66+
assert task is not None
67+
task_id = task.id
6368
else:
6469
task_id = tasks[0].id
70+
assert task_id is not None
6571
await client.environments.automations.tasks.update(
6672
id=task_id,
6773
spec={
6874
"command": command,
6975
},
7076
)
71-
72-
task_execution_id = (await client.environments.automations.tasks.start(id=task_id)).task_execution.id
77+
assert task_id is not None
78+
task_execution = (await client.environments.automations.tasks.start(id=task_id)).task_execution
79+
assert task_execution is not None
80+
task_execution_id = task_execution.id
81+
assert task_execution_id is not None
7382
log_url = await wait_for_task_log_url(client, environment_id, task_execution_id)
7483
return stream_logs(client, environment_id, log_url)
7584

7685
async def wait_for_task_log_url(client: AsyncGitpod, environment_id: str, task_execution_id: str) -> str:
77-
async def get_log_url():
86+
async def get_log_url() -> Optional[str]:
7887
execution = (await client.environments.automations.tasks.executions.retrieve(id=task_execution_id)).task_execution
88+
if not execution or not execution.status:
89+
return None
7990
return execution.status.log_url
8091

8192
return await wait_for_log_url(client, environment_id, task_execution_id, get_log_url, "RESOURCE_TYPE_TASK_EXECUTION")
8293

8394
async def wait_for_service_log_url(client: AsyncGitpod, environment_id: str, service_id: str) -> str:
84-
async def get_log_url():
95+
async def get_log_url() -> Optional[str]:
8596
service = (await client.environments.automations.services.retrieve(id=service_id)).service
97+
if not service or not service.status:
98+
return None
8699
if service.status.phase != "SERVICE_PHASE_RUNNING":
87100
return None
88101
return service.status.log_url
@@ -108,6 +121,8 @@ async def wait_for_log_url(client: AsyncGitpod, environment_id: str, resource_id
108121
finally:
109122
await event_stream.http_response.aclose()
110123

124+
raise Exception("Failed to get log URL")
125+
111126
async def stream_logs(client: AsyncGitpod, environment_id: str, log_url: str) -> AsyncIterator[str]:
112127
logs_access_token = (await client.environments.create_logs_token(environment_id=environment_id)).access_token
113128
async with httpx.AsyncClient() as http_client:

0 commit comments

Comments
 (0)