Skip to content

Commit 53bb407

Browse files
committed
chore: extract glob for pyupgrade to separate script for cross-platform compatibility
'sh' in tox.ini does not work on Windows in PowerShell. Signed-off-by: Peter Schuster <p.schuster@pilz.de>
1 parent 3ccfa4f commit 53bb407

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

tools/run_pyupgrade.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
3+
# Wrapper around pyupgrade to perform a lookup of all *.py/*.pyi files in passed directories
4+
# and pass them to pyupgrade in a single invocation.
5+
#
6+
# Usage: run_pyupgrade.py <pyupgrade-args...> -- <dir...>
7+
8+
import subprocess # nosec - subprocess is used to run pyupgrade and not part of published library
9+
import sys
10+
from pathlib import Path
11+
12+
if '--' not in sys.argv:
13+
print('Usage: run_pyupgrade.py <pyupgrade-args...> -- <dir...>', file=sys.stderr)
14+
sys.exit(1)
15+
16+
sep = sys.argv.index('--')
17+
pyupgrade_args = sys.argv[1:sep]
18+
directories = sys.argv[sep + 1:]
19+
20+
if not directories:
21+
print('Error: at least one directory must be specified after --', file=sys.stderr)
22+
sys.exit(1)
23+
24+
files = sorted({
25+
str(file)
26+
for directory in directories
27+
for pattern in ['*.py', '*.pyi']
28+
for file in Path(directory).rglob(pattern)
29+
})
30+
31+
result = subprocess.run( # nosec - shell=False is used to prevent injection, all arg passed as a list
32+
[sys.executable, '-m', 'pyupgrade', *pyupgrade_args, *files],
33+
shell=False # w/o shell all args are passed directly to the process without the need for quotes or escaping
34+
)
35+
sys.exit(result.returncode)

tox.ini

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ commands =
5252
poetry run deptry -v .
5353

5454
[testenv:pyupgrade]
55-
allowlist_externals = poetry, sh
56-
commands = sh -c "\
57-
find cyclonedx typings tests tools examples -type f \( -name '*.py' -or -name '*.pyi' \) -print0 \
58-
| xargs -0 poetry run pyupgrade --py39-plus {posargs} "
55+
# first -- stops command parsing by poetry run, the second -- splits pyupgrade args from args for glob patterns
56+
commands = poetry run -- python tools/run_pyupgrade.py --py39-plus {posargs} -- cyclonedx typings tests tools examples
5957

6058
[testenv:isort]
6159
commands = poetry run isort .

0 commit comments

Comments
 (0)