Compare commits
No commits in common. "wip-better-sources-replacer" and "main" have entirely different histories.
wip-better
...
main
2 changed files with 44 additions and 43 deletions
|
@ -1,6 +1,5 @@
|
||||||
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
|
||||||
|
@ -68,31 +67,6 @@ 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
|
||||||
|
@ -124,11 +98,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)
|
|
||||||
|
|
||||||
sources[arch if arch else "-"] = []
|
arch = re_match.group(1)
|
||||||
for v in Utils.get_string_values_from_array(value_node):
|
sources[arch if arch else "-"] = (
|
||||||
sources[arch if arch else "-"].append(v)
|
Utils.get_string_values_from_array(value_node)
|
||||||
|
)
|
||||||
|
|
||||||
re_match = re.match(CHECKSUMS_PATTERN, var_name)
|
re_match = re.match(CHECKSUMS_PATTERN, var_name)
|
||||||
if re_match:
|
if re_match:
|
||||||
|
@ -137,16 +111,10 @@ 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)
|
||||||
|
@ -167,9 +135,7 @@ 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(
|
source_files.append(file)
|
||||||
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)
|
||||||
|
|
35
aides_spec/replacers/sources_replacer.py
Normal file
35
aides_spec/replacers/sources_replacer.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
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