| | import os
|
| | import json
|
| | import re
|
| |
|
| | def sanitize_filename(filename):
|
| | """Remove ou substitui caracteres inv谩lidos em nomes de arquivos."""
|
| | return re.sub(r'[<>:"/\\|?*]', '_', filename)
|
| |
|
| | def compile_safe_codes_to_json(input_dir, output_file):
|
| | """Compila todos os arquivos de c贸digos seguros em um 煤nico JSON."""
|
| | compiled_data = []
|
| |
|
| |
|
| | for filename in os.listdir(input_dir):
|
| | if filename.endswith(".txt"):
|
| | file_path = os.path.join(input_dir, filename)
|
| | try:
|
| | with open(file_path, 'r', encoding='utf-8') as f:
|
| | content = f.read()
|
| |
|
| | compiled_data.append({
|
| | "filename": sanitize_filename(filename),
|
| | "content": content,
|
| | "safe": True
|
| | })
|
| | except Exception as e:
|
| | print(f"[ERROR] Could not process file {filename}: {e}")
|
| |
|
| |
|
| | try:
|
| | with open(output_file, 'w', encoding='utf-8') as json_file:
|
| | json.dump(compiled_data, json_file, indent=4)
|
| | print(f"[SUCCESS] Compiled {len(compiled_data)} safe codes into {output_file}")
|
| | except Exception as e:
|
| | print(f"[ERROR] Could not write JSON file: {e}")
|
| |
|
| | if __name__ == "__main__":
|
| | input_directory = "safe-code-analyzer/safe_codes"
|
| | output_json_file = "safe-code-analyzer/compiled_safe_codes.json"
|
| |
|
| | os.makedirs(os.path.dirname(output_json_file), exist_ok=True)
|
| | compile_safe_codes_to_json(input_directory, output_json_file)
|
| |
|