wip: add skip handle and source name
This commit is contained in:
parent
6c66e8d10f
commit
04ab7872b5
2 changed files with 43 additions and 44 deletions
|
@ -1,5 +1,6 @@
|
|||
import re
|
||||
from typing import TYPE_CHECKING
|
||||
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
|
||||
|
||||
if TYPE_CHECKING:
|
||||
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):
|
||||
def process(self):
|
||||
root_node = self.tree.root_node
|
||||
|
@ -98,11 +124,11 @@ class SourcesReplacer(BaseReplacer):
|
|||
re_match = re.match(SOURCE_PATTERN, var_name)
|
||||
if re_match:
|
||||
self.nodes_to_remove.append(node)
|
||||
|
||||
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)
|
||||
if re_match:
|
||||
|
@ -111,10 +137,16 @@ class SourcesReplacer(BaseReplacer):
|
|||
checksum = CHECKSUMS_REPLACEMENTS[re_match.group(1)]
|
||||
arch = re_match.group(2)
|
||||
|
||||
checksums[arch if arch else "-"] = [
|
||||
f"'{checksum}:{v.get_text_value()}'"
|
||||
for v in Utils.get_string_values_from_array(value_node)
|
||||
]
|
||||
checksums[arch if arch else "-"] = []
|
||||
|
||||
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"):
|
||||
execute(node)
|
||||
|
@ -135,7 +167,9 @@ class SourcesReplacer(BaseReplacer):
|
|||
for i, file in enumerate(files):
|
||||
file_name = file.get_text_value()
|
||||
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])
|
||||
else:
|
||||
self.local_files.append(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()
|
Loading…
Reference in a new issue