14
14
You can also use `--dry-run` to see what would happen without actually changing anything.
15
15
"""
16
16
17
+ from __future__ import annotations
18
+
17
19
import argparse
18
20
import os
19
21
import shutil
20
22
import tempfile
21
- from typing import Set
22
23
23
- from git import Repo
24
+ from git import Repo # pip install GitPython
24
25
25
26
OWNER = "rerun-io"
26
27
27
- # Files requires by C++, but not by both Python or Rust.
28
+ # Don't overwrite these when updating existing repository from the template
29
+ DO_NOT_OVERWRITE = {
30
+ "Cargo.lock" ,
31
+ "CHANGELOG.md" ,
32
+ "main.py" ,
33
+ "pixi.lock" ,
34
+ "README.md" ,
35
+ "requirements.txt" ,
36
+ }
37
+
38
+ # Files required by C++, but not by _both_ Python and Rust
28
39
CPP_FILES = {
29
40
".clang-format" ,
30
41
".github/workflows/cpp.yml" ,
31
42
"CMakeLists.txt" ,
32
- "pixi.lock" , # Not needed by Rust
33
- "pixi.toml" , # Not needed by Rust
34
- "src/main.cpp" ,
43
+ "pixi.lock" , # Pixi is only C++ & Python - For Rust we only use cargo
44
+ "pixi.toml" , # Pixi is only C++ & Python - For Rust we only use cargo
35
45
"src/" ,
46
+ "src/main.cpp" ,
36
47
}
37
48
38
- # Files requires by Python, but not by both C++ or Rust
49
+ # Files required by Python, but not by _both_ C++ and Rust
39
50
PYTHON_FILES = {
40
51
".github/workflows/python.yml" ,
41
52
".mypy.ini" ,
42
- "pixi.lock" , # Not needed by Rust
43
- "pixi.toml" , # Not needed by Rust
53
+ "main.py" ,
54
+ "pixi.lock" , # Pixi is only C++ & Python - For Rust we only use cargo
55
+ "pixi.toml" , # Pixi is only C++ & Python - For Rust we only use cargo
44
56
"pyproject.toml" ,
57
+ "requirements.txt" ,
45
58
}
46
59
47
- # Files requires by Rust, but not by both C++ or Python
60
+ # Files required by Rust, but not by _both_ C++ and Python
48
61
RUST_FILES = {
62
+ ".github/workflows/cargo_shear.yml" ,
49
63
".github/workflows/rust.yml" ,
50
64
"bacon.toml" ,
51
65
"Cargo.lock" ,
52
66
"Cargo.toml" ,
67
+ "CHANGELOG.md" , # We only keep a changelog for Rust crates at the moment
53
68
"clippy.toml" ,
54
69
"Cranky.toml" ,
55
70
"deny.toml" ,
71
+ "RELEASES.md" ,
56
72
"rust-toolchain" ,
57
73
"scripts/clippy_wasm/" ,
58
74
"scripts/clippy_wasm/clippy.toml" ,
75
+ "scripts/generate_changelog.py" , # We only keep a changelog for Rust crates at the moment
76
+ "src/" ,
59
77
"src/lib.rs" ,
60
78
"src/main.rs" ,
61
- "src/" ,
62
79
}
63
80
81
+ # Files we used to have, but have been removed in never version of rerun_template
82
+ DEAD_FILES = ["bacon.toml" , "Cranky.toml" ]
83
+
64
84
65
- def parse_languages (lang_str : str ) -> Set [str ]:
85
+ def parse_languages (lang_str : str ) -> set [str ]:
66
86
languages = lang_str .split ("," ) if lang_str else []
67
87
for lang in languages :
68
88
assert lang in ["cpp" , "python" , "rust" ], f"Unsupported language: { lang } "
69
89
return set (languages )
70
90
71
91
72
- def calc_deny_set (languages : Set [str ]) -> Set [str ]:
92
+ def calc_deny_set (languages : set [str ]) -> set [str ]:
73
93
"""The set of files to delete/ignore."""
74
94
files_to_delete = CPP_FILES | PYTHON_FILES | RUST_FILES
75
95
if "cpp" in languages :
@@ -81,29 +101,41 @@ def calc_deny_set(languages: Set[str]) -> Set[str]:
81
101
return files_to_delete
82
102
83
103
84
- def init (languages : Set [str ], dry_run : bool ) -> None :
104
+ def init (languages : set [str ], dry_run : bool ) -> None :
85
105
print ("Removing all language-specific files not needed for languages {languages}." )
86
106
files_to_delete = calc_deny_set (languages )
87
107
delete_files_and_folder (files_to_delete , dry_run )
88
108
89
109
90
- def delete_files_and_folder (paths : Set [str ], dry_run : bool ) -> None :
110
+ def remove_file (filepath : str ) -> None :
111
+ try :
112
+ os .remove (filepath )
113
+ except FileNotFoundError :
114
+ pass
115
+
116
+
117
+ def delete_files_and_folder (paths : set [str ], dry_run : bool ) -> None :
91
118
repo_path = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
92
119
for path in paths :
93
120
full_path = os .path .join (repo_path , path )
94
121
if os .path .exists (full_path ):
95
122
if os .path .isfile (full_path ):
96
123
print (f"Removing file { full_path } …" )
97
124
if not dry_run :
98
- os . remove (full_path )
125
+ remove_file (full_path )
99
126
elif os .path .isdir (full_path ):
100
127
print (f"Removing folder { full_path } …" )
101
128
if not dry_run :
102
129
shutil .rmtree (full_path )
103
130
104
131
105
- def update (languages : Set [str ], dry_run : bool ) -> None :
106
- files_to_ignore = calc_deny_set (languages )
132
+ def update (languages : set [str ], dry_run : bool ) -> None :
133
+ for file in DEAD_FILES :
134
+ print (f"Removing dead file { file } …" )
135
+ if not dry_run :
136
+ remove_file (file )
137
+
138
+ files_to_ignore = calc_deny_set (languages ) | DO_NOT_OVERWRITE
107
139
repo_path = os .path .dirname (os .path .dirname (os .path .realpath (__file__ )))
108
140
109
141
with tempfile .TemporaryDirectory () as temp_dir :
@@ -117,17 +149,15 @@ def update(languages: Set[str], dry_run: bool) -> None:
117
149
continue
118
150
if rel_path .startswith ("src/" ):
119
151
continue
120
-
121
- if rel_path in {"README.md" , "pixi.lock" , "Cargo.lock" }:
152
+ if rel_path in files_to_ignore :
122
153
continue
123
154
124
- if rel_path not in files_to_ignore :
125
- dest_path = os .path .join (repo_path , rel_path )
155
+ dest_path = os .path .join (repo_path , rel_path )
126
156
127
- print (f"Updating { rel_path } …" )
128
- if not dry_run :
129
- os .makedirs (os .path .dirname (dest_path ), exist_ok = True )
130
- shutil .copy2 (src_path , dest_path )
157
+ print (f"Updating { rel_path } …" )
158
+ if not dry_run :
159
+ os .makedirs (os .path .dirname (dest_path ), exist_ok = True )
160
+ shutil .copy2 (src_path , dest_path )
131
161
132
162
133
163
def main () -> None :
0 commit comments