-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapply_patch.py
More file actions
65 lines (48 loc) · 2.14 KB
/
apply_patch.py
File metadata and controls
65 lines (48 loc) · 2.14 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
"""
This script generate a patched Slicer repository
The Slicer origin and ref are hardcoded as the patches are specific for a given Slicer version.
Usage:
python apply_patch.py
pip wheel Slicer --extra-index-url https://vtk.org/files/wheel-sdks
"""
import os
from pathlib import Path
import shutil
from common import execute_process, mkpath, GIT_URL, GIT_REVISION, SLICER_DIR, PATCH_DIR
def is_patch(name: Path) -> bool:
return name.as_posix().endswith((".patch"))
def apply_patch(patch_dir: Path, dest_dir: Path, file: Path):
"""file is expected to be a relative path, it will be looked in both patch_dir and dest_dir"""
if is_patch(file):
print(f"Applying patch {file}")
execute_process(f"git apply {(patch_dir / file).resolve().as_posix()}", dest_dir)
else:
src = f"{patch_dir}/{file}"
dst = f"{dest_dir}/{file}"
print(f"Copying file from {src} to {dst}")
mkpath(dst)
shutil.copy2(src, dst)
def main() -> None:
if not os.path.exists(PATCH_DIR):
print(f"No patch found. Patches must be stored in {PATCH_DIR} directory!")
exit(1)
# Always start from a clean directory
if os.path.exists(SLICER_DIR):
print(f"{SLICER_DIR} repository will be removed.")
if input("Are you sure you want to continue? (Y/N)").lower() in ["n", "no", ""]:
exit(0)
shutil.rmtree(SLICER_DIR)
print(f"Cloning Slicer from {GIT_URL} at revision {GIT_REVISION} in the {SLICER_DIR} folder...")
execute_process(f"git clone {GIT_URL} {SLICER_DIR}")
execute_process(f"git checkout {GIT_REVISION}", SLICER_DIR)
for file in Path(PATCH_DIR).rglob("*"):
if not os.path.isfile(file):
continue
apply_patch(PATCH_DIR, SLICER_DIR, Path(*file.parts[1:])) # remove "PATCH_DIR/" part
# Commit the patches
execute_process("git add --all", SLICER_DIR)
execute_process('git config user.email "kitware@kitware.fr"', SLICER_DIR)
execute_process('git config user.name "Kitware SAS"', SLICER_DIR)
execute_process('git commit -m"AUTO: Generated by SlicerCore scripts"', SLICER_DIR)
if __name__ == '__main__':
main()