Fix cert.csv encoding to utf-8

This commit is contained in:
VirtuBox
2020-10-14 15:29:06 +02:00
parent 3ffeec6ef4
commit c3076cfdfe
5 changed files with 32 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ import fileinput
import os
import pwd
import shutil
import codecs
from wo.core.logging import Log
@@ -385,3 +386,27 @@ class WOFileUtils():
return True
else:
return False
def writeConversion(self, file):
with codecs.open('tmp' + '/' + file, 'w', 'utf-8') as targetFile:
for line in file:
targetFile.write(line)
def convertFileBestGuess(self, filename, filepath):
"""Convert file to utf-8"""
sourceFormats = ['ascii', 'iso-8859-1']
for format in sourceFormats:
try:
os.chdir(filepath)
with codecs.open(filename, 'rU', format) as sourceFile:
self.writeConversion(sourceFile)
return
except UnicodeDecodeError:
pass
if os.path.exists("/tmp/{0}".format(filename)):
try:
self.rm(filepath + filename)
self.mvfile("/tmp/{0}".format(filename), filepath + filename)
except Exception as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "Unable to change owner : {0} ".format(path))