fixes
This commit is contained in:
@ -1,92 +0,0 @@
|
|||||||
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()
|
|
@ -1,95 +0,0 @@
|
|||||||
import os
|
|
||||||
import re
|
|
||||||
import sys
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
def modify_files(config_path):
|
|
||||||
"""
|
|
||||||
Modifiziert Dateien basierend auf einer YAML-Konfigurationsdatei.
|
|
||||||
|
|
||||||
:param config_path: Pfad zur Konfigurationsdatei
|
|
||||||
"""
|
|
||||||
# load config
|
|
||||||
with open(config_path, 'r', encoding='utf-8') as config_file:
|
|
||||||
config = yaml.safe_load(config_file)
|
|
||||||
|
|
||||||
files_modified = 0
|
|
||||||
|
|
||||||
# iterate trough every config
|
|
||||||
for entry in config['modifications']:
|
|
||||||
search_pattern = entry.get('file_pattern')
|
|
||||||
insert_rules = entry.get('insert_rules', [])
|
|
||||||
replace_rules = entry.get('replace_rules', [])
|
|
||||||
|
|
||||||
# recursiv search starting at start dir
|
|
||||||
for root, dirs, files in os.walk(config.get('start_directory', '.')):
|
|
||||||
for filename in files:
|
|
||||||
# check filenamepattern
|
|
||||||
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
|
|
||||||
|
|
||||||
# iterate trough insert rules
|
|
||||||
for rule in insert_rules:
|
|
||||||
insert_text = rule.get('insert_text')
|
|
||||||
|
|
||||||
# insert after specific text
|
|
||||||
if 'after_text' in rule:
|
|
||||||
search_text = rule.get('after_text')
|
|
||||||
if insert_text not in modified_content:
|
|
||||||
modified_content = modified_content.replace(
|
|
||||||
search_text,
|
|
||||||
f'{search_text}\n{insert_text}'
|
|
||||||
)
|
|
||||||
modified = True
|
|
||||||
|
|
||||||
# insert before spicific text
|
|
||||||
elif 'before_text' in rule:
|
|
||||||
search_text = rule.get('before_text')
|
|
||||||
if insert_text not in modified_content:
|
|
||||||
modified_content = modified_content.replace(
|
|
||||||
search_text,
|
|
||||||
f'{insert_text}\n{search_text}'
|
|
||||||
)
|
|
||||||
modified = True
|
|
||||||
|
|
||||||
# iterate replace rules
|
|
||||||
for rule in replace_rules:
|
|
||||||
old_text = rule.get('old_text')
|
|
||||||
new_text = rule.get('new_text')
|
|
||||||
|
|
||||||
# check, if the text to be replaced, exists
|
|
||||||
if old_text in modified_content:
|
|
||||||
modified_content = modified_content.replace(old_text, new_text)
|
|
||||||
modified = True
|
|
||||||
|
|
||||||
# save changed files
|
|
||||||
if modified:
|
|
||||||
with open(full_path, 'w', encoding='utf-8') as file:
|
|
||||||
file.write(modified_content)
|
|
||||||
|
|
||||||
files_modified += 1
|
|
||||||
print(f'Modified: {full_path}')
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f'Error modifying file {full_path}: {e}')
|
|
||||||
|
|
||||||
print(f'\nAnzahl modifizierter Dateien: {files_modified}')
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# path of config file as argument
|
|
||||||
if len(sys.argv) < 2:
|
|
||||||
print("Error: Provide a path to the config.yaml file")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
config_path = sys.argv[1]
|
|
||||||
modify_files(config_path)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
@ -1,150 +1,113 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import shutil
|
||||||
import sys
|
|
||||||
import yaml
|
import yaml
|
||||||
|
import re
|
||||||
|
|
||||||
def load_configuration(config_path):
|
def loadConfig(config_path):
|
||||||
"""
|
"""Load YAML configuration."""
|
||||||
Load configuration from a YAML file.
|
print(f"Loading configuration from {config_path}")
|
||||||
|
with open(config_path, 'r', encoding='utf-8') as file:
|
||||||
:param config_path: Path to the configuration file
|
config = yaml.safe_load(file)
|
||||||
:return: Loaded configuration dictionary
|
print("Configuration loaded successfully.")
|
||||||
"""
|
return config
|
||||||
with open(config_path, 'r', encoding='utf-8') as config_file:
|
|
||||||
return yaml.safe_load(config_file)
|
|
||||||
|
|
||||||
def matches_file_pattern(filename, pattern):
|
def ensureDirectory(path):
|
||||||
"""
|
"""Ensure that a directory exists."""
|
||||||
Check if filename matches the given pattern.
|
print(f"Checking/creating directory: {path}")
|
||||||
|
os.makedirs(path, exist_ok=True)
|
||||||
:param filename: Name of the file
|
|
||||||
:param pattern: Regex pattern to match
|
|
||||||
:return: Boolean indicating if filename matches
|
|
||||||
"""
|
|
||||||
return re.search(pattern, filename) is not None
|
|
||||||
|
|
||||||
def apply_insert_rules(content, insert_rules):
|
def copySources(config, destination_directory):
|
||||||
"""
|
"""Copy files and folders according to copy rules."""
|
||||||
Apply insertion rules to the content.
|
print("Starting source file/folder copy process...")
|
||||||
|
for rule in config.get('copy_rules', []):
|
||||||
:param content: Original file content
|
for source in rule.get('sources', []):
|
||||||
:param insert_rules: List of insertion rules
|
# Distinguish between sources with explicit target and without
|
||||||
:return: Modified content and modification flag
|
if isinstance(source, dict):
|
||||||
"""
|
src_path = source['source']
|
||||||
modified_content = content
|
target_path = os.path.join(destination_directory, source['target'])
|
||||||
modified = False
|
else:
|
||||||
|
src_path = source
|
||||||
|
target_path = os.path.join(destination_directory, os.path.basename(src_path))
|
||||||
|
|
||||||
|
# Create target directory
|
||||||
|
ensureDirectory(os.path.dirname(target_path))
|
||||||
|
|
||||||
|
# Copy or replace mode
|
||||||
|
if rule.get('mode') == 'replace' and os.path.exists(target_path):
|
||||||
|
print(f"Replacing existing path: {target_path}")
|
||||||
|
shutil.rmtree(target_path) if os.path.isdir(target_path) else os.remove(target_path)
|
||||||
|
|
||||||
|
# Copy files or directories
|
||||||
|
if os.path.isdir(src_path):
|
||||||
|
print(f"Copying directory: {src_path} -> {target_path}")
|
||||||
|
shutil.copytree(src_path, target_path)
|
||||||
|
else:
|
||||||
|
print(f"Copying file: {src_path} -> {target_path}")
|
||||||
|
shutil.copy2(src_path, target_path)
|
||||||
|
print("Source file/folder copy process completed.")
|
||||||
|
|
||||||
for rule in insert_rules:
|
def modifyFiles(config, destination_directory):
|
||||||
insert_text = rule.get('insert_text')
|
"""Modify files according to modification rules."""
|
||||||
|
print("Starting file modification process...")
|
||||||
|
for rule in config.get('modification_rules', []):
|
||||||
|
file_pattern = rule.get('file_pattern', '*')
|
||||||
|
print(f"Processing files matching pattern: {file_pattern}")
|
||||||
|
|
||||||
# Insert after specific text
|
# Recursively search the destination directory
|
||||||
if 'after_text' in rule:
|
for root, _, files in os.walk(destination_directory):
|
||||||
search_text = rule.get('after_text')
|
|
||||||
if insert_text not in modified_content:
|
|
||||||
modified_content = modified_content.replace(
|
|
||||||
search_text,
|
|
||||||
f'{search_text}\n{insert_text}'
|
|
||||||
)
|
|
||||||
modified = True
|
|
||||||
|
|
||||||
# Insert before specific text
|
|
||||||
elif 'before_text' in rule:
|
|
||||||
search_text = rule.get('before_text')
|
|
||||||
if insert_text not in modified_content:
|
|
||||||
modified_content = modified_content.replace(
|
|
||||||
search_text,
|
|
||||||
f'{insert_text}\n{search_text}'
|
|
||||||
)
|
|
||||||
modified = True
|
|
||||||
|
|
||||||
return modified_content, modified
|
|
||||||
|
|
||||||
def apply_replace_rules(content, replace_rules):
|
|
||||||
"""
|
|
||||||
Apply replacement rules to the content.
|
|
||||||
|
|
||||||
:param content: Original file content
|
|
||||||
:param replace_rules: List of replacement rules
|
|
||||||
:return: Modified content and modification flag
|
|
||||||
"""
|
|
||||||
modified_content = content
|
|
||||||
modified = False
|
|
||||||
|
|
||||||
for rule in replace_rules:
|
|
||||||
old_text = rule.get('old_text')
|
|
||||||
new_text = rule.get('new_text')
|
|
||||||
|
|
||||||
# Replace text if found
|
|
||||||
if old_text in modified_content:
|
|
||||||
modified_content = modified_content.replace(old_text, new_text)
|
|
||||||
modified = True
|
|
||||||
|
|
||||||
return modified_content, modified
|
|
||||||
|
|
||||||
def modify_files(config):
|
|
||||||
"""
|
|
||||||
Modify files based on configuration rules.
|
|
||||||
|
|
||||||
:param config: Configuration dictionary
|
|
||||||
:return: Number of modified files
|
|
||||||
"""
|
|
||||||
files_modified = 0
|
|
||||||
start_directory = config.get('start_directory', '.')
|
|
||||||
|
|
||||||
# Process each modification entry
|
|
||||||
for entry in config.get('modifications', []):
|
|
||||||
search_pattern = entry.get('file_pattern')
|
|
||||||
insert_rules = entry.get('insert_rules', [])
|
|
||||||
replace_rules = entry.get('replace_rules', [])
|
|
||||||
|
|
||||||
# Walk through directory
|
|
||||||
for root, _, files in os.walk(start_directory):
|
|
||||||
for filename in files:
|
for filename in files:
|
||||||
# Check if file matches pattern
|
if re.match(file_pattern, filename):
|
||||||
if matches_file_pattern(filename, search_pattern):
|
file_path = os.path.join(root, filename)
|
||||||
full_path = os.path.join(root, filename)
|
print(f"Modifying file: {file_path}")
|
||||||
|
|
||||||
try:
|
# Read file content
|
||||||
# Read file content
|
with open(file_path, 'r', encoding='utf-8') as f:
|
||||||
with open(full_path, 'r', encoding='utf-8') as file:
|
content = f.read()
|
||||||
content = file.read()
|
|
||||||
|
|
||||||
# Apply insert rules
|
|
||||||
content_after_insert, insert_modified = apply_insert_rules(
|
|
||||||
content, insert_rules
|
|
||||||
)
|
|
||||||
|
|
||||||
# Apply replace rules
|
|
||||||
final_content, replace_modified = apply_replace_rules(
|
|
||||||
content_after_insert, replace_rules
|
|
||||||
)
|
|
||||||
|
|
||||||
# Save modified file
|
|
||||||
if insert_modified or replace_modified:
|
|
||||||
with open(full_path, 'w', encoding='utf-8') as file:
|
|
||||||
file.write(final_content)
|
|
||||||
|
|
||||||
files_modified += 1
|
|
||||||
print(f'Modified: {full_path}')
|
|
||||||
|
|
||||||
except Exception as e:
|
# Perform text replacements
|
||||||
print(f'Error processing file {full_path}: {e}')
|
for insert_rule in rule.get('insert_rules', []):
|
||||||
|
if 'after_text' in insert_rule:
|
||||||
return files_modified
|
print(f" Inserting text after: {insert_rule['after_text']}")
|
||||||
|
content = content.replace(
|
||||||
|
insert_rule['after_text'],
|
||||||
|
insert_rule['after_text'] + insert_rule['insert_text']
|
||||||
|
)
|
||||||
|
|
||||||
|
if 'before_text' in insert_rule:
|
||||||
|
print(f" Inserting text before: {insert_rule['before_text']}")
|
||||||
|
content = content.replace(
|
||||||
|
insert_rule['before_text'],
|
||||||
|
insert_rule['insert_text'] + insert_rule['before_text']
|
||||||
|
)
|
||||||
|
|
||||||
|
if 'old_text' in insert_rule:
|
||||||
|
print(f" Replacing text: {insert_rule['old_text']} -> {insert_rule['new_text']}")
|
||||||
|
content = content.replace(
|
||||||
|
insert_rule['old_text'],
|
||||||
|
insert_rule['new_text']
|
||||||
|
)
|
||||||
|
|
||||||
|
# Write modified contents
|
||||||
|
with open(file_path, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(content)
|
||||||
|
print("File modification process completed.")
|
||||||
|
|
||||||
def main():
|
def main(config_path):
|
||||||
# Check command-line argument
|
"""Main function to execute all operations."""
|
||||||
if len(sys.argv) < 2:
|
# Load configuration
|
||||||
print("Please provide the path to the configuration file.")
|
config = loadConfig(config_path)
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
config_path = sys.argv[1]
|
# Ensure destination directory
|
||||||
|
destination_directory = config.get('destination_directory', './web')
|
||||||
|
ensureDirectory(destination_directory)
|
||||||
|
|
||||||
# Load configuration and modify files
|
# Copy files and folders
|
||||||
config = load_configuration(config_path)
|
copySources(config, destination_directory)
|
||||||
total_modified = modify_files(config)
|
|
||||||
|
|
||||||
print(f'\nTotal files modified: {total_modified}')
|
# Modify files
|
||||||
|
modifyFiles(config, destination_directory)
|
||||||
|
|
||||||
|
print(f"All operations in {destination_directory} completed successfully.")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main('config.yaml')
|
@ -83,7 +83,7 @@ def apply_insert_rules(content, insert_rules):
|
|||||||
if insert_text not in modified_content:
|
if insert_text not in modified_content:
|
||||||
modified_content = modified_content.replace(
|
modified_content = modified_content.replace(
|
||||||
search_text,
|
search_text,
|
||||||
f'{search_text} {insert_text}'
|
f'{search_text}\n{insert_text}'
|
||||||
)
|
)
|
||||||
modified = True
|
modified = True
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ def apply_insert_rules(content, insert_rules):
|
|||||||
if insert_text not in modified_content:
|
if insert_text not in modified_content:
|
||||||
modified_content = modified_content.replace(
|
modified_content = modified_content.replace(
|
||||||
search_text,
|
search_text,
|
||||||
f'{insert_text} {search_text}'
|
f'{insert_text}\n{search_text}'
|
||||||
)
|
)
|
||||||
modified = True
|
modified = True
|
||||||
|
|
||||||
@ -167,8 +167,46 @@ def process_copy_and_modify_rules(config):
|
|||||||
if copy_with_mode(matched_src, full_dest, mode):
|
if copy_with_mode(matched_src, full_dest, mode):
|
||||||
successful_operations['copies'] += 1
|
successful_operations['copies'] += 1
|
||||||
|
|
||||||
# Rest of the function remains the same as in the original script
|
# Process modification rules
|
||||||
# ... (modification rules processing)
|
destination_dir = config.get('destination_directory', '.')
|
||||||
|
|
||||||
|
for mod_rule in config.get('modification_rules', []):
|
||||||
|
search_pattern = mod_rule.get('file_pattern')
|
||||||
|
insert_rules = mod_rule.get('insert_rules', [])
|
||||||
|
replace_rules = mod_rule.get('replace_rules', [])
|
||||||
|
|
||||||
|
# Walk through destination directory
|
||||||
|
for root, _, files in os.walk(destination_dir):
|
||||||
|
for filename in files:
|
||||||
|
# Check if file matches pattern
|
||||||
|
if matches_file_pattern(filename, search_pattern):
|
||||||
|
full_path = os.path.join(root, filename)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Read file content
|
||||||
|
with open(full_path, 'r', encoding='utf-8') as file:
|
||||||
|
content = file.read()
|
||||||
|
|
||||||
|
# Apply insert rules
|
||||||
|
content_after_insert, insert_modified = apply_insert_rules(
|
||||||
|
content, insert_rules
|
||||||
|
)
|
||||||
|
|
||||||
|
# Apply replace rules
|
||||||
|
final_content, replace_modified = apply_replace_rules(
|
||||||
|
content_after_insert, replace_rules
|
||||||
|
)
|
||||||
|
|
||||||
|
# Save modified file
|
||||||
|
if insert_modified or replace_modified:
|
||||||
|
with open(full_path, 'w', encoding='utf-8') as file:
|
||||||
|
file.write(final_content)
|
||||||
|
|
||||||
|
successful_operations['modifications'] += 1
|
||||||
|
print(f'Modified: {full_path}')
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Error processing file {full_path}: {e}')
|
||||||
|
|
||||||
return successful_operations
|
return successful_operations
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ copy_rules:
|
|||||||
mode: 'replace' # Überschreibt vorhandene Dateien/Ordner
|
mode: 'replace' # Überschreibt vorhandene Dateien/Ordner
|
||||||
|
|
||||||
- sources:
|
- sources:
|
||||||
- './ui'
|
- './ui' # Gesamter Ordner/Datei wird in destination_directory kopiert
|
||||||
|
|
||||||
- source: './img/background.png'
|
- source: './img/background.png'
|
||||||
target: './assets/img/background.png'
|
target: './assets/img/background.png'
|
||||||
|
Reference in New Issue
Block a user