agentdev-lib 1.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.
- agentdev_lib/__init__.py +1 -0
- agentdev_lib/git_default_branch.py +83 -0
- agentdev_lib/result_codes.py +123 -0
- agentdev_lib/skill_common.py +107 -0
- agentdev_lib-1.0.dist-info/METADATA +22 -0
- agentdev_lib-1.0.dist-info/RECORD +9 -0
- agentdev_lib-1.0.dist-info/WHEEL +5 -0
- agentdev_lib-1.0.dist-info/licenses/LICENSE +21 -0
- agentdev_lib-1.0.dist-info/top_level.txt +1 -0
agentdev_lib/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Code shared by the scripts of the agentdev plugins."""
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Recognize a repository's default branch, for agentdev skill scripts.
|
|
3
|
+
|
|
4
|
+
Importable, and runnable as ``python -m agentdev_lib.git_default_branch``:
|
|
5
|
+
|
|
6
|
+
is-default <branch> exit 0 when <branch> is main or master
|
|
7
|
+
remote <remote> print the remote's default branch
|
|
8
|
+
|
|
9
|
+
Standard library only.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import shutil
|
|
15
|
+
import subprocess
|
|
16
|
+
import sys
|
|
17
|
+
|
|
18
|
+
USAGE = """\
|
|
19
|
+
Usage:
|
|
20
|
+
python -m agentdev_lib.git_default_branch is-default <branch>
|
|
21
|
+
python -m agentdev_lib.git_default_branch remote <remote>
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _run(*arguments: str) -> subprocess.CompletedProcess[str]:
|
|
26
|
+
return subprocess.run(list(arguments), check=False, capture_output=True, text=True)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def is_default_branch(branch: str) -> bool:
|
|
30
|
+
"""Return whether `branch` carries a conventional default-branch name."""
|
|
31
|
+
return branch in ('main', 'master')
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def remote_default_branch(remote: str) -> str | None:
|
|
35
|
+
"""
|
|
36
|
+
Return the branch `remote` calls its default, or None when nothing knows.
|
|
37
|
+
|
|
38
|
+
``refs/remotes/<remote>/HEAD`` answers first, then GitHub's answer for the
|
|
39
|
+
remote URL.
|
|
40
|
+
"""
|
|
41
|
+
symref = _run('git', 'symbolic-ref', '--quiet', '--short', f'refs/remotes/{remote}/HEAD')
|
|
42
|
+
if symref.returncode == 0:
|
|
43
|
+
return symref.stdout.strip().removeprefix(f'{remote}/')
|
|
44
|
+
if shutil.which('gh') is None:
|
|
45
|
+
return None
|
|
46
|
+
url = _run('git', 'remote', 'get-url', remote)
|
|
47
|
+
if url.returncode != 0:
|
|
48
|
+
return None
|
|
49
|
+
view = _run(
|
|
50
|
+
'gh',
|
|
51
|
+
'repo',
|
|
52
|
+
'view',
|
|
53
|
+
url.stdout.strip(),
|
|
54
|
+
'--json',
|
|
55
|
+
'defaultBranchRef',
|
|
56
|
+
'--jq',
|
|
57
|
+
'.defaultBranchRef.name',
|
|
58
|
+
)
|
|
59
|
+
name = view.stdout.strip()
|
|
60
|
+
if view.returncode != 0 or not name:
|
|
61
|
+
return None
|
|
62
|
+
return name
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def main(arguments: list[str]) -> int:
|
|
66
|
+
"""Run one command-line request; exit 1 for a no, 2 for bad usage."""
|
|
67
|
+
if len(arguments) == 2 and arguments[0] == 'is-default':
|
|
68
|
+
return 0 if is_default_branch(arguments[1]) else 1
|
|
69
|
+
if len(arguments) == 2 and arguments[0] == 'remote':
|
|
70
|
+
name = remote_default_branch(arguments[1])
|
|
71
|
+
if name is None:
|
|
72
|
+
return 1
|
|
73
|
+
print(name)
|
|
74
|
+
return 0
|
|
75
|
+
if arguments in (['-h'], ['--help']):
|
|
76
|
+
print(USAGE, end='')
|
|
77
|
+
return 0
|
|
78
|
+
print(USAGE, end='', file=sys.stderr)
|
|
79
|
+
return 2
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
if __name__ == '__main__':
|
|
83
|
+
sys.exit(main(sys.argv[1:]))
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Shared result-code helpers for agentdev skill scripts.
|
|
3
|
+
|
|
4
|
+
One code-to-name table, one ``RESULT=<NAME>`` line as the last thing on stdout
|
|
5
|
+
for every exit path, and terminating signals named and then re-raised. Behind
|
|
6
|
+
a ``uv run`` shebang, ``uv`` reports that death to the caller as 128 + the signal
|
|
7
|
+
number.
|
|
8
|
+
|
|
9
|
+
Codes 0, 1, 2, 129, 130, and 143 are shared. Scripts declare outcomes from 3
|
|
10
|
+
through 125; a later assignment overrides an earlier name for the same code.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import atexit
|
|
16
|
+
from collections.abc import Callable
|
|
17
|
+
import os
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
import signal
|
|
20
|
+
import subprocess
|
|
21
|
+
import sys
|
|
22
|
+
from types import FrameType
|
|
23
|
+
|
|
24
|
+
RESULT_CODES: dict[int, str] = {
|
|
25
|
+
0: 'SUCCESS',
|
|
26
|
+
1: 'SCRIPT_FAILURE',
|
|
27
|
+
2: 'PREFLIGHT_ERROR',
|
|
28
|
+
129: 'SIGNAL_HUP',
|
|
29
|
+
130: 'SIGNAL_INT',
|
|
30
|
+
143: 'SIGNAL_TERM',
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
_SIGNAL_CODES = {signal.SIGHUP: 129, signal.SIGINT: 130, signal.SIGTERM: 143}
|
|
34
|
+
|
|
35
|
+
_result_emitted = False
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def emit_result(code: int) -> None:
|
|
39
|
+
"""Print the RESULT line for `code`, at most once per process."""
|
|
40
|
+
global _result_emitted
|
|
41
|
+
if _result_emitted:
|
|
42
|
+
return
|
|
43
|
+
_result_emitted = True
|
|
44
|
+
print(f'RESULT={RESULT_CODES.get(code, f"UNKNOWN_CODE_{code}")}')
|
|
45
|
+
sys.stdout.flush()
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def quit_by_code(code: int) -> None:
|
|
49
|
+
"""Exit with `code` after naming it, keeping RESULT the last line of stdout."""
|
|
50
|
+
emit_result(code)
|
|
51
|
+
sys.exit(code)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# `$?` has no Python equivalent readable from atexit, so the status is captured
|
|
55
|
+
# as it is produced: SystemExit carries its own code, and any other escaping
|
|
56
|
+
# exception is the interpreter's exit status 1.
|
|
57
|
+
_exit_status = 0
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _record_exit(exception: BaseException) -> None:
|
|
61
|
+
"""Remember the status `exception` will terminate the interpreter with."""
|
|
62
|
+
global _exit_status
|
|
63
|
+
if isinstance(exception, SystemExit):
|
|
64
|
+
code = exception.code
|
|
65
|
+
_exit_status = 0 if code is None else code if isinstance(code, int) else 1
|
|
66
|
+
else:
|
|
67
|
+
_exit_status = 1
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _report_unhandled_exit() -> None:
|
|
71
|
+
"""Keep the RESULT line total when a script exits without quit_by_code."""
|
|
72
|
+
emit_result(_exit_status)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _report_signal(signal_number: int, _frame: FrameType | None) -> None:
|
|
76
|
+
"""Name a terminating signal, restore its default action, then re-raise it."""
|
|
77
|
+
emit_result(_SIGNAL_CODES[signal.Signals(signal_number)])
|
|
78
|
+
signal.signal(signal_number, signal.SIG_DFL)
|
|
79
|
+
os.kill(os.getpid(), signal_number)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _excepthook(kind: type[BaseException], value: BaseException, _traceback: object) -> None:
|
|
83
|
+
"""
|
|
84
|
+
Record the exit status an escaping exception implies, and name it in one line.
|
|
85
|
+
|
|
86
|
+
A failed command's own stderr follows, since it usually says why.
|
|
87
|
+
"""
|
|
88
|
+
_record_exit(value)
|
|
89
|
+
print(f'ERROR: {Path(sys.argv[0]).name}: {kind.__name__}: {value}', file=sys.stderr)
|
|
90
|
+
detail = (
|
|
91
|
+
getattr(value, 'stderr', None)
|
|
92
|
+
if isinstance(value, subprocess.CalledProcessError)
|
|
93
|
+
else None
|
|
94
|
+
)
|
|
95
|
+
if isinstance(detail, bytes):
|
|
96
|
+
detail = detail.decode(errors='replace')
|
|
97
|
+
if detail:
|
|
98
|
+
sys.stderr.write(detail if detail.endswith('\n') else f'{detail}\n')
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def run(main: Callable[[], int | None]) -> None:
|
|
102
|
+
"""
|
|
103
|
+
Run `main` as the script body, then exit naming the status it produced.
|
|
104
|
+
|
|
105
|
+
The entry point every Python skill script uses. ``SystemExit`` bypasses
|
|
106
|
+
``sys.excepthook``, so a bare ``sys.exit(2)`` inside `main` is only
|
|
107
|
+
observable by catching it here — this is what keeps the RESULT name and the
|
|
108
|
+
exit status agreeing on every path.
|
|
109
|
+
"""
|
|
110
|
+
try:
|
|
111
|
+
code = main() or 0
|
|
112
|
+
except SystemExit as requested:
|
|
113
|
+
_record_exit(requested)
|
|
114
|
+
code = _exit_status
|
|
115
|
+
quit_by_code(code)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def install() -> None:
|
|
119
|
+
"""Install the exit hook, the exception hook, and the signal handlers."""
|
|
120
|
+
sys.excepthook = _excepthook
|
|
121
|
+
atexit.register(_report_unhandled_exit)
|
|
122
|
+
for terminating_signal in _SIGNAL_CODES:
|
|
123
|
+
signal.signal(terminating_signal, _report_signal)
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Shared helpers for agentdev skill scripts written in Python.
|
|
3
|
+
|
|
4
|
+
Imported from the ``agentdev-lib`` package, so a skill script ships no copy of
|
|
5
|
+
its own. Standard library only.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
import subprocess
|
|
12
|
+
import sys
|
|
13
|
+
|
|
14
|
+
PREFLIGHT_ERROR = 2
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Stop(Exception):
|
|
18
|
+
"""End the run with a declared result after the diagnostic was printed."""
|
|
19
|
+
|
|
20
|
+
def __init__(self, code: int) -> None:
|
|
21
|
+
super().__init__(code)
|
|
22
|
+
self.code = code
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# Characters bash's printf %q escapes with a backslash anywhere in a word.
|
|
26
|
+
_SHELL_SPECIAL = set(' \t\n\'"\\|&;()<>!{}*[?]^$`,')
|
|
27
|
+
_ANSI_C = {'\a': '\\a', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t',
|
|
28
|
+
'\v': '\\v', '\\': '\\\\', "'": "\\'", '\x1b': '\\E'} # fmt: skip
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def bash_quote(word: str) -> str:
|
|
32
|
+
"""Quote `word` exactly as bash's printf %q does, so the remote shell re-reads it."""
|
|
33
|
+
if not word:
|
|
34
|
+
return "''"
|
|
35
|
+
if any(not character.isprintable() for character in word):
|
|
36
|
+
quoted = ''.join(
|
|
37
|
+
_ANSI_C.get(character)
|
|
38
|
+
or (character if character.isprintable() else
|
|
39
|
+
''.join(f'\\{byte:03o}' for byte in character.encode()))
|
|
40
|
+
for character in word
|
|
41
|
+
) # fmt: skip
|
|
42
|
+
return f"$'{quoted}'"
|
|
43
|
+
quoted = []
|
|
44
|
+
for index, character in enumerate(word):
|
|
45
|
+
previous = word[index - 1] if index else ''
|
|
46
|
+
if character in _SHELL_SPECIAL:
|
|
47
|
+
quoted.append('\\')
|
|
48
|
+
elif character == '~' and (index == 0 or previous in '=:'):
|
|
49
|
+
quoted.append('\\')
|
|
50
|
+
elif character == '#' and index == 0:
|
|
51
|
+
quoted.append('\\')
|
|
52
|
+
quoted.append(character)
|
|
53
|
+
return ''.join(quoted)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def print_error(message: str) -> None:
|
|
57
|
+
"""Report a failure on stderr in the shared ERROR: form."""
|
|
58
|
+
print(f'ERROR: {message}', file=sys.stderr)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def run_tool(*arguments: str, cwd: Path | None = None) -> subprocess.CompletedProcess[str]:
|
|
62
|
+
"""Run a command, capturing its output, without raising on failure."""
|
|
63
|
+
return subprocess.run(list(arguments), check=False, capture_output=True, text=True, cwd=cwd)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def run_to_stderr(*arguments: str, cwd: Path | None = None) -> int:
|
|
67
|
+
"""Run a command with its stdout sent to stderr, keeping stdout for KEY=value lines."""
|
|
68
|
+
sys.stdout.flush()
|
|
69
|
+
return subprocess.run(list(arguments), check=False, stdout=sys.stderr, cwd=cwd).returncode
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def run_inherited(*arguments: str, cwd: Path | None = None) -> int:
|
|
73
|
+
"""Run a command on this process's own stdout and stderr."""
|
|
74
|
+
sys.stdout.flush()
|
|
75
|
+
return subprocess.run(list(arguments), check=False, cwd=cwd).returncode
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def require_git_repo() -> Path:
|
|
79
|
+
"""Return the repository's top level, or stop with a preflight error outside one."""
|
|
80
|
+
completed = run_tool('git', 'rev-parse', '--show-toplevel')
|
|
81
|
+
if completed.returncode != 0:
|
|
82
|
+
print_error('This script must be run inside a Git repository.')
|
|
83
|
+
raise Stop(PREFLIGHT_ERROR)
|
|
84
|
+
return Path(completed.stdout.strip())
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def current_branch() -> str:
|
|
88
|
+
"""Return the checked-out branch name, or HEAD when detached."""
|
|
89
|
+
return run_tool('git', 'rev-parse', '--abbrev-ref', 'HEAD').stdout.strip()
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def main_checkout_dir() -> Path | None:
|
|
93
|
+
"""Return the main checkout, which owns the Git directory linked worktrees share."""
|
|
94
|
+
completed = run_tool('git', 'rev-parse', '--path-format=absolute', '--git-common-dir')
|
|
95
|
+
if completed.returncode != 0:
|
|
96
|
+
return None
|
|
97
|
+
return Path(completed.stdout.strip()).parent
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def skill_root(script: str) -> Path:
|
|
101
|
+
"""Return the skill directory of a script under ``<skill>/agent-code/``."""
|
|
102
|
+
return Path(script).resolve().parents[1]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def help_header(description: str, script: str) -> str:
|
|
106
|
+
"""Return the description and skill root lines every script's help starts with."""
|
|
107
|
+
return f'{description}\n\nSkill root: {skill_root(script)}\n'
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentdev-lib
|
|
3
|
+
Version: 1.0
|
|
4
|
+
Summary: Code shared by the scripts of the agentdev plugins: result codes and git helpers.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# agentdev-lib
|
|
12
|
+
|
|
13
|
+
Code shared by the scripts of the agentdev plugins, published so that a script
|
|
14
|
+
in one installed plugin can use it without reaching into another plugin's files.
|
|
15
|
+
|
|
16
|
+
- `agentdev_lib.result_codes` — the exit-code table and the `RESULT=<NAME>` line.
|
|
17
|
+
- `agentdev_lib.skill_common` — `Stop`, `print_error`, git and tool runners.
|
|
18
|
+
- `agentdev_lib.git_default_branch` — recognize a repository's default branch.
|
|
19
|
+
|
|
20
|
+
Each plugin pins the exact version. A checkout of
|
|
21
|
+
[agent-devcontainer](https://github.com/plume-works/agent-devcontainer) runs
|
|
22
|
+
this source directly.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
agentdev_lib/__init__.py,sha256=hxGJQzIcYquB064i5L2oascW9uSAG7Sk7r3Z3L3wFig,58
|
|
2
|
+
agentdev_lib/git_default_branch.py,sha256=MRavfLaXhaS4ebzHVp50A2V1jqes5P9suIpMoTBzbfI,2366
|
|
3
|
+
agentdev_lib/result_codes.py,sha256=opDOSy0d7hMa4OpMIoTc3LyBH0rXiqPZzx0FXjRPGKM,3971
|
|
4
|
+
agentdev_lib/skill_common.py,sha256=GqbBZYC4-Eu6SuDM1BaPdW6yRf13AtAKenwKmgGqvkM,3904
|
|
5
|
+
agentdev_lib-1.0.dist-info/licenses/LICENSE,sha256=mWvLEBPs4WgUtH9MRdM8OHBvex33eH5bFDozKAEgaKs,1068
|
|
6
|
+
agentdev_lib-1.0.dist-info/METADATA,sha256=Z6gPdcT9aJg334KxCuPFnHUjLqnV4Hy2JJJv7hoU3XY,849
|
|
7
|
+
agentdev_lib-1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
+
agentdev_lib-1.0.dist-info/top_level.txt,sha256=Aa4A8XGfoQemuA23a_vTMl3J-ZeqtAnvf5liaJNYHCk,13
|
|
9
|
+
agentdev_lib-1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 plume-works
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
agentdev_lib
|