Created
June 10, 2024 23:26
-
-
Save cslemes/36ffb29194724cf266d69779b8b5f2f2 to your computer and use it in GitHub Desktop.
Habilita modulos cilium no arquivo .config_wsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# Lê o conteúdo do arquivo 'cilium_modules | |
config_replacements = {} | |
with open('cilium_modules', 'r', encoding='utf-8') as file1: | |
for line in file1: | |
line = line.strip() | |
# Ignora linhas vazias e comentários | |
if not line or line.startswith('##'): | |
continue | |
key, value = line.split('=') | |
config_replacements[key] = value | |
# Lê o conteúdo do arquivo '.config' | |
with open('.config', 'r') as file2: | |
file2_lines = file2.readlines() | |
# Mantém um conjunto para controle das chaves que foram atualizadas | |
updated_keys = set() | |
# Substitui linhas correspondentes em '.config' | |
with open('.config', 'w', encoding='utf-8') as file2: | |
for line in file2_lines: | |
# Verifica se a linha contém alguma chave de 'cilium_modules' usando regex | |
for key, value in config_replacements.items(): | |
if re.search(r'\b' + re.escape(key) + r'\b', line): | |
# Se a linha estiver comentada, remove o símbolo de comentário e atualiza | |
if line.startswith('# ' + key): | |
line = f"{key}={value}\n" | |
# Atualiza o valor da linha | |
elif re.search(r'^\s*' + re.escape(key) + r'\b', line): | |
line = f"{key}={value}\n" | |
updated_keys.add(key) | |
break | |
file2.write(line) | |
# Adiciona as chaves que não foram encontradas ao '.config' | |
for key, value in config_replacements.items(): | |
if key not in updated_keys: | |
file2.write(f"{key}={value}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment