Skip to content

Commit c18e9f7

Browse files
committed
feat(commands/manage_autouid): rework the generation of MID/HASH (code review)
1 parent c0742fb commit c18e9f7

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

strictdoc/commands/manage_autouid_command.py

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from typing import Dict
2+
from typing import Dict, Optional
33

44
from strictdoc.backend.sdoc.errors.document_tree_error import DocumentTreeError
55
from strictdoc.backend.sdoc.models.node import SDocNode
@@ -132,8 +132,8 @@ def _rewrite_source_file(
132132
) -> None:
133133
"""
134134
NOTE: This only updates the source code with the new calculated value.
135-
All links in the graph database and links in the search index are
136-
not modified for now.
135+
All links in the graph database and links in the search index
136+
ARE NOT! modified for now.
137137
"""
138138

139139
assert trace_info.source_file is not None
@@ -149,6 +149,18 @@ def _rewrite_source_file(
149149
with open(trace_info.source_file.full_path, "rb") as source_file_:
150150
file_bytes = source_file_.read()
151151

152+
field_remapped_mid = "MID"
153+
154+
relevant_source_node_config = (
155+
project_config.get_relevant_source_nodes_entry(
156+
trace_info.source_file.in_doctree_source_file_rel_path_posix
157+
)
158+
)
159+
if relevant_source_node_config is not None:
160+
field_remapped_mid = (
161+
relevant_source_node_config.sdoc_to_source_map.get("MID", "MID")
162+
)
163+
152164
file_rewrites = {}
153165
for source_node_ in trace_info.source_nodes:
154166
function = source_node_.function
@@ -160,14 +172,14 @@ def _rewrite_source_file(
160172
if source_node_.comment_byte_range is None:
161173
continue
162174

163-
if "SPDX-Req-ID" not in source_node_.fields:
175+
if field_remapped_mid not in source_node_.fields:
164176
continue
165177

166-
node_rewrites: Dict[str, str] = {}
178+
node_rewrites: Dict[str, bytes] = {}
167179

168-
# If the source node has the SPDX-REQ-ID but it is not yet a valid
169-
# SHA256 identifier, create one and patch the node.
170-
existing_req_id = source_node_.fields["SPDX-Req-ID"]
180+
# If the source node has the MID (SPDX-REQ-ID), but it is not yet a
181+
# valid SHA256 identifier, create one and patch the node.
182+
existing_req_id = source_node_.fields[field_remapped_mid]
171183
if not is_sha256(existing_req_id):
172184
hash_spdx_id_str = get_random_sha256()
173185
hash_spdx_id = bytes(hash_spdx_id_str, encoding="utf8")
@@ -178,17 +190,37 @@ def _rewrite_source_file(
178190
form_field_index=0,
179191
value=hash_spdx_id_str,
180192
)
181-
node_rewrites["SPDX-Req-ID"] = hash_spdx_id_str
193+
node_rewrites[field_remapped_mid] = hash_spdx_id
194+
195+
patched_node = MarkerWriter().write(
196+
source_node_,
197+
rewrites=node_rewrites,
198+
comment_file_bytes=file_bytes[
199+
source_node_.comment_byte_range.start : source_node_.comment_byte_range.end
200+
],
201+
)
202+
file_rewrites[source_node_] = patched_node
182203

183-
existing_req_hash = source_node_.fields.get("SPDX-REQ-HKey", None)
184-
if existing_req_hash is not None and existing_req_hash != "TBD":
185-
# FIXME: Shall we assume that a HKey can only be TBD, a
186-
# valid SHA256, or simply be missing?
187-
assert is_sha256(existing_req_hash), existing_req_hash
188-
else:
189-
assert source_node_.sdoc_node is not None
190-
sdoc_node: SDocNode = source_node_.sdoc_node
204+
# If a source node has no sidecar SDoc node attached, there is
205+
# nothing else to do.
206+
if source_node_.sdoc_node is None:
207+
continue
191208

209+
#
210+
# The following is only applicable to the Linux Kernel Requirements
211+
# Template proposal:
212+
#
213+
# Generate HASH field if it is not present. The HASH field is only
214+
# generated for SDoc nodes, the source code nodes are not modified.
215+
#
216+
sdoc_node: SDocNode = source_node_.sdoc_node
217+
218+
existing_req_hash: Optional[str] = None
219+
if "HASH" in sdoc_node.ordered_fields_lookup:
220+
hash_field = sdoc_node.get_field_by_name("HASH")
221+
existing_req_hash = hash_field.get_text_value()
222+
223+
if existing_req_hash is None or not is_sha256(existing_req_hash):
192224
# FILE_PATH: The file the code resides in, relative to the root of the project repository.
193225
file_path = bytes(
194226
trace_info.source_file.in_doctree_source_file_rel_path_posix,
@@ -201,19 +233,14 @@ def _rewrite_source_file(
201233
field_name_,
202234
field_values_,
203235
) in sdoc_node.ordered_fields_lookup.items():
204-
if field_name_ in (
205-
"MID",
206-
"HASH",
207-
"SPDX-Req-ID",
208-
"SPDX-Req-HKey",
209-
):
236+
if field_name_ in ("MID", "HASH"):
210237
continue
211238
for field_value_ in field_values_:
212239
instance_bytes += bytes(
213240
field_value_.get_text_value(), encoding="utf8"
214241
)
215242

216-
# CODE: The code that the SPDX-Req applies to.
243+
# CODE: The code that the node hash applies to.
217244
code = file_bytes[
218245
function.code_byte_range.start : function.code_byte_range.end
219246
]
@@ -237,19 +264,6 @@ def _rewrite_source_file(
237264
value=hash_spdx_hash_str,
238265
)
239266

240-
if len(node_rewrites) > 0:
241-
patched_node = MarkerWriter().write(
242-
source_node_,
243-
rewrites={
244-
"SPDX-Req-ID": hash_spdx_id,
245-
"SPDX-Req-HKey": hash_spdx_hash,
246-
},
247-
comment_file_bytes=file_bytes[
248-
source_node_.comment_byte_range.start : source_node_.comment_byte_range.end
249-
],
250-
)
251-
file_rewrites[source_node_] = patched_node
252-
253267
source_writer = SourceWriter()
254268
output_string = source_writer.write(
255269
trace_info, rewrites=file_rewrites, file_bytes=file_bytes

0 commit comments

Comments
 (0)