summaryrefslogtreecommitdiff
path: root/dev-python/anthropic/anthropic-0.86.0.ebuild
blob: 00fa49d4ed7aed56269e717d66020e75e656b3c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright 2024-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DISTUTILS_USE_PEP517=hatchling
PYTHON_COMPAT=( python3_{12..14} )

inherit distutils-r1 optfeature pypi

# See https://github.com/anthropics/anthropic-sdk-python/blob/main/.stats.yml
API_SPEC_BASE="https://storage.googleapis.com/stainless-sdk-openapi-specs"
API_SPEC="anthropic-openapi-spec-v2026.03.18.yml"
MY_PN="anthropic-sdk-python"
STDY_PV=0.19.7
DESCRIPTION="The official Python library for the anthropic API"
HOMEPAGE="
	https://github.com/anthropics/anthropic-sdk-python
	https://pypi.org/project/anthropic
"
SRC_URI="
	https://github.com/anthropics/${MY_PN}/archive/refs/tags/v${PV}.tar.gz
		-> ${MY_PN}-${PV}.gh.tar.gz

	test? (
		${API_SPEC_BASE}/anthropic%2Fanthropic-dd2dcd00a757075370a7e4a7f469a1e2d067c2118684c3b70d7906a8f5cf518b.yml
			-> ${API_SPEC}

		https://registry.npmjs.org/@stdy/cli/-/cli-${STDY_PV}.tgz
			-> npm-@stdy-cli-cli-${STDY_PV}.tgz

		amd64? (
			https://registry.npmjs.org/@stdy/cli-linux-x64/-/cli-linux-x64-${STDY_PV}.tgz
				-> npm-@stdy-cli-linux-x64-cli-linux-x64-${STDY_PV}.tgz
		)

		arm64? (
			https://registry.npmjs.org/@stdy/cli-linux-arm64/-/cli-linux-arm64-${STDY_PV}.tgz
				-> npm-@stdy-cli-linux-arm64-cli-linux-arm64-${STDY_PV}.tgz
		)
	)
"
S="${WORKDIR}/${MY_PN}-${PV}"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64 ~arm64"

RDEPEND="
	>=dev-python/anyio-3.5.0[${PYTHON_USEDEP}]
	>=dev-python/distro-1.7.0[${PYTHON_USEDEP}]
	>=dev-python/docstring-parser-0.15[${PYTHON_USEDEP}]
	>=dev-python/httpx-0.25.0[${PYTHON_USEDEP}]
	>=dev-python/jiter-0.4.0[${PYTHON_USEDEP}]
	>=dev-python/pydantic-1.9.0[${PYTHON_USEDEP}]
	dev-python/sniffio[${PYTHON_USEDEP}]
	>=dev-python/typing-extensions-4.14[${PYTHON_USEDEP}]
"

BDEPEND="
	dev-python/hatch-fancy-pypi-readme[${PYTHON_USEDEP}]
	test? (
		>=dev-python/boto3-1.28.57[${PYTHON_USEDEP}]
		>=dev-python/botocore-1.31.57[${PYTHON_USEDEP}]
		dev-python/httpx-aiohttp[${PYTHON_USEDEP}]
		>=dev-python/rich-13.7.1[${PYTHON_USEDEP}]
		net-libs/nodejs[npm]
		net-misc/curl
	)
"

EPYTEST_PLUGINS=(
	dirty-equals
	http-snapshot
	inline-snapshot
	pytest-{asyncio,xdist}
	respx
	time-machine
)
distutils_enable_tests pytest

src_unpack() {
	unpack "${MY_PN}-${PV}.gh.tar.gz"
}

src_test() {
	einfo "Assembling npm cache..."

	local -x npm_config_cache="${WORKDIR}/npm-cache"
	mkdir -p "${npm_config_cache}" || die

	for distfile in ${A}; do
		if [[ "${distfile}" == npm-* ]]; then
			npm cache add "${DISTDIR}/${distfile}" &>/dev/null || die
		fi
	done

	einfo "Installing mock server..."

	local mock_dir="${WORKDIR}/mock-server"
	mkdir -p "${mock_dir}" || die

	cp "${FILESDIR}/${PN}-0.86.0-mock-server-package.json" \
		"${mock_dir}/package.json" || die
	cp "${FILESDIR}/${PN}-0.86.0-mock-server-package-lock.json" \
		"${mock_dir}/package-lock.json" || die

	local mock_dir="${WORKDIR}/mock-server"
	local mock="${mock_dir}/node_modules/.bin/steady"

	pushd "${mock_dir}" >/dev/null || die

	npm ci &>/dev/null || die

	einfo "Starting mock server..."

	# Replicate the logic from scripts/mock --daemon
	"${mock}" --host 127.0.0.1 -p 4010 \
		--validator-form-array-format=brackets \
		--validator-query-array-format=brackets \
		--validator-form-object-format=brackets \
		--validator-query-object-format=brackets \
		"${DISTDIR}/${API_SPEC}" &> .stdy.log &
	local mock_pid=$!

	is_mock_running() {
		local -a args
		readarray -d '' args < "/proc/${mock_pid}/cmdline" 2>/dev/null || return 1
		[[ "${args[1]}" == "${mock}" ]]
	}

	local attempts=0
	while ! curl -sf "http://127.0.0.1:4010/_x-steady/health" &>/dev/null; do
		if ! is_mock_running; then
			cat .stdy.log
			die "Mock server failed to start"
		fi
		attempts=$((attempts + 1))
		if (( attempts >= 300 )); then
			cat .stdy.log
			die "Timed out waiting for mock server to start"
		fi
		sleep 0.1
	done

	# Oops; connected to another Steady instance running on 4010
	is_mock_running || die

	popd >/dev/null || die

	nonfatal distutils-r1_src_test
	local ret=${?}

	if is_mock_running; then
		einfo "Stopping mock server..."
		kill "${mock_pid}" || die
	fi

	[[ ${ret} -ne 0 ]] && die
}

pkg_postinst() {
	optfeature "alternative async HTTP client support" \
		"dev-python/aiohttp >=dev-python/httpx-aiohttp-0.1.9"
	optfeature "Google Cloud Vertex AI integration" \
		">=dev-python/google-auth-2 dev-python/requests"
	optfeature "Amazon Web Services (AWS) Bedrock integration" \
		">=dev-python/boto3-1.28.57 >=dev-python/botocore-1.31.57"
	optfeature "Model Context Protocol (MCP) support" \
		">=dev-python/mcp-1.0"
}