wip: add skip handle and source name

This commit is contained in:
Максим Слипенко 2025-01-08 13:53:25 +03:00
parent 6c66e8d10f
commit 04ab7872b5
2 changed files with 43 additions and 44 deletions

View file

@ -1,5 +1,6 @@
import re import re
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
if TYPE_CHECKING: if TYPE_CHECKING:
from tree_sitter import Node from tree_sitter import Node
@ -67,6 +68,31 @@ CHECKSUMS_PATTERN = (
) )
def modify_string(input_string):
# Шаблон для поиска строки в формате `"name"::"url"`
pattern = r'"([^"]+)"::"([^"]+)"'
def replacer(match):
name = match.group(1) # Извлекаем имя
url = match.group(2) # Извлекаем URL
# Разбираем URL
parsed_url = urlparse(url)
query_params = parse_qs(parsed_url.query)
# Добавляем новый параметр
query_params["~name"] = [name]
# Формируем новый URL
new_query = urlencode(query_params, doseq=True)
new_url = urlunparse(parsed_url._replace(query=new_query))
# Возвращаем результат
return f'"{new_url}"'
# Применяем замену
return re.sub(pattern, replacer, input_string)
class SourcesReplacer(BaseReplacer): class SourcesReplacer(BaseReplacer):
def process(self): def process(self):
root_node = self.tree.root_node root_node = self.tree.root_node
@ -98,11 +124,11 @@ class SourcesReplacer(BaseReplacer):
re_match = re.match(SOURCE_PATTERN, var_name) re_match = re.match(SOURCE_PATTERN, var_name)
if re_match: if re_match:
self.nodes_to_remove.append(node) self.nodes_to_remove.append(node)
arch = re_match.group(1) arch = re_match.group(1)
sources[arch if arch else "-"] = (
Utils.get_string_values_from_array(value_node) sources[arch if arch else "-"] = []
) for v in Utils.get_string_values_from_array(value_node):
sources[arch if arch else "-"].append(v)
re_match = re.match(CHECKSUMS_PATTERN, var_name) re_match = re.match(CHECKSUMS_PATTERN, var_name)
if re_match: if re_match:
@ -111,10 +137,16 @@ class SourcesReplacer(BaseReplacer):
checksum = CHECKSUMS_REPLACEMENTS[re_match.group(1)] checksum = CHECKSUMS_REPLACEMENTS[re_match.group(1)]
arch = re_match.group(2) arch = re_match.group(2)
checksums[arch if arch else "-"] = [ checksums[arch if arch else "-"] = []
f"'{checksum}:{v.get_text_value()}'"
for v in Utils.get_string_values_from_array(value_node) for v in Utils.get_string_values_from_array(value_node):
] checksum_value = v.get_text_value()
if checksum_value != "SKIP":
checksum_value = f"'{checksum}:{checksum_value}'"
else:
checksum_value = f"'{checksum_value}'"
checksums[arch if arch else "-"].append(checksum_value)
def traverse(node: "Node"): def traverse(node: "Node"):
execute(node) execute(node)
@ -135,7 +167,9 @@ class SourcesReplacer(BaseReplacer):
for i, file in enumerate(files): for i, file in enumerate(files):
file_name = file.get_text_value() file_name = file.get_text_value()
if "://" in file_name: if "://" in file_name:
source_files.append(file) source_files.append(
modify_string(file.node.text.decode("utf-8"))
)
checksums_str.append(checksums[arch][i]) checksums_str.append(checksums[arch][i])
else: else:
self.local_files.append(file) self.local_files.append(file)

View file

@ -1,35 +0,0 @@
import re
from aides_spec.replacers.arch_replacer import ArchReplacer
from aides_spec.replacers.base import BaseReplacer
class SourcesReplacer(BaseReplacer):
SOURCE_PATTERN = r"^source(?:_(x86_64|i686|armv7h|aarch64))?$"
def process(self):
root_node = self.tree.root_node
def find_replacements(node):
if node.type == "variable_name":
var_name = self._node_text(node)
re_match = re.match(self.SOURCE_PATTERN, var_name)
if re_match:
arch = re_match.group(1)
self.replaces.append(
{
"node": node,
"content": (
f"sources_{ArchReplacer.ARCH_MAPPING[arch]}"
if arch
else "sources"
),
}
)
for child in node.children:
find_replacements(child)
find_replacements(root_node)
return self._apply_replacements()