Deduplicate inline proxy allow list
This commit is contained in:
parent
32c2a267e9
commit
4a8f743d02
1 changed files with 84 additions and 23 deletions
|
|
@ -482,45 +482,106 @@ while i < len(lines):
|
||||||
stripped = line.lstrip()
|
stripped = line.lstrip()
|
||||||
if stripped.startswith("allowed_nodes:"):
|
if stripped.startswith("allowed_nodes:"):
|
||||||
start = i
|
start = i
|
||||||
i += 1
|
entries = []
|
||||||
while i < len(lines):
|
j = i + 1
|
||||||
nxt = lines[i]
|
while j < len(lines):
|
||||||
|
nxt = lines[j]
|
||||||
nxt_stripped = nxt.lstrip()
|
nxt_stripped = nxt.lstrip()
|
||||||
|
if nxt_stripped.startswith("-"):
|
||||||
|
entries.append(nxt_stripped[1:].strip())
|
||||||
|
j += 1
|
||||||
|
continue
|
||||||
if (
|
if (
|
||||||
nxt_stripped.startswith("-")
|
nxt_stripped.startswith("#")
|
||||||
or nxt_stripped.startswith("#")
|
|
||||||
or nxt_stripped == ""
|
or nxt_stripped == ""
|
||||||
or nxt.startswith((" ", "\t"))
|
or nxt.startswith((" ", "\t"))
|
||||||
):
|
):
|
||||||
i += 1
|
j += 1
|
||||||
continue
|
continue
|
||||||
break
|
break
|
||||||
blocks.append((start, i))
|
|
||||||
|
comment_indices = set()
|
||||||
|
comment_text = []
|
||||||
|
k = start - 1
|
||||||
|
while k >= 0 and lines[k].strip() == "":
|
||||||
|
comment_indices.add(k)
|
||||||
|
k -= 1
|
||||||
|
while k >= 0 and lines[k].lstrip().startswith("#"):
|
||||||
|
comment_indices.add(k)
|
||||||
|
comment_text.append(lines[k])
|
||||||
|
k -= 1
|
||||||
|
comment_text.reverse()
|
||||||
|
|
||||||
|
blocks.append(
|
||||||
|
{
|
||||||
|
"start": start,
|
||||||
|
"end": j,
|
||||||
|
"comment_indices": comment_indices,
|
||||||
|
"comment_text": comment_text,
|
||||||
|
"entries": entries,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
i = j
|
||||||
continue
|
continue
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
if len(blocks) <= 1:
|
if len(blocks) <= 1:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
keep_start, keep_end = blocks[-1]
|
seen = set()
|
||||||
|
merged = []
|
||||||
|
for block in blocks:
|
||||||
|
for entry in block["entries"]:
|
||||||
|
key = entry.lower()
|
||||||
|
if not key or key in seen:
|
||||||
|
continue
|
||||||
|
seen.add(key)
|
||||||
|
merged.append(entry)
|
||||||
|
|
||||||
def capture_comment(idx):
|
if not merged:
|
||||||
remove = set()
|
sys.exit(0)
|
||||||
j = idx - 1
|
|
||||||
while j >= 0 and (lines[j].strip() == "" or lines[j].lstrip().startswith("#")):
|
|
||||||
remove.add(j)
|
|
||||||
j -= 1
|
|
||||||
return remove
|
|
||||||
|
|
||||||
to_remove = set()
|
first_block = blocks[0]
|
||||||
for start, end in blocks[:-1]:
|
insert_at = min(
|
||||||
to_remove.update(range(start, end))
|
[first_block["start"]] + list(first_block["comment_indices"])
|
||||||
to_remove.update(capture_comment(start))
|
) if first_block["comment_indices"] else first_block["start"]
|
||||||
|
|
||||||
result = [text for idx, text in enumerate(lines) if idx not in to_remove]
|
def build_comment():
|
||||||
content = "\n".join(result).rstrip()
|
if first_block["comment_text"]:
|
||||||
if content:
|
return first_block["comment_text"]
|
||||||
content += "\n"
|
return ["# Cluster nodes (auto-discovered during installation)"]
|
||||||
|
|
||||||
|
comment_block = build_comment()
|
||||||
|
replacement = []
|
||||||
|
replacement.extend(comment_block)
|
||||||
|
if replacement and replacement[-1].strip() != "":
|
||||||
|
replacement.append("")
|
||||||
|
replacement.append("allowed_nodes:")
|
||||||
|
for entry in merged:
|
||||||
|
replacement.append(f" - {entry}")
|
||||||
|
replacement.append("")
|
||||||
|
|
||||||
|
indices_to_remove = set()
|
||||||
|
for block in blocks:
|
||||||
|
indices_to_remove.update(range(block["start"], block["end"]))
|
||||||
|
indices_to_remove.update(block["comment_indices"])
|
||||||
|
|
||||||
|
result = []
|
||||||
|
inserted = False
|
||||||
|
for idx, line in enumerate(lines):
|
||||||
|
if not inserted and idx == insert_at:
|
||||||
|
result.extend(replacement)
|
||||||
|
inserted = True
|
||||||
|
if idx in indices_to_remove:
|
||||||
|
continue
|
||||||
|
result.append(line)
|
||||||
|
|
||||||
|
if not inserted:
|
||||||
|
if result and result[-1].strip() != "":
|
||||||
|
result.append("")
|
||||||
|
result.extend(replacement)
|
||||||
|
|
||||||
|
content = "\n".join(result).rstrip() + "\n"
|
||||||
path.write_text(content)
|
path.write_text(content)
|
||||||
PY
|
PY
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue