Skip to content

Commit 855a567

Browse files
authored
[MAINT] convert several maintenance scripts to python (#1268)
* use python instead of bash * ref * use python instead of bash * update make file
1 parent 6b2b192 commit 855a567

File tree

13 files changed

+181
-64
lines changed

13 files changed

+181
-64
lines changed

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
FROM bids/base_validator:1.13.1
22

3+
LABEL org.opencontainers.image.source="https://github.com/cpp-lln-lab/bidspm"
4+
LABEL org.opencontainers.image.url="https://github.com/cpp-lln-lab/bidspm"
5+
LABEL org.opencontainers.image.documentation="https://bidspm.readthedocs.io/en/latest"
6+
LABEL org.opencontainers.image.licenses="GPL-3.0"
7+
LABEL org.opencontainers.image.title="bidspm"
8+
LABEL org.opencontainers.image.description="an SPM-centric BIDS app"
9+
310
ARG DEBIAN_FRONTEND="noninteractive"
411

512
## basic OS tools install octave

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ update: ## Tries to get the latest version of the current branch from upstream
5959
fix_submodule: ## Fix any submodules that would not be checked out
6060
git submodule update --init --recursive && git submodule update --recursive
6161

62-
bump_version:
63-
bash tools/bump_version.sh
62+
prepare_release:
63+
python tools/citation_cff_maint.py
64+
python tools/tools/add_links_to_changelog.py
65+
python tools/bump_version.py
6466

6567
validate_cff: ## Validate the citation file
6668
cffconvert --validate
6769

68-
release: validate_cff bump_version lint manual
70+
release: validate_cff prepare_release lint manual
6971

7072

7173
################################################################################

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Please see our [documentation](https://bidspm.readthedocs.io/en/latest/index.htm
169169
license = {GPL-3.0},
170170
title = {{bidspm}},
171171
url = {https://github.com/cpp-lln-lab/bidspm},
172-
version = {3.1.0}
172+
version = {3.1.0dev}
173173
}
174174
```
175175

docs/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ update_faq:
4646
cd faq/spm && faqtory build
4747
cd faq/stats && faqtory build
4848

49-
bidspm-manual.pdf:
49+
bidspm-manual.pdf: update_faq
5050
bash create_manual.sh
5151

5252
boilerplate:

src/reports/bidspm.bib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ @software{bidspm
33
license = {GPL-3.0},
44
title = {bidspm},
55
url = {https://github.com/cpp-lln-lab/bidspm},
6-
version = {3.1.0}
6+
version = {3.1.0dev}
77
doi = {10.5281/zenodo.3554331},
88
publisher = {Zenodo},
99
journal = {Software}

tools/add_links_to_changlog.py renamed to tools/add_links_to_changelog.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from __future__ import annotations
33

44
import re
5-
from pathlib import Path
65

7-
change_log = Path(__file__).parent.parent / "CHANGELOG.md"
6+
from utils import root_dir
7+
8+
change_log = root_dir() / "CHANGELOG.md"
89

910
with open(change_log) as f:
1011
lines = f.readlines()

tools/bump_version.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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()

tools/bump_version.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.

tools/citation_cff_maint.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Update CITATION.cff file."""
2+
3+
from __future__ import annotations
4+
5+
from pathlib import Path
6+
from typing import Any
7+
8+
import ruamel.yaml
9+
10+
from utils import root_dir
11+
12+
yaml = ruamel.yaml.YAML()
13+
yaml.indent(mapping=2, sequence=4, offset=2)
14+
15+
16+
def citation_file() -> Path:
17+
"""Return path to CITATIONS.cff file."""
18+
return root_dir() / "CITATION.cff"
19+
20+
21+
def read_citation_cff() -> dict[str, Any]:
22+
"""Read CITATION.cff file."""
23+
print(f"Reading file: {citation_file()}")
24+
with open(citation_file(), encoding="utf8") as f:
25+
citation = yaml.load(f)
26+
return citation
27+
28+
29+
def write_citation_cff(citation: dict[str, Any]) -> None:
30+
"""Write CITATION.cff file."""
31+
print(f"Writing file: {citation_file()}")
32+
with open(citation_file(), "w", encoding="utf8") as f:
33+
yaml.dump(citation, f)
34+
35+
36+
def sort_authors(authors: list[dict[str, str]]) -> list[dict[str, str]]:
37+
"""Sort authors by given name."""
38+
print(" Sorting authors by given name")
39+
authors.sort(key=lambda x: x["given-names"])
40+
return authors
41+
42+
43+
def main() -> None:
44+
"""Update names.rst and AUTHORS.rst files."""
45+
citation = read_citation_cff()
46+
citation["authors"] = sort_authors(citation["authors"])
47+
write_citation_cff(citation)
48+
49+
50+
if __name__ == "__main__":
51+
main()

tools/list_uncalled.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Print functions that are called less than twice in the code base."""
2+
3+
from pathlib import Path
4+
5+
from rich import print
6+
7+
from utils import root_dir
8+
9+
10+
def find_files_with_extension(directory, extension) -> list[Path]:
11+
"""Recursively find all files in directory with the given extension."""
12+
return list(Path(directory).rglob(f"*{extension}"))
13+
14+
15+
def main() -> None:
16+
src_folder = root_dir() / "src"
17+
files = find_files_with_extension(src_folder, ".m")
18+
19+
for file in files:
20+
function = file.stem
21+
22+
occurrences = []
23+
for other_file in files:
24+
with open(other_file, encoding="utf-8") as f:
25+
content = f.read()
26+
if function in content:
27+
occurrences.append(f"{other_file}:{content.find(function)}")
28+
29+
nb_lines = len(occurrences)
30+
31+
if nb_lines < 2:
32+
print(
33+
"\n---------------------------------------------------------------------"
34+
)
35+
print(function)
36+
print()
37+
for line in occurrences:
38+
print(line)
39+
40+
41+
if __name__ == "__main__":
42+
main()

0 commit comments

Comments
 (0)