-
Notifications
You must be signed in to change notification settings - Fork 17
/
update.py
100 lines (82 loc) · 4.02 KB
/
update.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Script is tested on OS X 10.12
# YOUR MILEAGE MAY VARY
import urllib.request
import zipfile
import shutil
import logging
import os, re, sys
import subprocess
from pathlib import Path
from multiprocessing.pool import ThreadPool
from socket import timeout
stm32_families = [
"l0", "l1", "l4", "l5",
"f0", "f1", "f2", "f3", "f4", "f7",
"c0",
"g0", "g4",
"h5", "h7",
"wb", "wba", "wl",
"u0", "u5",
]
def get_header_version(release_notes):
vmatch = re.search(r">V([0-9]+\.[0-9]+\.[0-9]+)", release_notes)
return vmatch.group(1) if vmatch else None
def get_header_date(release_notes):
vmatch = re.search(r">V.+?/.*?(\d{2}-[A-Z][a-z]+?-20\d{2}).*?<", release_notes, flags=re.DOTALL | re.MULTILINE)
return vmatch.group(1) if vmatch else None
logging.basicConfig(level=logging.DEBUG if "-vv" in sys.argv else logging.INFO)
def get_header_files(family):
LOGGER = logging.getLogger(family.upper())
remote_path = Path(f"raw/STM32{family.upper()}xx").absolute()
repo_url = f"https://github.com/STMicroelectronics/cmsis_device_{family.lower()}.git"
subprocess.run(f"git clone --depth=1 {repo_url} {remote_path}", shell=True)
remote_readme = (remote_path / "Release_Notes.html")
remote_readme_content = remote_readme.read_text(errors="replace")
header_remote_version = get_header_version(remote_readme_content)
header_remote_date = get_header_date(remote_readme_content)
destination_path = Path("stm32{}xx".format(family))
header_local_version = (destination_path / "Release_Notes.html")
if header_local_version.exists():
header_local_version = get_header_version(header_local_version.read_text(errors="replace"))
else:
header_local_version = None
LOGGER.info("Header v{} -> v{}".format(header_local_version, header_remote_version))
if header_local_version == header_remote_version:
LOGGER.info("Skipping update...")
return (header_remote_version, header_remote_date)
shutil.rmtree(destination_path, ignore_errors=True)
destination_path.mkdir(parents=True)
shutil.copy(remote_readme, destination_path / "Release_Notes.html")
for path in remote_path.glob("Include/*.h"):
if not path.is_file(): continue
dest = destination_path / path.relative_to(remote_path)
dest.parent.mkdir(parents=True, exist_ok=True)
# Copy, normalize newline and remove trailing whitespace
with path.open("r", newline=None, encoding="utf-8", errors="ignore") as rfile, \
dest.open("w", encoding="utf-8") as wfile:
wfile.writelines(l.rstrip()+"\n" for l in rfile.readlines())
for patch in Path('patches').glob("{}*.patch".format(family)):
LOGGER.info("Applying {}...".format(patch))
if os.system("git apply -v --ignore-whitespace {}".format(patch)) != 0:
LOGGER.critical("Applying {} FAILED...".format(patch))
return None
LOGGER.info("Successful update")
return (header_remote_version, header_remote_date)
shutil.rmtree("raw", ignore_errors=True)
Path("raw").mkdir()
with ThreadPool(len(stm32_families)) as pool:
family_versions = pool.map(get_header_files, stm32_families)
# family_versions = [get_header_files(f) for f in stm32_families]
def update_readme(readme, family, new_version, new_date):
match = r"{0}: v.+? created .+?]".format(family.upper())
replace = "{0}: v{1} created {2}]".format(family.upper(), new_version, new_date)
return re.sub(match, replace, readme)
for family, versions in zip(stm32_families, family_versions):
if versions is None or versions[0] is None: continue;
readme = Path("README.md").read_text()
readme = update_readme(readme, family, versions[0], versions[1])
Path("README.md").write_text(readme)
subprocess.run("git add README.md stm32{}xx".format(family), shell=True)
if subprocess.call("git diff-index --quiet HEAD --", shell=True):
subprocess.run('git commit -m "Update STM32{} headers to v{}"'.format(family.upper(), versions[0]), shell=True)
exit(family_versions.count(None))