forked from rene-d/wifinfo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkarduinosrc.py
executable file
·138 lines (103 loc) · 3.32 KB
/
mkarduinosrc.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# module téléinformation client
# rene-d 2020
from pathlib import Path
import re
import shutil
import tempfile
import zipfile
import click
class Amalgamation:
"""
Concatenate all source files into one.
"""
def __init__(self, src: Path):
self.src = src
self.lines = []
self.included = set()
# self.system_headers = set()
self.lines.append(f"//amalgamation: {src} *.ino *.cpp\n")
for file in src.glob("*.ino"):
self._add(file)
f = Path(src / "main.cpp")
if f.is_file():
self._add(f)
for file in src.glob("*.cpp"):
self._add(file)
def _add(self, file: Path):
rel = file.relative_to(self.src)
if rel in self.included:
return
self.included.add(rel)
self.lines.append("\n")
self.lines.append(f"//begin: {rel}\n")
for line in file.open():
if line.lstrip().startswith("#pragma once"):
line = "// " + line
elif line.lstrip().startswith("#include"):
m = re.search(r'#include "(.+)"', line)
if m:
h = file.parent / m.group(1)
line = "// " + line
if h.relative_to(self.src) not in self.included:
self.lines.append(line)
self._add(h)
continue
else:
m = re.search(r"#include <(.+)>", line)
if m:
h = m.group(1)
# if h in self.system_headers:
# line = "// " + line
# else:
# self.system_headers.add(h)
else:
print("error:", line)
exit(2)
self.lines.append(line)
self.lines.append(f"//end: {rel}\n")
@click.command()
@click.argument("target")
@click.option("-c", "--copy", is_flag=True, default=False)
@click.option("-1", "--one", is_flag=True, default=False)
def cmd_arduino_ide(target: str, copy: bool, one: bool) -> None:
"""
Crée une archive pour utilisation avec l'IDE Arduino.
"""
target = Path(target)
root = Path("wifinfo")
src = Path("src")
data = Path("data")
if copy:
def cp(src: Path, dst: Path) -> None:
dst = target / dst
dst.parent.mkdir(exist_ok=True, parents=True)
shutil.copy2(src, dst)
op = cp
else:
if target.suffix == "":
target = target.with_suffix(".zip")
z = zipfile.ZipFile(target, "w")
op = z.write
if one:
ino = tempfile.NamedTemporaryFile("w+")
ino.writelines(Amalgamation(src).lines)
ino.flush()
d = root / "wifinfo.ino"
print("*", "->", d)
op(ino.name, d)
else:
# ajout de src/* => wifinfo/*
for f in src.glob("*"):
if f.is_file():
d = root / f.relative_to(src)
print(f, "->", d)
op(f, d)
# ajout de data/* => wifinfo/data/*
for f in data.rglob("*"):
if f.is_file():
d = root / f
print(f, "->", d)
op(f, d)
if __name__ == "__main__":
cmd_arduino_ide()