aides-spec/aides_spec/utils/from_pkgbuild.py
2025-01-03 00:26:20 +03:00

56 lines
1.5 KiB
Python

import sys
import requests
import tree_sitter_bash as tsbash
from tree_sitter import Language, Parser
from aides_spec.replacers.arch_replacer import ArchReplacer
from aides_spec.replacers.simple_replacer import SimpleReplacer
from aides_spec.replacers.sources_replacer import SourcesReplacer
parser_ts = None
def download_pkgbuild(pkgname):
aur_url = (
f"https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h={pkgname}"
)
try:
response = requests.get(aur_url, timeout=300)
response.raise_for_status()
return response.content
except requests.RequestException as e:
print(f"Ошибка загрузки PKGBUILD из AUR: {e}")
sys.exit(1)
def process_file(content, tree, replacers):
for replacer_class in replacers:
replacer = replacer_class(content, tree)
content = replacer.process()
tree = parser_ts.parse(content, tree)
return content
def create_from_pkgbuild(content, output_file):
global parser_ts
BASH_LANGUAGE = Language(tsbash.language())
parser_ts = Parser(BASH_LANGUAGE)
tree = parser_ts.parse(content)
replacers = [
SimpleReplacer,
ArchReplacer,
SourcesReplacer,
]
new_content = process_file(content, tree, replacers)
try:
with open(output_file, "wb") as f:
f.write(new_content)
print(f"Файл успешно записан в {output_file}.")
except IOError as e:
print(f"Ошибка при записи файла: {e}")
sys.exit(1)