diff --git a/gns3server/modules/vmware/__init__.py b/gns3server/modules/vmware/__init__.py index 414eecb0..b999a946 100644 --- a/gns3server/modules/vmware/__init__.py +++ b/gns3server/modules/vmware/__init__.py @@ -26,6 +26,7 @@ import shutil import asyncio import subprocess import logging +import codecs from collections import OrderedDict from gns3server.utils.interfaces import interfaces @@ -214,7 +215,27 @@ class VMware(BaseManager): """ pairs = OrderedDict() - with open(path, encoding="utf-8") as f: + encoding = "utf-8" + # get the first line to read the .encoding value + with open(path, encoding=encoding) as f: + line = f.readline() + if line.startswith("#!"): + # skip the shebang + line = f.readline() + try: + key, value = line.split('=', 1) + if key.strip().lower() == ".encoding": + file_encoding = value.strip('" ') + try: + codecs.lookup(file_encoding) + encoding = file_encoding + except LookupError: + log.warning("Invalid file encoding detected in '{}': {}".format(path, file_encoding)) + except ValueError: + log.warning("Couldn't find file encoding in {}, using {}...".format(path, encoding)) + + # read the file with the correct encoding + with open(path, encoding=encoding, errors="ignore") as f: for line in f.read().splitlines(): try: key, value = line.split('=', 1) @@ -232,7 +253,15 @@ class VMware(BaseManager): :param pairs: settings to write """ - with open(path, "w", encoding="utf-8") as f: + encoding = "utf-8" + if ".encoding" in pairs: + file_encoding = pairs[".encoding"] + try: + codecs.lookup(file_encoding) + encoding = file_encoding + except LookupError: + log.warning("Invalid file encoding detected in '{}': {}".format(path, file_encoding)) + with open(path, "w", encoding=encoding, errors="ignore") as f: for key, value in pairs.items(): entry = '{} = "{}"\n'.format(key, value) f.write(entry) @@ -246,7 +275,15 @@ class VMware(BaseManager): :param pairs: settings to write """ - with open(path, "w", encoding="utf-8") as f: + encoding = "utf-8" + if ".encoding" in pairs: + file_encoding = pairs[".encoding"] + try: + codecs.lookup(file_encoding) + encoding = file_encoding + except LookupError: + log.warning("Invalid file encoding detected in '{}': {}".format(path, file_encoding)) + with open(path, "w", encoding=encoding, errors="ignore") as f: if sys.platform.startswith("linux"): # write the shebang on the first line on Linux vmware_path = shutil.which("vmware")