forked from CrossTheRoadElec/Phoenix6-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_template.py
78 lines (63 loc) · 2.74 KB
/
update_template.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright (C) Cross The Road Electronics. All rights reserved.
# License information can be found in CTRE_LICENSE.txt
# For support and suggestions contact [email protected] or file
# an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
import os
import re
import shutil
from numpy import empty
# Mapping of templates to target directories
TEMPLATE_DIRS: dict[str, str] = {
"./.automation/java_template/" : "./java",
"./.automation/cpp_template/" : "./cpp",
"./.automation/python_template/": "./python"
}
# List of globs to ignore for deletion
DEL_EXCLUSIONS: list = [
r'src(?:\/|\\)',
r'deploy(?:\/|\\)',
r'.*.py$',
r'tuner-project.json',
r'(?i)vendordeps(?:\/|\\)(?!Phoenix6|WPILibNewCommands)',
]
# Copy all of the files in the template directory into
# the corresponding example directory
def update_templates(template_dirs, del_exclusions):
# iterate over templates and globs
for template_dir, target_seek_dir in TEMPLATE_DIRS.items():
for root, dirs, files in os.walk(target_seek_dir):
# Delete all files that aren't regexed
for file in files:
full_path = os.path.join(root, file)
to_keep = False
# Check to see if we should keep the file
for glob in DEL_EXCLUSIONS:
regex_search = re.search(glob, full_path)
if not to_keep:
to_keep = regex_search is not None
if not to_keep:
print("Removing", full_path)
try:
os.remove(full_path)
except:
print("WARNING: Failed to remove", full_path)
for dir in dirs:
full_dir_path = os.path.join(root, dir)
if len(os.listdir(full_dir_path)) == 0:
print("Removing", full_dir_path)
os.removedirs(full_dir_path)
# Iterate over the examples, copying the files as necessary
for dir in os.listdir(target_seek_dir):
target_dir = os.path.join(target_seek_dir, dir)
for src_path in os.listdir(template_dir):
source_path = os.path.join(template_dir, src_path)
target_path = os.path.join(target_dir, src_path)
# Copy directories
try:
print("Copying source", source_path, "to", target_path)
shutil.copytree(source_path, target_path, dirs_exist_ok=True)
except:
# Copy individual files
shutil.copy(source_path, target_path)
if __name__ == "__main__":
update_templates(TEMPLATE_DIRS, DEL_EXCLUSIONS)