-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathcheck_version.py
executable file
·46 lines (37 loc) · 1.36 KB
/
check_version.py
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
#!/usr/bin/env python3
"""
Check the version in Cargo.toml matches the version from `GITHUB_REF` environment variable.
"""
import os
import re
import sys
from pathlib import Path
def main() -> int:
cargo_path = Path('Cargo.toml')
if not cargo_path.is_file():
print(f'✖ path "{cargo_path}" does not exist')
return 1
version_ref = os.getenv('GITHUB_REF')
if version_ref:
version = re.sub('^refs/tags/v*', '', version_ref.lower())
else:
print('✖ "GITHUB_REF" env variables not found')
return 1
# convert from python pre-release version to rust pre-release version
# this is the reverse of what's done in lib.rs::_rust_notify
version = version.replace('a', '-alpha').replace('b', '-beta')
version_regex = re.compile(r"""^version ?= ?(["'])(.+)\1""", re.M)
cargo_content = cargo_path.read_text()
match = version_regex.search(cargo_content)
if not match:
print(f'✖ {version_regex!r} not found in {cargo_path}')
return 1
cargo_version = match.group(2)
if cargo_version == version:
print(f'✓ GITHUB_REF version matches {cargo_path} version "{cargo_version}"')
return 0
else:
print(f'✖ GITHUB_REF version "{version}" does not match {cargo_path} version "{cargo_version}"')
return 1
if __name__ == '__main__':
sys.exit(main())