-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_encoding_script.ps1
More file actions
343 lines (292 loc) · 9.73 KB
/
fix_encoding_script.ps1
File metadata and controls
343 lines (292 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# Fix-Encoding.ps1 - Script to repair encoding issues
# Recursively fixes all encoding issues for file .ps1 and .py
param(
[switch]$WhatIf,
[switch]$Backup,
[switch]$Verbose
)
Write-Host "=== Audio Transcription Tool - Encoding Fix ===" -ForegroundColor Blue
Write-Host ""
# Define mapping of common corrupted characters
$CharacterMappings = @{
# Corrupted special characters
'â˜"' = '✓'
'âœ"' = '✓'
'â†'' = '✓'
'↨' = '✓'
'â†'' = '→'
'â€"' = '–'
'’' = "'"
'“' = '"'
'â€' = '"'
'…' = '...'
# Other problematic characters
'á' = 'á'
'é' = 'é'
'Ã' = 'í'
'ó' = 'ó'
'ú' = 'ú'
'ñ' = 'ñ'
'ü' = 'ü'
# Characters that cause PowerShell parsing errors
'‘' = "'"
'’' = "'"
'“' = '"'
'â€' = '"'
'"' = '"'
'"' = '"'
''' = "'"
''' = "'"
}
# Pattern to remove invisible control characters
$ControlCharPattern = '[^\x20-\x7E\x0A\x0D\x09]'
function Write-Log {
param([string]$Message, [string]$Level = "INFO")
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$color = switch ($Level) {
"ERROR" { "Red" }
"WARNING" { "Yellow" }
"SUCCESS" { "Green" }
default { "White" }
}
Write-Host "[$timestamp] $Level : $Message" -ForegroundColor $color
}
function Test-FileEncoding {
param([string]$FilePath)
try {
$bytes = [System.IO.File]::ReadAllBytes($FilePath)
# Check for BOM
if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {
return "UTF8-BOM"
}
elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 0xFF -and $bytes[1] -eq 0xFE) {
return "UTF16-LE"
}
elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 0xFE -and $bytes[1] -eq 0xFF) {
return "UTF16-BE"
}
else {
# Try to detect by content
$content = [System.Text.Encoding]::UTF8.GetString($bytes)
if ($content -match $ControlCharPattern -or $content -match 'â|Ã') {
return "CORRUPTED"
}
return "UTF8"
}
}
catch {
return "ERROR"
}
}
function Fix-FileContent {
param([string]$FilePath, [string]$Content)
$fixedContent = $Content
$changesApplied = @()
# Apply character mappings
foreach ($corruptedChar in $CharacterMappings.Keys) {
$correctChar = $CharacterMappings[$corruptedChar]
if ($fixedContent.Contains($corruptedChar)) {
$fixedContent = $fixedContent.Replace($corruptedChar, $correctChar)
$changesApplied += "$corruptedChar -> $correctChar"
}
}
# Remove invisible control characters (except tabs, newlines, carriage returns)
$originalLength = $fixedContent.Length
$fixedContent = $fixedContent -replace $ControlCharPattern, ''
if ($fixedContent.Length -ne $originalLength) {
$removedChars = $originalLength - $fixedContent.Length
$changesApplied += "Removed $removedChars invisible control characters"
}
# Fix common PowerShell syntax issues
if ($FilePath -match '\.ps1$') {
# Fix broken string quotes
$fixedContent = $fixedContent -replace '(?<!\\)"([^"]*?)"(?=\s|$)', '"$1"'
# Fix function brackets
$fixedContent = $fixedContent -replace '\{\s*$', '{'
# Ensure proper line endings
$fixedContent = $fixedContent -replace '\r\n', "`n"
$fixedContent = $fixedContent -replace '\r', "`n"
$fixedContent = $fixedContent -replace '\n', "`r`n"
}
return @{
Content = $fixedContent
Changes = $changesApplied
Modified = $changesApplied.Count -gt 0
}
}
function Backup-File {
param([string]$FilePath)
$backupPath = "$FilePath.backup-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
try {
Copy-Item -Path $FilePath -Destination $backupPath -Force
Write-Log "Backup created: $backupPath" "INFO"
return $backupPath
}
catch {
Write-Log "Failed to create backup for $FilePath : $($_.Exception.Message)" "ERROR"
return $null
}
}
function Test-PowerShellSyntax {
param([string]$FilePath)
if ($FilePath -notmatch '\.ps1$') {
return $true
}
try {
$null = [System.Management.Automation.PSParser]::Tokenize((Get-Content $FilePath -Raw), [ref]$null)
return $true
}
catch {
return $false
}
}
function Test-PythonSyntax {
param([string]$FilePath)
if ($FilePath -notmatch '\.py$') {
return $true
}
try {
$result = python -m py_compile $FilePath 2>&1
return $LASTEXITCODE -eq 0
}
catch {
return $false
}
}
# Main execution
$scriptFiles = Get-ChildItem -Path "." -Recurse -Include "*.ps1", "*.py" | Where-Object { !$_.PSIsContainer }
Write-Log "Found $($scriptFiles.Count) script files to check" "INFO"
$stats = @{
Total = 0
Fixed = 0
Errors = 0
Skipped = 0
}
foreach ($file in $scriptFiles) {
$stats.Total++
$filePath = $file.FullName
$relativePath = Resolve-Path -Path $filePath -Relative
Write-Host ""
Write-Log "Processing: $relativePath" "INFO"
# Test current encoding
$encoding = Test-FileEncoding -FilePath $filePath
Write-Log "Current encoding: $encoding" "INFO"
try {
# Read file content with best-effort encoding detection
$content = $null
try {
$content = Get-Content -Path $filePath -Raw -Encoding UTF8
}
catch {
try {
$content = Get-Content -Path $filePath -Raw -Encoding Default
}
catch {
$bytes = [System.IO.File]::ReadAllBytes($filePath)
$content = [System.Text.Encoding]::UTF8.GetString($bytes)
}
}
if ([string]::IsNullOrEmpty($content)) {
Write-Log "File is empty or unreadable, skipping" "WARNING"
$stats.Skipped++
continue
}
# Check if file needs fixing
$needsFix = $false
# Check for encoding issues
if ($encoding -eq "CORRUPTED" -or $content -match 'â|Ã') {
$needsFix = $true
Write-Log "Encoding issues detected" "WARNING"
}
# Check syntax
$syntaxOk = $true
if ($filePath -match '\.ps1$') {
$syntaxOk = Test-PowerShellSyntax -FilePath $filePath
if (-not $syntaxOk) {
$needsFix = $true
Write-Log "PowerShell syntax errors detected" "WARNING"
}
}
elseif ($filePath -match '\.py$') {
$syntaxOk = Test-PythonSyntax -FilePath $filePath
if (-not $syntaxOk) {
$needsFix = $true
Write-Log "Python syntax issues detected" "WARNING"
}
}
if (-not $needsFix) {
Write-Log "File is OK, no changes needed" "SUCCESS"
continue
}
# Fix the content
$fixResult = Fix-FileContent -FilePath $filePath -Content $content
if (-not $fixResult.Modified) {
Write-Log "No fixable issues found" "INFO"
continue
}
# Show what will be changed
Write-Log "Changes to apply:" "INFO"
foreach ($change in $fixResult.Changes) {
Write-Log " - $change" "INFO"
}
if ($WhatIf) {
Write-Log "WHATIF: Would apply changes to $relativePath" "INFO"
continue
}
# Create backup if requested
if ($Backup) {
$backupPath = Backup-File -FilePath $filePath
if (-not $backupPath) {
Write-Log "Skipping file due to backup failure" "ERROR"
$stats.Errors++
continue
}
}
# Apply fixes
try {
[System.IO.File]::WriteAllText($filePath, $fixResult.Content, [System.Text.Encoding]::UTF8)
Write-Log "File fixed successfully" "SUCCESS"
$stats.Fixed++
# Verify syntax after fix
if ($filePath -match '\.ps1$') {
if (Test-PowerShellSyntax -FilePath $filePath) {
Write-Log "PowerShell syntax verification: OK" "SUCCESS"
}
else {
Write-Log "PowerShell syntax verification: FAILED" "ERROR"
}
}
elseif ($filePath -match '\.py$') {
if (Test-PythonSyntax -FilePath $filePath) {
Write-Log "Python syntax verification: OK" "SUCCESS"
}
else {
Write-Log "Python syntax verification: FAILED" "ERROR"
}
}
}
catch {
Write-Log "Failed to write fixed content: $($_.Exception.Message)" "ERROR"
$stats.Errors++
}
}
catch {
Write-Log "Error processing file: $($_.Exception.Message)" "ERROR"
$stats.Errors++
}
}
# Summary
Write-Host ""
Write-Host "=== SUMMARY ===" -ForegroundColor Blue
Write-Log "Total files processed: $($stats.Total)" "INFO"
Write-Log "Files fixed: $($stats.Fixed)" "SUCCESS"
Write-Log "Files with errors: $($stats.Errors)" "ERROR"
Write-Log "Files skipped: $($stats.Skipped)" "WARNING"
if ($WhatIf) {
Write-Host ""
Write-Log "This was a dry run. Use without -WhatIf to apply changes." "INFO"
}
if ($stats.Fixed -gt 0) {
Write-Host ""
Write-Log "Encoding fix completed! You can now run your scripts." "SUCCESS"
}