-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_requirements_sync.sh
More file actions
executable file
·68 lines (60 loc) · 2 KB
/
Copy pathcheck_requirements_sync.sh
File metadata and controls
executable file
·68 lines (60 loc) · 2 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
#!/usr/bin/env bash
set -euo pipefail
python - <<'PY'
from pathlib import Path
import re
pyproject_text = Path('pyproject.toml').read_text(encoding='utf-8')
# Read only [project].dependencies = [ ... ] entries pinned as name==version
in_project = False
in_deps = False
deps = []
for raw in pyproject_text.splitlines():
line = raw.strip()
if line.startswith('[') and line.endswith(']'):
in_project = line == '[project]'
if line != '[project]':
in_deps = False
continue
if not in_project:
continue
if line.startswith('dependencies') and '[' in line:
in_deps = True
continue
if in_deps:
if ']' in line:
in_deps = False
continue
m = re.match(r'"([A-Za-z0-9_.-]+)==([^"\s]+)"\s*,?$', line)
if m:
deps.append((m.group(1), m.group(2)))
pattern = re.compile(r'^\s*([A-Za-z0-9_.-]+)==([^\s#]+)\s*$')
req_bytes = Path('requirements.txt').read_bytes()
for enc in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be'):
try:
req_text = req_bytes.decode(enc)
break
except UnicodeDecodeError:
continue
else:
raise SystemExit('Não foi possível decodificar requirements.txt (UTF-8/UTF-16).')
req_pins = {}
for line in req_text.splitlines():
m = pattern.match(line)
if m:
req_pins[m.group(1).lower().replace('_', '-').replace('.', '-')] = m.group(2)
missing, mismatch = [], []
for name, exp in deps:
key = name.lower().replace('_', '-').replace('.', '-')
got = req_pins.get(key)
if got is None:
missing.append(name)
elif got != exp:
mismatch.append((name, exp, got))
if missing or mismatch:
if missing:
print('Dependências diretas ausentes em requirements.txt:', ', '.join(missing))
for name, exp, got in mismatch:
print(f'Versão divergente para {name}: pyproject={exp}, requirements={got}')
raise SystemExit(1)
print('Dependências diretas de pyproject.toml estão sincronizadas em requirements.txt.')
PY