Skip to content

Commit d5a9832

Browse files
committed
fix(cmake): harden Path usage
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 3f75ee2 commit d5a9832

10 files changed

+65
-73
lines changed

cmake/scripts/ccwrapper.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
import io
44
import re
5-
import pathlib
5+
from pathlib import Path
66
import subprocess
77
import argparse
88

99
parser = argparse.ArgumentParser()
1010
parser.add_argument(
11-
"--source", "-i", type=pathlib.Path, required=True, help="input file being compiled"
12-
)
13-
parser.add_argument(
14-
"--logdir", "-d", type=pathlib.Path, required=True, help="log directory"
11+
"--source", "-i", type=Path, required=True, help="input file being compiled"
1512
)
13+
parser.add_argument("--logdir", "-d", type=Path, required=True, help="log directory")
1614
parser.add_argument(
1715
"cmd",
1816
action="extend",
@@ -29,7 +27,7 @@
2927
if proc.returncode != 0:
3028
exit(proc.returncode)
3129

32-
with open(shargs.logdir / (shargs.source.name + ".log"), "w") as file:
30+
with open(shargs.logdir / f"{shargs.source.name}.log", "w") as file:
3331
print(" " + str(shargs.source), file=file)
3432
for line in io.StringIO(proc.stderr):
3533
if logline.match(line):

cmake/scripts/cmake_easy_setup.py

+28-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import shutil
66
import tempfile
77
import os
8-
import pathlib
8+
from pathlib import Path
99
import json
1010
from jinja2 import Environment, FileSystemLoader
1111
import difflib
@@ -16,7 +16,7 @@
1616
parser.add_argument(
1717
"--cli",
1818
"-x",
19-
type=pathlib.Path,
19+
type=Path,
2020
required=False,
2121
default=shutil.which("arduino-cli"),
2222
help="path to arduino-cli",
@@ -30,12 +30,12 @@
3030
)
3131
output_args = parser.add_mutually_exclusive_group(required=True)
3232
output_args.add_argument(
33-
"--output", "-o", type=pathlib.Path, help="output file (CMake) with placeholders"
33+
"--output", "-o", type=Path, help="output file (CMake) with placeholders"
3434
)
3535
output_args.add_argument(
3636
"--sketch",
3737
"-s",
38-
type=pathlib.Path,
38+
type=Path,
3939
help="output file (CMake) filled given a sketch folder",
4040
)
4141

@@ -62,6 +62,13 @@
6262
exit(-1)
6363

6464

65+
# Path management
66+
scriptname = Path(__file__).resolve()
67+
corepath = scriptname.parent.parent.parent.resolve()
68+
boards_txt = corepath / "boards.txt"
69+
userhome = Path.home()
70+
71+
6572
def arduino(*args):
6673
# return (out.stdout, logfile)
6774
# raises an exception if the command fails
@@ -76,16 +83,23 @@ def arduino(*args):
7683
return (out, logfile)
7784

7885

86+
def userhome_process(path):
87+
lpath = str(path)
88+
if path.is_relative_to(userhome):
89+
lpath = f"~/{str(path.relative_to(userhome))}"
90+
return lpath
91+
92+
7993
def get_log(fname):
8094
with open(fname) as file:
8195
for line in file:
8296
yield json.loads(line)
8397

8498

85-
def get_boards(boardstxt):
99+
def get_boards():
86100

87-
# we "reject" everything because we don't care about the values, only the keys
88-
families = parse_file(boardstxt, lambda x: True)
101+
# "reject" everything because we don't care about the values, only the keys
102+
families = parse_file(boards_txt, lambda x: True)
89103
del families["menu"]
90104

91105
boards = set()
@@ -97,7 +111,7 @@ def get_boards(boardstxt):
97111

98112
_, logf = arduino("lib", "list")
99113

100-
allboards = get_boards(pathlib.Path(__file__).parent.parent.parent / "boards.txt")
114+
allboards = get_boards()
101115

102116

103117
if shargs.board and shargs.board not in allboards:
@@ -119,11 +133,11 @@ def get_boards(boardstxt):
119133
libpaths = dict()
120134
for line in get_log(logf):
121135
if line["msg"] == "Adding libraries dir":
122-
libpaths[line["location"]] = pathlib.Path(line["dir"])
136+
libpaths[line["location"]] = Path(line["dir"])
123137

124138
# platform lib path is already known, obviously, since that's where this script resides
125139
if "user" in libpaths.keys():
126-
userlibs = pathlib.Path(libpaths["user"])
140+
userlibs = Path(libpaths["user"])
127141
if userlibs.exists():
128142
userlibs = userlibs.resolve()
129143
libs = [u.name for u in userlibs.iterdir() if u.is_dir()]
@@ -137,7 +151,7 @@ def get_boards(boardstxt):
137151
)
138152
libs = list()
139153
else:
140-
userlibs = pathlib.Path.home() / "Arduino/libraries"
154+
userlibs = Path.home() / "Arduino/libraries"
141155
print(
142156
f"""No user library path found through arduino-cli (falling back to {userlibs}).
143157
This has likely to do with your arduino-cli configuration.
@@ -147,22 +161,13 @@ def get_boards(boardstxt):
147161
)
148162
libs = list()
149163

150-
corepath = pathlib.Path(__file__).parent.parent.parent.resolve()
151-
152164
j2_env = Environment(
153165
loader=FileSystemLoader(str(corepath / "cmake" / "templates")),
154166
trim_blocks=True,
155167
lstrip_blocks=True,
156168
)
157169
cmake_template = j2_env.get_template("easy_cmake.cmake")
158170

159-
160-
userhome = pathlib.Path.home()
161-
if userlibs.is_relative_to(userhome):
162-
userlibs = "~/" + str(userlibs.relative_to(userhome))
163-
if corepath.is_relative_to(userhome):
164-
corepath = "~/" + str(corepath.relative_to(userhome))
165-
166171
if shargs.sketch:
167172
SOURCEFILE_EXTS = (".c", ".cpp", ".S", ".ino")
168173
sources = {
@@ -180,19 +185,15 @@ def get_boards(boardstxt):
180185
tgtname = ""
181186
sources = set()
182187

183-
scriptname = pathlib.Path(__file__)
184-
if scriptname.is_relative_to(userhome):
185-
scriptname = "~/" + str(scriptname.relative_to(userhome))
186-
187188
with open(shargs.output or shargs.sketch / "CMakeLists.txt", "w") as out:
188189
out.write(
189190
cmake_template.render(
190-
corepath=str(corepath).replace(
191+
corepath=userhome_process(corepath).replace(
191192
"\\", "\\\\"
192193
), # escape backslashes for CMake
193-
userlibs=str(userlibs).replace("\\", "\\\\"),
194+
userlibs=userhome_process(userlibs).replace("\\", "\\\\"),
194195
libs=libs,
195-
scriptfile=scriptname,
196+
scriptfile=userhome_process(scriptname),
196197
tgtname=tgtname,
197198
scrcfiles=sources,
198199
boardname=shargs.board,

cmake/scripts/cmake_gen.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22

3+
from pathlib import Path
4+
35
SOURCEFILE_EXTS = (
46
".c",
57
".cpp",
@@ -20,7 +22,7 @@ def get_default_config():
2022
)
2123

2224

23-
def parse_configfile(file):
25+
def parse_configfile(file: Path):
2426
rawcfg = dict()
2527

2628
for line in open(file):
@@ -42,7 +44,7 @@ def parse_configfile(file):
4244
return cfg
4345

4446

45-
def get_sources(dir, recursive=False, relative_to=None):
47+
def get_sources(dir: Path, recursive: bool = False, relative_to: Path = None):
4648
if relative_to is None:
4749
relative_to = dir
4850
if recursive:
@@ -57,12 +59,12 @@ def get_sources(dir, recursive=False, relative_to=None):
5759
}
5860

5961

60-
def render(dir, template, config):
62+
def render(dir: Path, template, config):
6163
with open(dir / "CMakeLists.txt", "w") as outfile:
6264
outfile.write(template.render(**config))
6365

6466

65-
def get_static_libs(dir):
67+
def get_static_libs(dir: Path):
6668
result = dict()
6769
cpu = ""
6870
fpconf = "-" # format: f"{fpu}-{float_abi}"; this makes "-" by default
@@ -80,7 +82,7 @@ def get_static_libs(dir):
8082
return result
8183

8284

83-
def config_for_bareflat(dir, force_recurse=False):
85+
def config_for_bareflat(dir: Path, force_recurse: bool = False):
8486
# no library.properties
8587
config = get_default_config()
8688

@@ -98,7 +100,7 @@ def config_for_bareflat(dir, force_recurse=False):
98100
return config
99101

100102

101-
def config_for_modern(dir):
103+
def config_for_modern(dir: Path):
102104
# library.properties present, src/ present
103105
config = get_default_config()
104106
config.update(parse_configfile(dir / "library.properties"))
@@ -111,7 +113,7 @@ def config_for_modern(dir):
111113
return config
112114

113115

114-
def autoconfig(libdir):
116+
def autoconfig(libdir: Path):
115117
conf_file = libdir / "library.properties"
116118
srcdir = libdir / "src"
117119
if (

cmake/scripts/cmake_updater_hook.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
import argparse
1010
import subprocess
11-
import pathlib
11+
from pathlib import Path
1212
import sys
1313

1414
parser = argparse.ArgumentParser(
1515
usage="updater hook for CMake files. Fully automatic, takes no argument."
1616
)
1717
shargs = parser.parse_args()
1818

19-
script_dir = pathlib.Path(__file__).parent # Arduino_Core_STM32/cmake/scripts
19+
script_dir = Path(__file__).parent.resolve() # Arduino_Core_STM32/cmake/scripts
2020
base_dir = script_dir.parent.parent # Arduino_Core_STM32
2121
templates_dir = base_dir / "cmake" / "templates"
2222

cmake/scripts/generate_header.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
#!/usr/bin/env python3
22

33
import argparse
4-
import pathlib
4+
from pathlib import Path
55

66
parser = argparse.ArgumentParser()
7-
parser.add_argument(
8-
"--source", "-i", type=pathlib.Path, required=True, help="ctags's output"
9-
)
10-
parser.add_argument(
11-
"--out", "-o", type=pathlib.Path, required=True, help="header to generate"
12-
)
7+
parser.add_argument("--source", "-i", type=Path, required=True, help="ctags's output")
8+
parser.add_argument("--out", "-o", type=Path, required=True, help="header to generate")
139

1410
shargs = parser.parse_args()
1511

cmake/scripts/includes.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
#!/usr/bin/env python3
22

3-
import pathlib
3+
from pathlib import Path
44
import argparse
55

66
import graphviz
77

88
parser = argparse.ArgumentParser()
9-
parser.add_argument("-o", type=pathlib.Path, help="file to write the full graph to")
10-
parser.add_argument(
11-
"-t", type=pathlib.Path, help="file to write the transitive graph to"
12-
)
9+
parser.add_argument("-o", type=Path, help="file to write the full graph to")
10+
parser.add_argument("-t", type=Path, help="file to write the transitive graph to")
1311
parser.add_argument(
1412
"logs",
15-
type=pathlib.Path,
13+
type=Path,
1614
nargs="*",
1715
action="extend",
1816
help="list of log files to parse",
@@ -39,7 +37,7 @@ def parse_output(log):
3937
for line in log:
4038
d, h = line.rstrip().split(" ", 1)
4139
d = d.count(".")
42-
h = pathlib.Path(h)
40+
h = Path(h)
4341

4442
if d == 0:
4543
rootcause = h

cmake/scripts/sizereport.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
import pathlib
3+
from pathlib import Path
44
import re
55
import subprocess
66
import argparse
@@ -9,14 +9,14 @@
99
parser.add_argument(
1010
"-x",
1111
"--tool",
12-
type=pathlib.Path,
12+
type=Path,
1313
required=True,
1414
help="path to `arm-none-eabi-size`",
1515
)
1616
parser.add_argument(
1717
"-f",
1818
"--binfile",
19-
type=pathlib.Path,
19+
type=Path,
2020
required=True,
2121
help="path to the binary to analyze",
2222
)

cmake/scripts/syms.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
#!/usr/bin/env python3
22

3-
import pathlib
3+
from pathlib import Path
44
import argparse
55

66
import graphviz
77

88
parser = argparse.ArgumentParser()
99
parser.add_argument(
10-
"-m", "--mapfile", type=pathlib.Path, required=True, help="path to ld's map file"
10+
"-m", "--mapfile", type=Path, required=True, help="path to ld's map file"
1111
)
12+
parser.add_argument("-f", "--fullgv", type=Path, help="file to write the full graph to")
1213
parser.add_argument(
13-
"-f", "--fullgv", type=pathlib.Path, help="file to write the full graph to"
14-
)
15-
parser.add_argument(
16-
"-s", "--summarygv", type=pathlib.Path, help="file to write the summarized graph to"
14+
"-s", "--summarygv", type=Path, help="file to write the summarized graph to"
1715
)
1816

1917
shargs = parser.parse_args()

0 commit comments

Comments
 (0)