-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathaction.yml
More file actions
106 lines (95 loc) · 3.7 KB
/
Copy pathaction.yml
File metadata and controls
106 lines (95 loc) · 3.7 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
name: 'NBStripout Check'
description: 'Verify that Jupyter notebooks have their output stripped'
inputs:
python-version:
description: 'Python version to use (supports versions, ranges, or "3.x"). Can also use a .python-version file by setting this to an empty string and ensuring the file exists.'
required: false
default: '3.x'
paths:
description: 'Newline-separated list of paths to check (supports wildcards)'
required: false
default: '**/*.ipynb'
extra-keys:
description: 'Extra metadata keys to strip (space-separated)'
required: false
default: ''
keep-output:
description: 'Keep output in notebooks'
required: false
default: 'false'
keep-count:
description: 'Keep execution counts'
required: false
default: 'false'
strip-init-cells:
description: 'Strip init cells'
required: false
default: 'false'
runs:
using: 'composite'
steps:
- name: Setup Python
uses: actions/setup-python@v6
with:
python-version: ${{ inputs.python-version }}
pip-install: 'nbstripout'
- name: Check notebooks are stripped
shell: bash
env:
INPUT_PATHS: ${{ inputs.paths }}
INPUT_EXTRA_KEYS: ${{ inputs.extra-keys }}
INPUT_KEEP_OUTPUT: ${{ inputs.keep-output }}
INPUT_KEEP_COUNT: ${{ inputs.keep-count }}
INPUT_STRIP_INIT_CELLS: ${{ inputs.strip-init-cells }}
run: |
# Build nbstripout args as an array (bash 3.1+)
args=(--verify)
if [ "$INPUT_KEEP_OUTPUT" = "true" ]; then
args+=(--keep-output)
fi
if [ "$INPUT_KEEP_COUNT" = "true" ]; then
args+=(--keep-count)
fi
if [ "$INPUT_STRIP_INIT_CELLS" = "true" ]; then
args+=(--strip-init-cells)
fi
if [ -n "$INPUT_EXTRA_KEYS" ]; then
args+=(--extra-keys "$INPUT_EXTRA_KEYS")
fi
# Use Python for file discovery: correct ** glob handling on all platforms
# Outputs null-delimited paths and deduplicates overlapping patterns.
# INPUT_PATHS is quoted to prevent bash from expanding globs before Python sees them.
notebook_list="${RUNNER_TEMP:-/tmp}/nbstripout_filelist.txt"
python3 - "$INPUT_PATHS" << 'PYTHON' > "$notebook_list"
import sys
from pathlib import Path
raw_input = sys.argv[1]
patterns = [stripped for line in raw_input.splitlines() if (stripped := line.strip())]
seen = set()
for pat in patterns:
for p in sorted(Path(".").glob(pat)):
rp = str(p)
if p.is_file() and rp not in seen:
seen.add(rp)
sys.stdout.write(rp + "\0")
PYTHON
if [ ! -s "$notebook_list" ]; then
echo "No notebook files found matching: $INPUT_PATHS"
exit 0
fi
# Count null-delimited entries (POSIX-compatible, works on macOS and Linux)
notebook_count=$(tr -cd '\0' < "$notebook_list" | wc -c | tr -d ' ')
echo "Found $notebook_count notebook(s) to check."
# xargs -0 reads null-delimited paths (handles any filename characters)
# and batches automatically for ARG_MAX safety on large repos
if xargs -0 nbstripout "${args[@]}" < "$notebook_list"; then
echo "All notebooks are properly stripped! ✅"
else
echo ""
echo "================================================"
echo "Some notebooks have output that should be stripped."
echo "Please run: nbstripout <notebook-file>"
echo "Or install nbstripout as a git filter with: nbstripout --install"
echo "================================================"
exit 1
fi