Repository navigation
Expand file tree
/
Copy pathtasks.py
More file actions
121 lines (96 loc) · 3.91 KB
/
Copy pathtasks.py
File metadata and controls
121 lines (96 loc) · 3.91 KB
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
# SPDX-FileCopyrightText: 2025 Eaton Corporation
# SPDX-License-Identifier: MIT
"""Disparate tasks to support memtab development and testing."""
import os
from pathlib import Path
from invoke import task
from invoke.context import Context
@task(aliases=["v"])
def get_version(c: Context) -> None:
import toml
with open("pyproject.toml", "r") as f:
data = toml.load(f)
print(data["project"]["version"])
@task(aliases=["m"])
def map_regex_tester(c: Context) -> None:
"""Test out regexes for the map file parser."""
import re
map_file = Path("map_lines.txt")
with open(map_file, "r") as f:
content = f.read()
section_regex = re.compile(r"\n(?P<section>[a-zA-Z._]+)(\s+|\n)0x(?P<offset>[0-9a-f]+)\s+0x(?P<size>[0-9a-f/]+)(?P<comment>.*)")
for match in section_regex.finditer(content):
section = match.group("section").strip()
offset = int(match.group("offset"), 16)
size = int(match.group("size"), 16)
comment = match.group("comment").strip()
if offset and size:
print(f"Region: {section}, Start: 0x{offset:08x}, Size: {size}, Flags: {comment}")
subsection_regex = re.compile(
r"\n\s.(?P<name>[0-9a-zA-Z._]+)(\s+|\n)0x(?P<offset>[0-9a-f]+)\s+0x(?P<size>[0-9a-f]+)(\s+|\n)(?P<path>[a-zA-Z:\\\/_.\-0-9]+)\(*(?P<file>[a-zA-Z._0-9\-]*)\)*"
)
for match in subsection_regex.finditer(content):
name = match.group("name").strip()
offset = int(match.group("offset"), 16)
size = int(match.group("size"), 16)
path = match.group("path").strip()
file = match.group("file").strip()
words = name.split(".")
section = words[0]
if len(words) > 2:
symbol_name = words[2]
elif len(words) == 2:
symbol_name = words[1]
else:
symbol_name = file.replace(".o", "")
if not file:
object_file = path
else:
object_file = file
if offset and size:
print(f"section: {section}, symbol: {symbol_name}, addr: 0x{offset:08x}, size: {size}, object_file: {object_file}")
@task(aliases=["mtr"])
def metrics(c: Context) -> None:
"""Gather basic code metrics, such as lines of code, complexity, etc.
Uses the radon package, as well as a coverage.xml file if it exists."""
import json
from radon.cli import Config
from radon.cli.harvest import CCHarvester, HCHarvester, MIHarvester, RawHarvester
from radon.complexity import SCORE
from rich.pretty import pprint
root_dir = os.path.dirname(os.path.abspath(__file__))
src_dirs = [os.path.join(root_dir, "src/memtab/")]
radon_config = Config(
exclude="",
ignore="",
order=SCORE,
no_assert=False,
show_closures=True,
min="A",
max="F",
multi=True,
by_function=True,
)
cc = CCHarvester(src_dirs, config=radon_config)
raw = RawHarvester(src_dirs, config=radon_config)
mi = MIHarvester(src_dirs, config=radon_config)
hc = HCHarvester(src_dirs, config=radon_config)
raw_json = json.loads(raw.as_json())
cc_json = json.loads(cc.as_json())
mi_json = json.loads(mi.as_json())
hc_json = json.loads(hc.as_json())
assert len(raw_json) == len(mi_json) == len(hc_json), f"The JSON outputs from radon do not match in length: {len(raw_json)}, {len(mi_json)}, {len(hc_json)}"
assert len(cc_json) <= len(raw_json), f"The Cyclomatic Complexity JSON outputs from radon do not match in length: {len(cc_json)}, {len(raw_json)}"
overall = {}
for filename in raw_json:
overall[filename] = {
"raw": raw_json[filename],
"cc": cc_json.get(filename, {}),
"mi": mi_json[filename],
"hc": hc_json[filename],
}
pprint(overall)
with open("metrics.json", "w") as stream:
json.dump(overall, stream, indent=4)
if os.path.exists("coverage.xml"):
pass