92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
import os
|
|
import re
|
|
import sys
|
|
|
|
# Konfiguration direkt im Skript
|
|
MODIFICATIONS = [
|
|
{
|
|
'file_pattern': r'session-login-index-html\..*\.bundle\.js',
|
|
'rules': [
|
|
{
|
|
'after_text': '<div class="padded-left padded-right padded-bottom-page margin-auto-y">',
|
|
'insert_text': '<img src="/web/logo.png" width=350px style="padding: 0px;display:block; margin-left: auto; margin-right: auto;">'
|
|
},
|
|
{
|
|
'after_text': '<div class="another-specific-div">',
|
|
'insert_text': '<p>Zusätzlicher Informationstext</p>'
|
|
}
|
|
]
|
|
},
|
|
{
|
|
'file_pattern': r'another-file-pattern\.js',
|
|
'rules': [
|
|
{
|
|
'after_text': 'specific-text-to-search-for',
|
|
'insert_text': 'New content to insert'
|
|
}
|
|
]
|
|
}
|
|
]
|
|
|
|
def modify_files(start_directory='.'):
|
|
"""
|
|
Modifiziert Dateien basierend auf vordefinierter Konfiguration.
|
|
|
|
:param start_directory: Startverzeichnis für die Suche
|
|
"""
|
|
files_modified = 0
|
|
|
|
# Durch jede Konfiguration gehen
|
|
for config in MODIFICATIONS:
|
|
search_pattern = config['file_pattern']
|
|
insert_rules = config['rules']
|
|
|
|
# Rekursive Suche ab dem Startverzeichnis
|
|
for root, dirs, files in os.walk(start_directory):
|
|
for filename in files:
|
|
# Überprüfe Dateinamenmuster
|
|
if re.search(search_pattern, filename):
|
|
full_path = os.path.join(root, filename)
|
|
|
|
try:
|
|
with open(full_path, 'r', encoding='utf-8') as file:
|
|
content = file.read()
|
|
|
|
modified = False
|
|
modified_content = content
|
|
|
|
# Durchlaufe alle Einfügeregeln für diese Datei
|
|
for rule in insert_rules:
|
|
search_text = rule['after_text']
|
|
insert_text = rule['insert_text']
|
|
|
|
# Prüfe, ob der Einfügetext bereits vorhanden ist
|
|
if insert_text not in modified_content:
|
|
modified_content = modified_content.replace(
|
|
search_text,
|
|
f'{search_text}\n{insert_text}'
|
|
)
|
|
modified = True
|
|
|
|
# Speichere die Datei, wenn Änderungen vorgenommen wurden
|
|
if modified:
|
|
with open(full_path, 'w', encoding='utf-8') as file:
|
|
file.write(modified_content)
|
|
|
|
files_modified += 1
|
|
print(f'Modifiziert: {full_path}')
|
|
|
|
except Exception as e:
|
|
print(f'Fehler bei Datei {full_path}: {e}')
|
|
|
|
print(f'\nAnzahl modifizierter Dateien: {files_modified}')
|
|
|
|
def main():
|
|
# Optionaler Startpfad als Argument
|
|
start_directory = sys.argv[1] if len(sys.argv) > 1 else '.'
|
|
|
|
print(f'Suche ab Verzeichnis: {os.path.abspath(start_directory)}')
|
|
modify_files(start_directory)
|
|
|
|
if __name__ == '__main__':
|
|
main() |