aides-spec/alr_spec/main.py
2024-12-01 21:49:14 +03:00

66 lines
1.7 KiB
Python

import argparse
import sys
import tree_sitter_bash as tsbash
from tree_sitter import Language, Parser
from alr_spec.replacers.arch_replacer import ArchReplacer
from alr_spec.replacers.simple_replacer import SimpleReplacer
from alr_spec.replacers.sources_replacer import SourcesReplacer
def node_text(source_code, node):
return source_code[node.start_byte : node.end_byte].decode("utf-8")
parser_ts = None
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 main():
parser = argparse.ArgumentParser(
description="Заменяет pkgname на name в PKGBUILD используя Tree-sitter."
)
parser.add_argument("file", help="Путь к файлу PKGBUILD")
args = parser.parse_args()
BASH_LANGUAGE = Language(tsbash.language())
global parser_ts
parser_ts = Parser(BASH_LANGUAGE)
try:
with open(args.file, "rb") as f:
content = f.read()
except IOError as e:
print(f"Ошибка при чтении файла: {e}")
sys.exit(1)
tree = parser_ts.parse(content)
replacers = [
SimpleReplacer,
ArchReplacer,
SourcesReplacer,
]
new_content = process_file(content, tree, replacers)
# new_content = replace_pkgname(content, positions)
try:
with open("alr.sh", "wb") as f:
f.write(new_content)
except IOError as e:
print(f"Ошибка при записи файла: {e}")
sys.exit(1)
if __name__ == "__main__":
main()