-
Notifications
You must be signed in to change notification settings - Fork 0
336 lines (292 loc) · 11.6 KB
/
Copy pathpublish-python.yml
File metadata and controls
336 lines (292 loc) · 11.6 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
name: Publish Python SDK
on:
workflow_dispatch:
release:
types:
- published
push:
branches:
- main
jobs:
preflight:
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
publish_needed: ${{ steps.pypi_check.outputs.publish_needed }}
version: ${{ steps.meta.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Read package metadata
id: meta
run: |
python - <<'PY'
import os
import tomllib
with open("bindings/python/pyproject.toml", "rb") as f:
project = tomllib.load(f)["project"]
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as out:
out.write(f"name={project['name']}\n")
out.write(f"version={project['version']}\n")
PY
- name: Check if version is already on PyPI
id: pypi_check
run: |
python - <<'PY'
import json
import os
import tomllib
import urllib.error
import urllib.request
with open("bindings/python/pyproject.toml", "rb") as f:
project = tomllib.load(f)["project"]
name = project["name"]
version = project["version"]
url = f"https://pypi.org/pypi/{name}/json"
publish_needed = True
try:
with urllib.request.urlopen(url, timeout=15) as resp:
payload = json.load(resp)
releases = payload.get("releases", {})
publish_needed = version not in releases
except urllib.error.HTTPError as e:
if e.code != 404:
raise
print(f"{name} {version} publish_needed={publish_needed}")
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as out:
out.write(f"publish_needed={'true' if publish_needed else 'false'}\n")
PY
build_native:
needs: preflight
if: needs.preflight.outputs.publish_needed == 'true'
strategy:
fail-fast: false
matrix:
include:
- os: windows-2022
artifact: win32-x64
- os: ubuntu-22.04
artifact: linux-x64
- os: macos-15-intel
artifact: darwin-x64
- os: macos-14
artifact: darwin-arm64
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- name: Setup MSYS2 (Windows onionrelay toolchain)
if: runner.os == 'Windows'
uses: msys2/setup-msys2@v2
with:
msystem: MINGW64
update: true
install: >-
mingw-w64-x86_64-toolchain
mingw-w64-x86_64-openssl
mingw-w64-x86_64-libevent
mingw-w64-x86_64-xz
mingw-w64-x86_64-zstd
mingw-w64-x86_64-zlib
autoconf
automake
libtool
make
pkgconf
gettext
- name: Install Linux dependencies (onionrelay)
if: runner.os == 'Linux'
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y \
build-essential \
pkg-config \
libevent-dev \
libssl-dev \
zlib1g-dev \
liblzma-dev \
libzstd-dev \
automake \
libtool \
autoconf \
gettext
- name: Install macOS dependencies (onionrelay)
if: runner.os == 'macOS'
shell: bash
run: |
brew update
brew install \
pkg-config \
libevent \
openssl@3 \
xz \
zstd \
automake \
libtool \
autoconf \
gettext
- name: Build native core (Windows)
if: runner.os == 'Windows'
shell: powershell
run: .\scripts\build_p4_core.ps1
- name: Build native core (Linux/macOS)
if: runner.os != 'Windows'
shell: bash
run: |
chmod +x scripts/build_p4_core_unix.sh
./scripts/build_p4_core_unix.sh
- name: Build onionrelay (Windows)
if: runner.os == 'Windows'
shell: powershell
env:
MSYS2_ROOT: ${{ runner.temp }}\\msys64
run: .\build_onionrelay_windows.ps1
- name: Build onionrelay (Linux/macOS)
if: runner.os != 'Windows'
shell: bash
run: |
chmod +x scripts/build_onionrelay_unix.sh
./scripts/build_onionrelay_unix.sh "${GITHUB_WORKSPACE}/dist" onionrelay
- name: Stage bundled payload (Windows)
if: runner.os == 'Windows'
shell: powershell
run: |
New-Item -ItemType Directory -Force -Path out\win32-x64\core | Out-Null
New-Item -ItemType Directory -Force -Path out\win32-x64\onionrelay | Out-Null
Copy-Item dist\p4_core\windows-x64\p4_core.dll out\win32-x64\core\p4_core.dll -Force
Copy-Item onionrelay_src\src\app\onionrelay.exe out\win32-x64\onionrelay\onionrelay.exe -Force
$dlls = Get-ChildItem onionrelay_src\src\app\*.dll -ErrorAction SilentlyContinue
foreach ($dll in $dlls) {
Copy-Item $dll.FullName out\win32-x64\onionrelay\ -Force
}
- name: Stage bundled payload (Linux)
if: runner.os == 'Linux'
shell: bash
run: |
mkdir -p out/linux-x64/core
mkdir -p out/linux-x64/onionrelay
cp -f dist/p4_core/linux-x64/libp4_core.so out/linux-x64/core/libp4_core.so
cp -f dist/onionrelay out/linux-x64/onionrelay/onionrelay
chmod +x out/linux-x64/onionrelay/onionrelay
- name: Stage bundled payload (macOS)
if: runner.os == 'macOS'
shell: bash
run: |
mkdir -p "out/${{ matrix.artifact }}/core"
mkdir -p "out/${{ matrix.artifact }}/onionrelay"
cp -f dist/p4_core/macos/libp4_core.dylib "out/${{ matrix.artifact }}/core/libp4_core.dylib"
cp -f dist/onionrelay "out/${{ matrix.artifact }}/onionrelay/onionrelay"
chmod +x "out/${{ matrix.artifact }}/onionrelay/onionrelay"
- name: Upload native artifact
uses: actions/upload-artifact@v4
with:
name: python-native-${{ matrix.artifact }}
path: out/**
publish:
needs: [preflight, build_native]
if: needs.preflight.outputs.publish_needed == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Download native artifact (Windows x64)
uses: actions/download-artifact@v4
with:
name: python-native-win32-x64
path: tmp_native/win32
- name: Download native artifact (Linux x64)
uses: actions/download-artifact@v4
with:
name: python-native-linux-x64
path: tmp_native/linux
- name: Download native artifact (macOS x64)
uses: actions/download-artifact@v4
with:
name: python-native-darwin-x64
path: tmp_native/darwin-x64
- name: Download native artifact (macOS arm64)
uses: actions/download-artifact@v4
with:
name: python-native-darwin-arm64
path: tmp_native/darwin-arm64
- name: Stage downloaded native payload
shell: bash
run: |
mkdir -p bindings/python/p4_core/native
mkdir -p bindings/python/p4_core/onionrelay
find bindings/python/p4_core/native -mindepth 1 -maxdepth 1 ! -name '__init__.py' ! -name '.gitkeep' -exec rm -rf {} +
find bindings/python/p4_core/onionrelay -mindepth 1 -maxdepth 1 ! -name '.gitkeep' -exec rm -rf {} +
rm -rf bindings/python/p4_core/native/win32-x64
rm -rf bindings/python/p4_core/native/linux-x64
rm -rf bindings/python/p4_core/native/darwin-x64
rm -rf bindings/python/p4_core/native/darwin-arm64
rm -rf bindings/python/p4_core/onionrelay/win32-x64
rm -rf bindings/python/p4_core/onionrelay/linux-x64
rm -rf bindings/python/p4_core/onionrelay/darwin-x64
rm -rf bindings/python/p4_core/onionrelay/darwin-arm64
mkdir -p bindings/python/p4_core/native/win32-x64
mkdir -p bindings/python/p4_core/native/linux-x64
mkdir -p bindings/python/p4_core/native/darwin-x64
mkdir -p bindings/python/p4_core/native/darwin-arm64
mkdir -p bindings/python/p4_core/onionrelay/win32-x64
mkdir -p bindings/python/p4_core/onionrelay/linux-x64
mkdir -p bindings/python/p4_core/onionrelay/darwin-x64
mkdir -p bindings/python/p4_core/onionrelay/darwin-arm64
WIN_SRC="$(find tmp_native/win32 -type f -path '*core/p4_core.dll' | head -n1)"
LIN_SRC="$(find tmp_native/linux -type f -name 'libp4_core.so' | head -n1)"
DARWIN_X64="$(find tmp_native/darwin-x64 -type f -name 'libp4_core.dylib' | head -n1)"
DARWIN_ARM="$(find tmp_native/darwin-arm64 -type f -name 'libp4_core.dylib' | head -n1)"
WIN_ONION_DIR="$(find tmp_native/win32 -type d -name onionrelay | head -n1)"
LIN_ONION="$(find tmp_native/linux -type f -path '*onionrelay/onionrelay' | head -n1)"
DARWIN_X64_ONION="$(find tmp_native/darwin-x64 -type f -path '*onionrelay/onionrelay' | head -n1)"
DARWIN_ARM_ONION="$(find tmp_native/darwin-arm64 -type f -path '*onionrelay/onionrelay' | head -n1)"
if [ -z "${WIN_SRC}" ] || [ -z "${LIN_SRC}" ] || [ -z "${DARWIN_ARM}" ] || [ -z "${DARWIN_X64}" ] || [ -z "${WIN_ONION_DIR}" ] || [ -z "${LIN_ONION}" ] || [ -z "${DARWIN_X64_ONION}" ] || [ -z "${DARWIN_ARM_ONION}" ]; then
echo "Failed to locate expected native/onionrelay artifacts."
find tmp_native -maxdepth 5 -type f -print
exit 1
fi
cp -f "${WIN_SRC}" bindings/python/p4_core/native/win32-x64/p4_core.dll
cp -f "${LIN_SRC}" bindings/python/p4_core/native/linux-x64/libp4_core.so
cp -f "${DARWIN_X64}" bindings/python/p4_core/native/darwin-x64/libp4_core.dylib
cp -f "${DARWIN_ARM}" bindings/python/p4_core/native/darwin-arm64/libp4_core.dylib
cp -f "${WIN_ONION_DIR}"/* bindings/python/p4_core/onionrelay/win32-x64/
cp -f "${LIN_ONION}" bindings/python/p4_core/onionrelay/linux-x64/onionrelay
cp -f "${DARWIN_X64_ONION}" bindings/python/p4_core/onionrelay/darwin-x64/onionrelay
cp -f "${DARWIN_ARM_ONION}" bindings/python/p4_core/onionrelay/darwin-arm64/onionrelay
chmod +x bindings/python/p4_core/onionrelay/linux-x64/onionrelay
chmod +x bindings/python/p4_core/onionrelay/darwin-x64/onionrelay
chmod +x bindings/python/p4_core/onionrelay/darwin-arm64/onionrelay
- name: Build wheel + sdist
run: |
python -m pip install --upgrade pip build twine
python -m build bindings/python
- name: Publish to PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
if [ -z "${TWINE_PASSWORD}" ]; then
echo "PYPI_API_TOKEN secret is missing"
exit 1
fi
twine upload bindings/python/dist/*
skip:
needs: preflight
if: needs.preflight.outputs.publish_needed != 'true'
runs-on: ubuntu-latest
steps:
- run: echo "Version already exists on PyPI; skipping publish."