shell-session-manager 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- shell_session_manager/__init__.py +29 -0
- shell_session_manager/py.typed +0 -0
- shell_session_manager/session.py +683 -0
- shell_session_manager-1.0.0.dist-info/METADATA +62 -0
- shell_session_manager-1.0.0.dist-info/RECORD +8 -0
- shell_session_manager-1.0.0.dist-info/WHEEL +4 -0
- shell_session_manager-1.0.0.dist-info/entry_points.txt +4 -0
- shell_session_manager-1.0.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Public API for async shell/subprocess session management.
|
|
2
|
+
|
|
3
|
+
The package exports the session primitives from `shell_session_manager.session`.
|
|
4
|
+
Callers are responsible for constructing the command string; this package only
|
|
5
|
+
owns process lifetime, incremental stdin writes, stdout/stderr draining, and
|
|
6
|
+
session registry management.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from shell_session_manager.session import (
|
|
10
|
+
NextResult,
|
|
11
|
+
ShellSession,
|
|
12
|
+
ShellSessionInfo,
|
|
13
|
+
ShellSessionManager,
|
|
14
|
+
ShellSessionOptions,
|
|
15
|
+
StreamName,
|
|
16
|
+
command_slug_parts,
|
|
17
|
+
random_6digits,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"NextResult",
|
|
22
|
+
"ShellSession",
|
|
23
|
+
"ShellSessionInfo",
|
|
24
|
+
"ShellSessionManager",
|
|
25
|
+
"ShellSessionOptions",
|
|
26
|
+
"StreamName",
|
|
27
|
+
"command_slug_parts",
|
|
28
|
+
"random_6digits",
|
|
29
|
+
]
|
|
File without changes
|
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
"""Async helpers for streaming subprocess-backed shell sessions.
|
|
2
|
+
|
|
3
|
+
This module uses `anyio.open_process` and background tasks to drain
|
|
4
|
+
stdout/stderr into a memory stream. The tasks are regular `asyncio` tasks
|
|
5
|
+
instead of `anyio.TaskGroup` because `ShellSession` is designed to be held
|
|
6
|
+
across multiple tool calls, which may run in different tasks; `anyio.TaskGroup`
|
|
7
|
+
scopes must be entered/exited by the same task.
|
|
8
|
+
|
|
9
|
+
Command construction is deliberately outside this package. Callers should pass
|
|
10
|
+
the exact command to execute, including any shell, SSH, PTY, or environment
|
|
11
|
+
bootstrap wrappers they require.
|
|
12
|
+
|
|
13
|
+
Higher-level callers that need to manage multiple concurrent sessions should
|
|
14
|
+
use `ShellSessionManager`, which owns a mapping of session ids to `ShellSession`
|
|
15
|
+
instances and provides guardrails for cleanup.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import asyncio
|
|
21
|
+
import secrets
|
|
22
|
+
import subprocess
|
|
23
|
+
from contextlib import suppress
|
|
24
|
+
from dataclasses import dataclass, field
|
|
25
|
+
from typing import Literal, Self
|
|
26
|
+
|
|
27
|
+
import anyio
|
|
28
|
+
from anyio.abc import (
|
|
29
|
+
ByteReceiveStream,
|
|
30
|
+
ByteSendStream,
|
|
31
|
+
Process,
|
|
32
|
+
)
|
|
33
|
+
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
|
|
34
|
+
|
|
35
|
+
type NextResult = tuple[bytes, bytes, int | None]
|
|
36
|
+
type StreamName = Literal["stdout", "stderr"]
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def random_6digits() -> str:
|
|
40
|
+
"""Generate a short session id suitable for surfacing to end users.
|
|
41
|
+
|
|
42
|
+
Notes:
|
|
43
|
+
This id is intended to be opaque, easy to read/relay, and "unique enough"
|
|
44
|
+
for in-memory session registries. Callers that persist sessions should
|
|
45
|
+
use a stronger id.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
return f"{secrets.randbelow(1_000_000):06d}"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@dataclass(frozen=True, slots=True)
|
|
52
|
+
class ShellSessionOptions:
|
|
53
|
+
"""Timing/timeout knobs for `ShellSession` (all in seconds)."""
|
|
54
|
+
|
|
55
|
+
timeout_seconds: float = 15.0
|
|
56
|
+
idle_output_wait_seconds: float = 0.1
|
|
57
|
+
post_exit_flush_seconds: float = 0.5
|
|
58
|
+
post_exit_drain_wait_seconds: float = 0.01
|
|
59
|
+
terminate_wait_seconds: float = 2.0
|
|
60
|
+
kill_wait_seconds: float = 2.0
|
|
61
|
+
process_close_wait_seconds: float = 0.5
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True)
|
|
65
|
+
class _StreamDone:
|
|
66
|
+
stream: StreamName
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
type OutputEvent = tuple[StreamName, bytes] | _StreamDone
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass(frozen=True, slots=True)
|
|
73
|
+
class ShellSessionInfo:
|
|
74
|
+
"""Public metadata for an active `ShellSession` registered in a manager.
|
|
75
|
+
|
|
76
|
+
Notes:
|
|
77
|
+
`desc` is an optional short, human-readable description supplied by the
|
|
78
|
+
higher-level caller. It is intentionally not the full raw command, which
|
|
79
|
+
may be long or sensitive.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
session_id: str
|
|
83
|
+
desc: str | None = None
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@dataclass(slots=True)
|
|
87
|
+
class ShellSessionManager:
|
|
88
|
+
"""Manage multiple `ShellSession` instances by `session_id`.
|
|
89
|
+
|
|
90
|
+
This is intended for "tool loop" style runtimes where:
|
|
91
|
+
- sessions must be resumable across calls by a short opaque id
|
|
92
|
+
- callers need a single place to prune/interrupt/close sessions
|
|
93
|
+
|
|
94
|
+
Concurrency:
|
|
95
|
+
This manager serializes all operations with a single lock. It prioritizes
|
|
96
|
+
correctness and simple invariants over parallelism.
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
max_sessions: int = 32
|
|
100
|
+
|
|
101
|
+
_sessions: dict[str, ShellSession] = field(default_factory=dict, init=False)
|
|
102
|
+
_lock: anyio.Lock = field(default_factory=anyio.Lock, init=False, repr=False)
|
|
103
|
+
_closed: bool = field(default=False, init=False, repr=False)
|
|
104
|
+
|
|
105
|
+
async def __aenter__(self) -> Self:
|
|
106
|
+
return self
|
|
107
|
+
|
|
108
|
+
async def __aexit__(self, exc_type: object, exc: object, tb: object) -> None:
|
|
109
|
+
await self.close()
|
|
110
|
+
|
|
111
|
+
def _require_open(self) -> None:
|
|
112
|
+
if self._closed:
|
|
113
|
+
raise RuntimeError("ShellSessionManager is closed")
|
|
114
|
+
|
|
115
|
+
async def new_shell(
|
|
116
|
+
self,
|
|
117
|
+
command: str,
|
|
118
|
+
*,
|
|
119
|
+
options: ShellSessionOptions | None = None,
|
|
120
|
+
desc: str | None = None,
|
|
121
|
+
) -> str:
|
|
122
|
+
"""Create, start, and register a new `ShellSession`.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
The new session's `session_id`.
|
|
126
|
+
"""
|
|
127
|
+
|
|
128
|
+
async with self._lock:
|
|
129
|
+
self._require_open()
|
|
130
|
+
await self._prune_exited_sessions_locked()
|
|
131
|
+
if len(self._sessions) >= self.max_sessions:
|
|
132
|
+
raise RuntimeError(
|
|
133
|
+
f"Too many active sessions ({len(self._sessions)}/{self.max_sessions})"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
session = ShellSession(
|
|
137
|
+
command, options=options or ShellSessionOptions(), desc=desc
|
|
138
|
+
)
|
|
139
|
+
# Avoid id collisions within this process.
|
|
140
|
+
for _ in range(20):
|
|
141
|
+
if session.session_id not in self._sessions:
|
|
142
|
+
break
|
|
143
|
+
session.session_id = random_6digits()
|
|
144
|
+
else:
|
|
145
|
+
raise RuntimeError("Failed to allocate a unique session id")
|
|
146
|
+
|
|
147
|
+
try:
|
|
148
|
+
await session.ensure_started()
|
|
149
|
+
except BaseException:
|
|
150
|
+
await session.interrupt()
|
|
151
|
+
raise
|
|
152
|
+
|
|
153
|
+
self._sessions[session.session_id] = session
|
|
154
|
+
return session.session_id
|
|
155
|
+
|
|
156
|
+
async def next(
|
|
157
|
+
self,
|
|
158
|
+
session_id: str,
|
|
159
|
+
stdin: bytes | None = None,
|
|
160
|
+
timeout_seconds: float | None = None,
|
|
161
|
+
) -> NextResult:
|
|
162
|
+
"""Run the next step for a registered session.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
session_id: Registered shell session id.
|
|
166
|
+
stdin: Optional stdin bytes to write before waiting.
|
|
167
|
+
timeout_seconds:
|
|
168
|
+
Optional per-call timeout override (seconds) for this `next()`
|
|
169
|
+
call only. If omitted, the session default is used.
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
async with self._lock:
|
|
173
|
+
self._require_open()
|
|
174
|
+
session = self._sessions.get(session_id)
|
|
175
|
+
if session is None:
|
|
176
|
+
raise KeyError(f"Unknown session id: {session_id}")
|
|
177
|
+
|
|
178
|
+
result = await session.next(stdin=stdin, timeout_seconds=timeout_seconds)
|
|
179
|
+
if session.is_closed():
|
|
180
|
+
self._sessions.pop(session_id, None)
|
|
181
|
+
return result
|
|
182
|
+
|
|
183
|
+
async def interrupt(self, session_id: str) -> None:
|
|
184
|
+
"""Interrupt and unregister a session (idempotent if already closed)."""
|
|
185
|
+
|
|
186
|
+
async with self._lock:
|
|
187
|
+
self._require_open()
|
|
188
|
+
session = self._sessions.pop(session_id, None)
|
|
189
|
+
if session is None:
|
|
190
|
+
raise KeyError(f"Unknown session id: {session_id}")
|
|
191
|
+
await session.interrupt()
|
|
192
|
+
|
|
193
|
+
async def clear(self) -> None:
|
|
194
|
+
"""Interrupt all managed sessions and clear the registry."""
|
|
195
|
+
|
|
196
|
+
async with self._lock:
|
|
197
|
+
self._require_open()
|
|
198
|
+
sessions = list(self._sessions.values())
|
|
199
|
+
self._sessions.clear()
|
|
200
|
+
|
|
201
|
+
for session in sessions:
|
|
202
|
+
await session.interrupt()
|
|
203
|
+
|
|
204
|
+
async def close(self) -> None:
|
|
205
|
+
"""Close the manager and interrupt all managed sessions."""
|
|
206
|
+
|
|
207
|
+
async with self._lock:
|
|
208
|
+
if self._closed:
|
|
209
|
+
return
|
|
210
|
+
self._closed = True
|
|
211
|
+
sessions = list(self._sessions.values())
|
|
212
|
+
self._sessions.clear()
|
|
213
|
+
|
|
214
|
+
for session in sessions:
|
|
215
|
+
await session.interrupt()
|
|
216
|
+
|
|
217
|
+
async def list_sessions(self) -> list[ShellSessionInfo]:
|
|
218
|
+
"""Prune exited sessions and list remaining active sessions."""
|
|
219
|
+
|
|
220
|
+
async with self._lock:
|
|
221
|
+
self._require_open()
|
|
222
|
+
await self._prune_exited_sessions_locked()
|
|
223
|
+
return [
|
|
224
|
+
ShellSessionInfo(
|
|
225
|
+
session_id=s.session_id,
|
|
226
|
+
desc=s.desc,
|
|
227
|
+
)
|
|
228
|
+
for s in self._sessions.values()
|
|
229
|
+
if not s.is_closed()
|
|
230
|
+
]
|
|
231
|
+
|
|
232
|
+
async def _prune_exited_sessions_locked(self) -> None:
|
|
233
|
+
"""Best-effort prune of sessions whose subprocess has already exited.
|
|
234
|
+
|
|
235
|
+
This is mostly a guard for callers that start a session but never call
|
|
236
|
+
`next()`; `ShellSession.next()` normally detects exit and auto-closes.
|
|
237
|
+
We use a tiny bounded wait when polling process status because a strict
|
|
238
|
+
zero-timeout cancel scope can prevent the backend from refreshing
|
|
239
|
+
`process.returncode` in time.
|
|
240
|
+
"""
|
|
241
|
+
|
|
242
|
+
to_remove: list[str] = []
|
|
243
|
+
for session_id, session in self._sessions.items():
|
|
244
|
+
if session.is_closed():
|
|
245
|
+
to_remove.append(session_id)
|
|
246
|
+
continue
|
|
247
|
+
|
|
248
|
+
process = session._process
|
|
249
|
+
if process is None:
|
|
250
|
+
continue
|
|
251
|
+
|
|
252
|
+
if process.returncode is None:
|
|
253
|
+
# Keep this short to avoid blocking list/prune for active sessions.
|
|
254
|
+
with anyio.move_on_after(0.01):
|
|
255
|
+
await process.wait()
|
|
256
|
+
|
|
257
|
+
if process.returncode is not None:
|
|
258
|
+
await session.interrupt()
|
|
259
|
+
to_remove.append(session_id)
|
|
260
|
+
|
|
261
|
+
for session_id in to_remove:
|
|
262
|
+
self._sessions.pop(session_id, None)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@dataclass(slots=True)
|
|
266
|
+
class ShellSession:
|
|
267
|
+
"""A minimal async session for incremental stdin and drained stdout/stderr.
|
|
268
|
+
|
|
269
|
+
API:
|
|
270
|
+
- `__init__(command: str, ...)`
|
|
271
|
+
- `await next(stdin: bytes | None) -> (stdout, stderr, returncode|None)`
|
|
272
|
+
- `await interrupt()`
|
|
273
|
+
|
|
274
|
+
`returncode=None` means the subprocess is still running.
|
|
275
|
+
|
|
276
|
+
When `next()` observes that the subprocess has exited, it drains any remaining
|
|
277
|
+
stdout/stderr (best-effort) and then automatically calls `interrupt()` to
|
|
278
|
+
release OS resources and mark the session as closed.
|
|
279
|
+
"""
|
|
280
|
+
|
|
281
|
+
command: str
|
|
282
|
+
|
|
283
|
+
options: ShellSessionOptions = field(default_factory=ShellSessionOptions)
|
|
284
|
+
|
|
285
|
+
_process: Process | None = field(init=False, default=None, repr=False)
|
|
286
|
+
_stdin: ByteSendStream | None = field(init=False, default=None, repr=False)
|
|
287
|
+
_stdout: ByteReceiveStream | None = field(init=False, default=None, repr=False)
|
|
288
|
+
_stderr: ByteReceiveStream | None = field(init=False, default=None, repr=False)
|
|
289
|
+
_stdout_task: asyncio.Task[None] | None = field(
|
|
290
|
+
init=False, default=None, repr=False
|
|
291
|
+
)
|
|
292
|
+
_stderr_task: asyncio.Task[None] | None = field(
|
|
293
|
+
init=False, default=None, repr=False
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
_out_send: MemoryObjectSendStream[OutputEvent] = field(init=False, repr=False)
|
|
297
|
+
_out_recv: MemoryObjectReceiveStream[OutputEvent] = field(init=False, repr=False)
|
|
298
|
+
|
|
299
|
+
_closed: bool = field(init=False, default=False, repr=False)
|
|
300
|
+
_stdout_done: bool = field(init=False, default=False, repr=False)
|
|
301
|
+
_stderr_done: bool = field(init=False, default=False, repr=False)
|
|
302
|
+
|
|
303
|
+
session_id: str = field(init=False, default_factory=random_6digits)
|
|
304
|
+
desc: str | None = None
|
|
305
|
+
|
|
306
|
+
def __post_init__(self) -> None:
|
|
307
|
+
self._out_send, self._out_recv = anyio.create_memory_object_stream[OutputEvent](
|
|
308
|
+
1000
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
async def __aenter__(self) -> Self:
|
|
312
|
+
try:
|
|
313
|
+
await self.ensure_started()
|
|
314
|
+
return self
|
|
315
|
+
except BaseException:
|
|
316
|
+
# Best-effort cleanup on enter failures (e.g., spawn errors).
|
|
317
|
+
await self.interrupt()
|
|
318
|
+
raise
|
|
319
|
+
|
|
320
|
+
async def __aexit__(self, exc_type: object, exc: object, tb: object) -> None:
|
|
321
|
+
await self.interrupt()
|
|
322
|
+
|
|
323
|
+
async def ensure_started(self) -> None:
|
|
324
|
+
if self._process is not None:
|
|
325
|
+
return
|
|
326
|
+
try:
|
|
327
|
+
thread_name = (
|
|
328
|
+
f"{'-'.join(command_slug_parts(self.command))}-{self.session_id}"
|
|
329
|
+
)
|
|
330
|
+
process = await anyio.open_process(
|
|
331
|
+
self.command,
|
|
332
|
+
stdin=subprocess.PIPE,
|
|
333
|
+
stdout=subprocess.PIPE,
|
|
334
|
+
stderr=subprocess.PIPE,
|
|
335
|
+
start_new_session=False,
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
# Publish early so interrupt() can clean up even if we fail mid-start.
|
|
339
|
+
self._process = process
|
|
340
|
+
|
|
341
|
+
if (
|
|
342
|
+
process.stdin is None
|
|
343
|
+
or process.stdout is None
|
|
344
|
+
or process.stderr is None
|
|
345
|
+
):
|
|
346
|
+
raise RuntimeError("Process started without stdin/stdout/stderr pipes")
|
|
347
|
+
|
|
348
|
+
# Publish state only after all required resources exist.
|
|
349
|
+
self._stdin = process.stdin
|
|
350
|
+
self._stdout = process.stdout
|
|
351
|
+
self._stderr = process.stderr
|
|
352
|
+
|
|
353
|
+
# Use asyncio Tasks instead of anyio.TaskGroup: this session is held across
|
|
354
|
+
# multiple tool calls which may run in different tasks. anyio.TaskGroup
|
|
355
|
+
# requires __aenter__/__aexit__ in the same task, which is easy to violate.
|
|
356
|
+
self._stdout_task = asyncio.create_task(
|
|
357
|
+
self._pump("stdout"),
|
|
358
|
+
name=f"ShellSession({thread_name}):stdout",
|
|
359
|
+
)
|
|
360
|
+
self._stderr_task = asyncio.create_task(
|
|
361
|
+
self._pump("stderr"),
|
|
362
|
+
name=f"ShellSession({thread_name}):stderr",
|
|
363
|
+
)
|
|
364
|
+
except BaseException:
|
|
365
|
+
await self.interrupt()
|
|
366
|
+
raise
|
|
367
|
+
|
|
368
|
+
async def _pump(
|
|
369
|
+
self,
|
|
370
|
+
stream_name: StreamName,
|
|
371
|
+
) -> None:
|
|
372
|
+
stream: ByteReceiveStream | None
|
|
373
|
+
if stream_name == "stdout":
|
|
374
|
+
stream = self._stdout
|
|
375
|
+
else:
|
|
376
|
+
stream = self._stderr
|
|
377
|
+
if stream is None:
|
|
378
|
+
raise RuntimeError(f"Process {stream_name} is not available")
|
|
379
|
+
|
|
380
|
+
# Bind to a local var so interrupt() clearing instance fields doesn't affect the reader.
|
|
381
|
+
bound_stream = stream
|
|
382
|
+
try:
|
|
383
|
+
while True:
|
|
384
|
+
try:
|
|
385
|
+
chunk = await bound_stream.receive(65536)
|
|
386
|
+
except anyio.EndOfStream:
|
|
387
|
+
break
|
|
388
|
+
if chunk:
|
|
389
|
+
try:
|
|
390
|
+
await self._out_send.send((stream_name, chunk))
|
|
391
|
+
except BaseException:
|
|
392
|
+
break
|
|
393
|
+
finally:
|
|
394
|
+
# Best-effort signal that this stream is done.
|
|
395
|
+
with suppress(BaseException):
|
|
396
|
+
await self._out_send.send(_StreamDone(stream=stream_name))
|
|
397
|
+
|
|
398
|
+
async def next(
|
|
399
|
+
self, stdin: bytes | None = None, timeout_seconds: float | None = None
|
|
400
|
+
) -> NextResult:
|
|
401
|
+
"""Send stdin and wait for output or exit.
|
|
402
|
+
|
|
403
|
+
Args:
|
|
404
|
+
stdin: Optional stdin bytes to send first.
|
|
405
|
+
timeout_seconds:
|
|
406
|
+
Optional per-call timeout override in seconds. If omitted, uses
|
|
407
|
+
`self.options.timeout_seconds`.
|
|
408
|
+
|
|
409
|
+
Returns:
|
|
410
|
+
(stdout, stderr, returncode). `returncode=None` means the process did
|
|
411
|
+
not exit before the timeout.
|
|
412
|
+
|
|
413
|
+
Notes:
|
|
414
|
+
If the process exits (`returncode is not None`), this method performs
|
|
415
|
+
best-effort post-exit output draining and then automatically closes
|
|
416
|
+
the session by calling `interrupt()`. Callers should treat a non-None
|
|
417
|
+
return code as terminal: no further `next()` calls are supported.
|
|
418
|
+
"""
|
|
419
|
+
|
|
420
|
+
if self._closed:
|
|
421
|
+
raise RuntimeError("Session is closed")
|
|
422
|
+
await self.ensure_started()
|
|
423
|
+
process = self._process
|
|
424
|
+
if process is None:
|
|
425
|
+
raise RuntimeError("Session not started")
|
|
426
|
+
|
|
427
|
+
if stdin is not None:
|
|
428
|
+
if self._stdin is None:
|
|
429
|
+
raise RuntimeError("Process stdin is not available")
|
|
430
|
+
await self._stdin.send(stdin)
|
|
431
|
+
|
|
432
|
+
stdout = bytearray()
|
|
433
|
+
stderr = bytearray()
|
|
434
|
+
returncode: int | None = None
|
|
435
|
+
|
|
436
|
+
effective_timeout = (
|
|
437
|
+
timeout_seconds
|
|
438
|
+
if timeout_seconds is not None
|
|
439
|
+
else self.options.timeout_seconds
|
|
440
|
+
)
|
|
441
|
+
if effective_timeout <= 0:
|
|
442
|
+
raise ValueError(f"timeout_seconds must be > 0; got {effective_timeout}")
|
|
443
|
+
|
|
444
|
+
deadline = anyio.current_time() + effective_timeout
|
|
445
|
+
while True:
|
|
446
|
+
if process.returncode is not None:
|
|
447
|
+
returncode = process.returncode
|
|
448
|
+
break
|
|
449
|
+
|
|
450
|
+
remaining = deadline - anyio.current_time()
|
|
451
|
+
if remaining <= 0:
|
|
452
|
+
returncode = None
|
|
453
|
+
break
|
|
454
|
+
|
|
455
|
+
# Drain what we already have; if there was nothing buffered, wait briefly
|
|
456
|
+
# for one event so callers don't need to spin.
|
|
457
|
+
await self._drain_output(stdout, stderr, timeout=0)
|
|
458
|
+
if not stdout and not stderr:
|
|
459
|
+
await self._drain_output(
|
|
460
|
+
stdout,
|
|
461
|
+
stderr,
|
|
462
|
+
timeout=min(self.options.idle_output_wait_seconds, remaining),
|
|
463
|
+
)
|
|
464
|
+
|
|
465
|
+
with anyio.move_on_after(0):
|
|
466
|
+
await process.wait()
|
|
467
|
+
|
|
468
|
+
# Final drain after exit/timeout. If the process exited, give pumps a brief
|
|
469
|
+
# chance to flush remaining output and send stream-done markers.
|
|
470
|
+
if returncode is not None:
|
|
471
|
+
flush_deadline = anyio.current_time() + self.options.post_exit_flush_seconds
|
|
472
|
+
while anyio.current_time() < flush_deadline and not (
|
|
473
|
+
self._stdout_done and self._stderr_done
|
|
474
|
+
):
|
|
475
|
+
await self._drain_output(
|
|
476
|
+
stdout,
|
|
477
|
+
stderr,
|
|
478
|
+
timeout=self.options.post_exit_drain_wait_seconds,
|
|
479
|
+
)
|
|
480
|
+
await self._drain_output(stdout, stderr, timeout=0)
|
|
481
|
+
|
|
482
|
+
result = (bytes(stdout), bytes(stderr), returncode)
|
|
483
|
+
|
|
484
|
+
# If we have a final return code, this session is terminal. Close eagerly
|
|
485
|
+
# so higher-level callers (e.g. tool loops) don't need to remember to call
|
|
486
|
+
# interrupt() to release resources.
|
|
487
|
+
if returncode is not None:
|
|
488
|
+
await self.interrupt()
|
|
489
|
+
|
|
490
|
+
return result
|
|
491
|
+
|
|
492
|
+
async def _drain_output(
|
|
493
|
+
self, stdout: bytearray, stderr: bytearray, *, timeout: float
|
|
494
|
+
) -> None:
|
|
495
|
+
"""Drain buffered output, then optionally wait up to `timeout` for one event.
|
|
496
|
+
|
|
497
|
+
The `timeout` applies only to the single blocking `receive()`; all other
|
|
498
|
+
draining is non-blocking.
|
|
499
|
+
"""
|
|
500
|
+
|
|
501
|
+
# Drain whatever is already buffered (non-blocking).
|
|
502
|
+
while True:
|
|
503
|
+
try:
|
|
504
|
+
event = self._out_recv.receive_nowait()
|
|
505
|
+
except (anyio.WouldBlock, anyio.EndOfStream):
|
|
506
|
+
break
|
|
507
|
+
self._apply_event(event, stdout, stderr)
|
|
508
|
+
|
|
509
|
+
if timeout <= 0:
|
|
510
|
+
return
|
|
511
|
+
|
|
512
|
+
# Wait up to `timeout` for a single output event, then drain any burst.
|
|
513
|
+
cancel_exc = anyio.get_cancelled_exc_class()
|
|
514
|
+
event = None
|
|
515
|
+
with anyio.move_on_after(timeout):
|
|
516
|
+
try:
|
|
517
|
+
event = await self._out_recv.receive()
|
|
518
|
+
except anyio.EndOfStream:
|
|
519
|
+
return
|
|
520
|
+
except cancel_exc:
|
|
521
|
+
event = None
|
|
522
|
+
if event is not None:
|
|
523
|
+
self._apply_event(event, stdout, stderr)
|
|
524
|
+
|
|
525
|
+
while True:
|
|
526
|
+
try:
|
|
527
|
+
event = self._out_recv.receive_nowait()
|
|
528
|
+
except (anyio.WouldBlock, anyio.EndOfStream):
|
|
529
|
+
break
|
|
530
|
+
self._apply_event(event, stdout, stderr)
|
|
531
|
+
|
|
532
|
+
def _apply_event(
|
|
533
|
+
self, event: OutputEvent, stdout: bytearray, stderr: bytearray
|
|
534
|
+
) -> None:
|
|
535
|
+
if isinstance(event, _StreamDone):
|
|
536
|
+
if event.stream == "stdout":
|
|
537
|
+
self._stdout_done = True
|
|
538
|
+
else:
|
|
539
|
+
self._stderr_done = True
|
|
540
|
+
return
|
|
541
|
+
|
|
542
|
+
stream_name, chunk = event
|
|
543
|
+
if stream_name == "stdout":
|
|
544
|
+
stdout += chunk
|
|
545
|
+
else:
|
|
546
|
+
stderr += chunk
|
|
547
|
+
|
|
548
|
+
async def interrupt(self) -> None:
|
|
549
|
+
"""Best-effort cleanup hook (never raises).
|
|
550
|
+
|
|
551
|
+
This is intentionally tolerant of partial initialization and cleanup
|
|
552
|
+
failures; callers should not need to wrap it in try/except.
|
|
553
|
+
"""
|
|
554
|
+
|
|
555
|
+
if self._closed:
|
|
556
|
+
return
|
|
557
|
+
self._closed = True
|
|
558
|
+
|
|
559
|
+
try:
|
|
560
|
+
with anyio.CancelScope(shield=True):
|
|
561
|
+
process = self._process
|
|
562
|
+
stdout_task = self._stdout_task
|
|
563
|
+
stderr_task = self._stderr_task
|
|
564
|
+
|
|
565
|
+
# Terminate/kill first to encourage pumps to finish naturally.
|
|
566
|
+
if process is not None and process.returncode is None:
|
|
567
|
+
with suppress(BaseException):
|
|
568
|
+
process.terminate()
|
|
569
|
+
with anyio.move_on_after(self.options.terminate_wait_seconds):
|
|
570
|
+
with suppress(BaseException):
|
|
571
|
+
await process.wait()
|
|
572
|
+
if process.returncode is None:
|
|
573
|
+
with suppress(BaseException):
|
|
574
|
+
process.kill()
|
|
575
|
+
with anyio.move_on_after(self.options.kill_wait_seconds):
|
|
576
|
+
with suppress(BaseException):
|
|
577
|
+
await process.wait()
|
|
578
|
+
|
|
579
|
+
# Cancel reader tasks before closing the output stream to avoid
|
|
580
|
+
# concurrent sends into a closed channel.
|
|
581
|
+
for task in (stdout_task, stderr_task):
|
|
582
|
+
if task is None:
|
|
583
|
+
continue
|
|
584
|
+
with suppress(BaseException):
|
|
585
|
+
task.cancel()
|
|
586
|
+
for task in (stdout_task, stderr_task):
|
|
587
|
+
if task is None:
|
|
588
|
+
continue
|
|
589
|
+
with suppress(BaseException):
|
|
590
|
+
await task
|
|
591
|
+
|
|
592
|
+
with suppress(BaseException):
|
|
593
|
+
await self._out_send.aclose()
|
|
594
|
+
|
|
595
|
+
if process is not None:
|
|
596
|
+
with anyio.move_on_after(self.options.process_close_wait_seconds):
|
|
597
|
+
with suppress(BaseException):
|
|
598
|
+
await process.aclose()
|
|
599
|
+
except BaseException:
|
|
600
|
+
# Cleanup must never leak exceptions (including cancellation) out to callers.
|
|
601
|
+
pass
|
|
602
|
+
finally:
|
|
603
|
+
self._process = None
|
|
604
|
+
self._stdin = None
|
|
605
|
+
self._stdout = None
|
|
606
|
+
self._stderr = None
|
|
607
|
+
self._stdout_task = None
|
|
608
|
+
self._stderr_task = None
|
|
609
|
+
|
|
610
|
+
def is_closed(self) -> bool:
|
|
611
|
+
"""Check if the session has been closed."""
|
|
612
|
+
return self._closed
|
|
613
|
+
|
|
614
|
+
def __del__(self) -> None:
|
|
615
|
+
# Best-effort sync cleanup path for abnormal object destruction.
|
|
616
|
+
try:
|
|
617
|
+
process = getattr(self, "_process", None)
|
|
618
|
+
if process is not None and getattr(process, "returncode", None) is None:
|
|
619
|
+
process.terminate()
|
|
620
|
+
except Exception:
|
|
621
|
+
pass
|
|
622
|
+
|
|
623
|
+
|
|
624
|
+
def command_slug_parts(
|
|
625
|
+
command: str,
|
|
626
|
+
*,
|
|
627
|
+
head_components: int = 3,
|
|
628
|
+
tail_components: int = 2,
|
|
629
|
+
component_limit: int = 24,
|
|
630
|
+
label_limit: int = 80,
|
|
631
|
+
) -> list[str]:
|
|
632
|
+
"""Generate a short, human-readable slug preview for a shell command.
|
|
633
|
+
|
|
634
|
+
Returns:
|
|
635
|
+
A list of slug components suitable for joining with `-` into a task name.
|
|
636
|
+
|
|
637
|
+
Notes:
|
|
638
|
+
This intentionally uses only a preview of the command (first few + last few
|
|
639
|
+
whitespace-delimited components) so we don't embed full command lines (which
|
|
640
|
+
can be long and/or sensitive) into asyncio task names that may surface in
|
|
641
|
+
logs and debuggers.
|
|
642
|
+
"""
|
|
643
|
+
|
|
644
|
+
normalized = " ".join(command.strip().split())
|
|
645
|
+
if not normalized:
|
|
646
|
+
return ["cmd"]
|
|
647
|
+
|
|
648
|
+
components = normalized.split(" ")
|
|
649
|
+
|
|
650
|
+
def clean(component: str) -> str:
|
|
651
|
+
component = component.strip()
|
|
652
|
+
if (
|
|
653
|
+
len(component) >= 2
|
|
654
|
+
and component[0] == component[-1]
|
|
655
|
+
and component[0] in {"'", '"'}
|
|
656
|
+
):
|
|
657
|
+
component = component[1:-1].strip()
|
|
658
|
+
component = component.rsplit("/", 1)[-1]
|
|
659
|
+
if len(component) > component_limit:
|
|
660
|
+
component = component[: max(0, component_limit - 1)] + "…"
|
|
661
|
+
return component or "…"
|
|
662
|
+
|
|
663
|
+
head = [clean(c) for c in components[:head_components]]
|
|
664
|
+
tail_start = max(head_components, len(components) - tail_components)
|
|
665
|
+
tail = [clean(c) for c in components[tail_start:]]
|
|
666
|
+
|
|
667
|
+
preview: list[str] = [*head]
|
|
668
|
+
if tail_start > head_components and tail:
|
|
669
|
+
preview.append("…")
|
|
670
|
+
preview.extend(tail)
|
|
671
|
+
|
|
672
|
+
# Ensure the final joined label stays reasonably short.
|
|
673
|
+
label = "-".join(preview).strip("-")
|
|
674
|
+
if len(label) > label_limit and preview:
|
|
675
|
+
prefix = "-".join(preview[:-1]).strip("-")
|
|
676
|
+
remaining = label_limit - (len(prefix) + (1 if prefix else 0))
|
|
677
|
+
if remaining <= 0:
|
|
678
|
+
return ["cmd"]
|
|
679
|
+
last = preview[-1]
|
|
680
|
+
if len(last) > remaining:
|
|
681
|
+
preview[-1] = last[: max(0, remaining - 1)] + "…"
|
|
682
|
+
|
|
683
|
+
return [p for p in preview if p] or ["cmd"]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: shell-session-manager
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Async subprocess session manager with incremental stdin/stdout/stderr support
|
|
5
|
+
Author-Email: =?utf-8?b?WWFubGkg55uQ57KS?= <yanli@mail.one>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: ==3.14.*
|
|
9
|
+
Requires-Dist: anyio>=4.12.1
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# shell-session-manager
|
|
13
|
+
|
|
14
|
+
`shell-session-manager` provides async subprocess sessions that can be resumed
|
|
15
|
+
across multiple calls. It drains stdout/stderr in background asyncio tasks,
|
|
16
|
+
supports incremental stdin writes, exposes short session ids, and manages
|
|
17
|
+
best-effort cleanup for long-lived command sessions.
|
|
18
|
+
|
|
19
|
+
Command construction is intentionally out of scope: callers should pass the
|
|
20
|
+
exact command string they want to execute, including any shell, SSH, PTY, or
|
|
21
|
+
environment bootstrap wrappers.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import anyio
|
|
27
|
+
|
|
28
|
+
from shell_session_manager import ShellSession, ShellSessionOptions
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
async def main() -> None:
|
|
32
|
+
async with ShellSession(
|
|
33
|
+
"python -u -c 'import sys; print(\"ready\"); line = sys.stdin.readline(); print(line.upper(), end=\"\")'",
|
|
34
|
+
options=ShellSessionOptions(timeout_seconds=1),
|
|
35
|
+
) as session:
|
|
36
|
+
stdout, stderr, code = await session.next()
|
|
37
|
+
print(stdout.decode(), stderr.decode(), code) # ready\n, "", None
|
|
38
|
+
|
|
39
|
+
stdout, stderr, code = await session.next(b"hello\n")
|
|
40
|
+
print(stdout.decode(), stderr.decode(), code) # HELLO\n, "", 0
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
anyio.run(main)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For multiple long-lived sessions, use `ShellSessionManager`:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
import anyio
|
|
50
|
+
|
|
51
|
+
from shell_session_manager import ShellSessionManager
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async def main() -> None:
|
|
55
|
+
async with ShellSessionManager() as manager:
|
|
56
|
+
session_id = await manager.new_shell("python -c 'print(42)'")
|
|
57
|
+
stdout, stderr, returncode = await manager.next(session_id)
|
|
58
|
+
print(stdout.decode(), stderr.decode(), returncode)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
anyio.run(main)
|
|
62
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
shell_session_manager-1.0.0.dist-info/METADATA,sha256=yIoUCLDcHHXzK70kyyZQJp30JHBpWNo-FG6KpNCyl9k,1925
|
|
2
|
+
shell_session_manager-1.0.0.dist-info/WHEEL,sha256=Z36eTX6lG3PITRleSd5hAZHCcz52yg3c0JQVxKBbLW0,90
|
|
3
|
+
shell_session_manager-1.0.0.dist-info/entry_points.txt,sha256=6OYgBcLyFCUgeqLgnvMyOJxPCWzgy7se4rLPKtNonMs,34
|
|
4
|
+
shell_session_manager-1.0.0.dist-info/licenses/LICENSE,sha256=argP9wYgQ1LWBagmxujAcNYuqlLgFJTGoF3ehfkczt8,10259
|
|
5
|
+
shell_session_manager/__init__.py,sha256=wJ-5WLRmGkLI-VcnbwGwDdOjQuXU6hEn0XP8gFU5BGc,742
|
|
6
|
+
shell_session_manager/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
shell_session_manager/session.py,sha256=iDgw4SjLs-cj91AizdD2QWmMQg6GXcH-amBobITfpBw,24223
|
|
8
|
+
shell_session_manager-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|