|
| 1 | +import json |
| 2 | +import re |
| 3 | + |
| 4 | +def read_config_from_json(json_file_path): |
| 5 | + try: |
| 6 | + with open(json_file_path, 'r') as json_file: |
| 7 | + data = json.load(json_file) |
| 8 | + file_path = data.get("file_path") |
| 9 | + old_signkey = data.get("old_signkey") |
| 10 | + new_signkey = data.get("new_signkey") |
| 11 | + |
| 12 | + if not file_path or not old_signkey or not new_signkey: |
| 13 | + raise ValueError("JSON-Datei muss 'file_path', 'old_signkey' und 'new_signkey' enthalten.") |
| 14 | + |
| 15 | + return file_path, old_signkey, new_signkey |
| 16 | + |
| 17 | + except FileNotFoundError: |
| 18 | + print(f"Die JSON-Datei {json_file_path} wurde nicht gefunden.") |
| 19 | + raise |
| 20 | + except json.JSONDecodeError: |
| 21 | + print(f"Fehler beim Parsen der JSON-Datei {json_file_path}.") |
| 22 | + raise |
| 23 | + |
| 24 | +def replace_signkey_in_file(file_path, old_signkey, new_signkey): |
| 25 | + if len(old_signkey) != len(new_signkey): |
| 26 | + raise ValueError("Der neue Hex-String muss die gleiche Länge haben wie der alte Hex-String.") |
| 27 | + |
| 28 | + if old_signkey.startswith("0x"): |
| 29 | + old_signkey = old_signkey[2:] |
| 30 | + if new_signkey.startswith("0x"): |
| 31 | + new_signkey = new_signkey[2:] |
| 32 | + |
| 33 | + if not re.fullmatch(r'[0-9a-fA-F]+', old_signkey): |
| 34 | + raise ValueError("Der alte Hex-String ist nicht gültig.") |
| 35 | + if not re.fullmatch(r'[0-9a-fA-F]+', new_signkey): |
| 36 | + raise ValueError("Der neue Hex-String ist nicht gültig.") |
| 37 | + |
| 38 | + try: |
| 39 | + with open(file_path, 'rb') as file: |
| 40 | + content = file.read() |
| 41 | + |
| 42 | + old_signkey_bytes = bytes.fromhex(old_signkey) |
| 43 | + new_signkey_bytes = bytes.fromhex(new_signkey) |
| 44 | + |
| 45 | + content = content.replace(old_signkey_bytes, new_signkey_bytes) |
| 46 | + |
| 47 | + with open(file_path, 'wb') as file: |
| 48 | + file.write(content) |
| 49 | + |
| 50 | + print("Hex-String erfolgreich ersetzt.") |
| 51 | + |
| 52 | + except FileNotFoundError: |
| 53 | + print(f"Die Datei {file_path} wurde nicht gefunden.") |
| 54 | + except Exception as e: |
| 55 | + print(f"Ein Fehler ist aufgetreten: {e}") |
| 56 | + |
| 57 | +json_file_path = 'hex.json' |
| 58 | + |
| 59 | +try: |
| 60 | + file_path, old_signkey, new_signkey = read_config_from_json(json_file_path) |
| 61 | + replace_signkey_in_file(file_path, old_signkey, new_signkey) |
| 62 | +except Exception as e: |
| 63 | + print(f"Fehler: {e}") |
0 commit comments