|
| 1 | +import re |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from update_versions_bug_report import main as update_bug_report |
| 6 | + |
| 7 | +from utils import root_dir |
| 8 | + |
| 9 | + |
| 10 | +def read_version_from_citation() -> str: |
| 11 | + with open(root_dir() / "CITATION.cff", encoding="utf-8") as file: |
| 12 | + for line in file: |
| 13 | + if line.startswith("version:"): |
| 14 | + version = line.strip().replace("version: ", "v") |
| 15 | + with open( |
| 16 | + root_dir() / "version.txt", "w", encoding="utf-8" |
| 17 | + ) as version_file: |
| 18 | + version_file.write(version) |
| 19 | + return version[1:] # Remove the leading 'v' |
| 20 | + |
| 21 | + |
| 22 | +def update_file(file_path, pattern, replacement): |
| 23 | + file_path = Path(file_path) |
| 24 | + content = file_path.read_text(encoding="utf-8") |
| 25 | + new_content = re.sub(pattern, replacement, content) |
| 26 | + file_path.write_text(new_content, encoding="utf-8") |
| 27 | + |
| 28 | + |
| 29 | +def main(): |
| 30 | + version = read_version_from_citation() |
| 31 | + |
| 32 | + if not version: |
| 33 | + print("Version not found in CITATION.cff") |
| 34 | + return |
| 35 | + |
| 36 | + update_file("README.md", r"version = {.*}", f"version = {{{version}}}") |
| 37 | + update_file("README.md", r"__version__ = .*", f"version = {{{version}}}") |
| 38 | + update_file( |
| 39 | + "src/reports/bidspm.bib", r" version = {.*}", f" version = {{{version}}}" |
| 40 | + ) |
| 41 | + |
| 42 | + tools_dir = Path("tools") |
| 43 | + |
| 44 | + versions_txt_path = tools_dir / "versions.txt" |
| 45 | + versions = ( |
| 46 | + subprocess.run(["git", "tag", "--list"], capture_output=True, text=True) |
| 47 | + .stdout.strip() |
| 48 | + .split("\n")[::-1] |
| 49 | + ) |
| 50 | + versions_txt_path.write_text("\n".join(versions), encoding="utf-8") |
| 51 | + |
| 52 | + update_bug_report() |
| 53 | + |
| 54 | + print(f"Version updated to {version}") |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + main() |
0 commit comments