-
Notifications
You must be signed in to change notification settings - Fork 0
348 lines (308 loc) · 11.6 KB
/
wipac-cicd.yaml
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
337
338
339
340
341
342
343
344
345
346
347
348
name: ci/cd
on:
push:
branches:
- '**'
tags-ignore:
- '**'
jobs:
###########################################################################
# PACKAGING
###########################################################################
py-setup:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
- uses: WIPACrepo/[email protected]
with:
python_min: 3.11
python_max: 3.11
pypi_name: icecube-simprod-histogram
author: IceCube
author_email: [email protected]
keywords: |
"histogram sampling" simulation statistics
###########################################################################
# LINTERS
###########################################################################
py-versions:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.versions.outputs.matrix }}
steps:
- uses: actions/checkout@v3
- id: versions
uses: WIPACrepo/[email protected]
flake8:
needs: [ py-versions ]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
py3: ${{ fromJSON(needs.py-versions.outputs.matrix) }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.py3 }}
- uses: WIPACrepo/[email protected]
mypy:
needs: [ py-versions ]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
py3: ${{ fromJSON(needs.py-versions.outputs.matrix) }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.py3 }}
- uses: WIPACrepo/[email protected]
###########################################################################
# FORMATTER
###########################################################################
code-format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- name: Run Ruff for code formatting
run: |
set -euo pipefail
pip install ruff
ruff check --select C408 --fix . --unsafe-fixes
- name: Commit formatted code
run: |
set -euo pipefail
git config user.name github-actions
git config user.email [email protected]
git add .
git commit -m "<bot> auto code format file(s)" || true
git push || true
###########################################################################
# TESTS
###########################################################################
unit-tests:
needs: [ py-versions ]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
py3: ${{ fromJSON(needs.py-versions.outputs.matrix) }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.py3 }}
- name: install
run: |
set -euo pipefail
pip install .[tests]
- name: Run unit tests
run: |
set -euo pipefail
pytest -vvv tests/unit/
test-sample-each-dataset-sh:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
max_num_datasets:
- 1
- 25
- 100 # aka all of them, currently, there are 48
base_path:
- /tmp/data/sim/Upgrade/2022/generated/neutrino-generator/88888
- /tmp/data/sim/IceCube/2023/filtered/CORSIKA
- /tmp/data/sim/Upgrade/2022/filtered
- /tmp/data/sim/IceCube/2023
- /tmp/data/sim/Upgrade
- /tmp/data/sim
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
- name: Create source dataset dirs/files
run: |
set -euo pipefail
job_range_dpaths=(
/tmp/data/sim/{IceCube,Upgrade}/{2022,2023}/{generated,filtered}/{CORSIKA,neutrino-generator}/{77777,88888,99999}/{00-11,22-33,44-55}
)
for dpath in "${job_range_dpaths[@]}"; do
mkdir -p "$dpath"/histos/
# create 1-5 pkl files
for i in $( seq 1 "$(( (RANDOM % 5) + 1 ))" ); do
random_file=$(find "tests/data/simprod-histograms" -type f -name "*.pkl" | shuf -n 1)
cp "$random_file" "$dpath/histos/histo_$i.pkl"
done
done
- name: Look at filetree (before)
run: |
set -euo pipefail
tree /tmp/data/sim/
- name: Run script
run: |
set -euo pipefail
set -x
./scripts/sample-each-dataset.sh ${{ matrix.base_path }} 0.5 ${{ matrix.max_num_datasets }}
- name: Validate script execution
run: |
set -euo pipefail
echo "Max num of datasets: ${{ matrix.max_num_datasets }}"
# Count dataset directories containing at least one "*.histo.hdf5" file
available_datasets=$(find ${{ matrix.base_path }} -type d -regex ".*/[0-9]+-[0-9]+$" -exec dirname {} \; | sort -u | wc -l)
echo "Available datasets: $available_datasets"
# Use the lesser of available_datasets and num_datasets for validation
expected_num_datasets=$(( available_datasets < ${{ matrix.max_num_datasets }} ? available_datasets : ${{ matrix.max_num_datasets }} ))
echo "Expected datasets: $expected_num_datasets"
# Check processed count
processed_count=$(find ${{ matrix.base_path }} -name '*.histo.hdf5' | wc -l)
echo "Processed count: $processed_count"
if [[ $processed_count -ne $expected_num_datasets ]]; then
echo "Script did not process the expected number of datasets!"
exit 1
fi
echo "All tests passed."
- name: Look at filetree (after)
run: |
set -euo pipefail
tree /tmp/data/sim/
test-cp-dataset-histos-sh:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
prev_histos_setting:
- none
- overwrite
- keep
base_path:
- /tmp/data/sim/IceCube/2023/generated/neutrino-generator
- /tmp/data/sim/Upgrade/2022/filtered/CORSIKA
dest_dir:
- /tmp/mycopy
env:
OLD_FILE_MODTIME: 0
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
- name: Create source dataset dirs/files
run: |
set -euo pipefail
dataset_dpaths=(
/tmp/data/sim/{IceCube,Upgrade}/{2022,2023}/{generated,filtered}/{CORSIKA,neutrino-generator}/{77777,88888,99999}/
)
for dpath in "${dataset_dpaths[@]}"; do
histo="$dpath"/"$(basename $dpath).histo.hdf5"
mkdir $(dirname $histo)
touch $histo
# pre-create some of these files in the destination
if [[ "${{ matrix.prev_histos_setting }}" == "overwrite" || "${{ matrix.prev_histos_setting }}" == "keep" ]]; then
if (( RANDOM % 100 < 25 )); then
relative_path="${dpath#*/sim/}"
dest_dataset_dir="${{ matrix.dest_dir }}/sim/$relative_path"
touch "$dest_dataset_dir"/"$(basename "$dest_dataset_dir").histo.hdf5"
fi
fi
done
# set the oldest file's mod time
if [[ "${{ matrix.prev_histos_setting }}" == "overwrite" || "${{ matrix.prev_histos_setting }}" == "keep" ]]; then
oldest_modtime=$(find "${{ matrix.dest_dir }}" -name "*.histo.hdf5" -type f -exec stat --format='%Y' {} + | sort -n | head -1)
echo "OLD_FILE_MODTIME=$oldest_modtime" >> $GITHUB_ENV
fi
- name: Look at src filetree (before)
run: |
set -euo pipefail
tree /tmp/data/sim/
- name: Look at dest filetree (before)
run: |
set -euo pipefail
tree ${{ matrix.dest_dir }}
- name: Run script
run: |
set -euo pipefail
prev_histos_setting=""
if [[ "${{ matrix.prev_histos_setting }}" == "overwrite" ]]; then
force_flag="--force"
fi
set -x
./cp-dataset-histos.sh ${{ matrix.base_path }} ${{ matrix.dest_dir }} $force_flag
- name: Validate copied histograms
run: |
set -euo pipefail
src_count=$(find ${{ matrix.base_path }} -name "*.histo.hdf5" | wc -l)
dest_count=$(find ${{ matrix.dest_dir }} -name "*.histo.hdf5" | wc -l)
echo "Source histograms: $src_count"
echo "Copied histograms: $dest_count"
if [[ $src_count -ne $dest_count ]]; then
echo "Copied histograms count ($dest_count) does not match source histograms count ($src_count)!"
exit 1
fi
# check the overwriting settings
oldest_modtime=$(find "${{ matrix.dest_dir }}" -name "*.histo.hdf5" -type f -exec stat --format='%Y' {} + | sort -n | head -1)
echo "Oldest histo file modtime: $oldest_modtime"
case "${{ matrix.prev_histos_setting }}" in
none)
# oldest modtime should be younger than previously-stored value
if [[ ! $oldest_modtime -lt $OLD_FILE_MODTIME ]]; then
echo "ERROR: there is an older file in here!" >&2
exit 1
fi
;;
overwrite)
# oldest modtime should be younger than previously-stored value
if [[ ! $oldest_modtime -lt $OLD_FILE_MODTIME ]]; then
echo "ERROR: there is an older file in here! aka script didn't overwrite" >&2
exit 1
fi
;;
keep)
# oldest modtime should be the previously-stored value
if [[ $oldest_modtime -ne $OLD_FILE_MODTIME ]]; then
echo "ERROR: there is no older file in here! aka the scrip did overwrite" >&2
exit 1
fi
;;
*)
echo "Error: Unknown value for prev_histos_setting: $prev_histos_setting" >&2
exit 1
;;
esac
echo "All tests passed for base_path=${{ matrix.base_path }} and dest_dir=${{ matrix.dest_dir }}."
- name: Look at dest filetree (after)
run: |
set -euo pipefail
tree ${{ matrix.dest_dir }}
###########################################################################
# RELEASE
###########################################################################
release:
if: github.ref == 'refs/heads/main'
needs: [ py-setup, flake8, mypy, code-format, unit-tests, test-sample-each-dataset-sh, test-cp-dataset-histos-sh ]
runs-on: ubuntu-latest
concurrency: release # prevent any possible race conditions
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Python-Package Version Bump
- uses: python-semantic-release/[email protected]
id: psr-psr
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
# PyPI Release
- uses: pypa/[email protected]
if: steps.psr-psr.outputs.released == 'true'
with:
password: ${{ secrets.WIPAC_PYPI_TOKEN }}
# GitHub Release
- uses: python-semantic-release/[email protected]
if: steps.psr-psr.outputs.released == 'true'
with:
github_token: ${{ secrets.GITHUB_TOKEN }}