From 80a0e0ebf75bb344bf79ceeccd4618d4344be125 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 25 Apr 2015 11:58:34 -0600 Subject: [PATCH] Explicit utf-8 encoding where necessary to avoid Unicode errors on Windows (we require/set an utf-8 locale on other systems). --- gns3server/modules/dynamips/hypervisor.py | 4 +-- gns3server/modules/dynamips/nodes/router.py | 8 ++--- gns3server/modules/iou/iou_vm.py | 32 +++++++++---------- gns3server/modules/qemu/qemu_vm.py | 6 ++-- .../modules/virtualbox/virtualbox_vm.py | 4 +-- gns3server/modules/vpcs/vpcs_vm.py | 12 +++---- gns3server/web/route.py | 2 +- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/gns3server/modules/dynamips/hypervisor.py b/gns3server/modules/dynamips/hypervisor.py index 407072ca..a568ff51 100644 --- a/gns3server/modules/dynamips/hypervisor.py +++ b/gns3server/modules/dynamips/hypervisor.py @@ -158,8 +158,8 @@ class Hypervisor(DynamipsHypervisor): output = "" if self._stdout_file and os.access(self._stdout_file, os.R_OK): try: - with open(self._stdout_file, errors="replace") as file: - output = file.read() + with open(self._stdout_file, "rb") as file: + output = file.read().decode("utf-8", errors="replace") except OSError as e: log.warn("could not read {}: {}".format(self._stdout_file, e)) return output diff --git a/gns3server/modules/dynamips/nodes/router.py b/gns3server/modules/dynamips/nodes/router.py index 1875ab22..77045290 100644 --- a/gns3server/modules/dynamips/nodes/router.py +++ b/gns3server/modules/dynamips/nodes/router.py @@ -1396,7 +1396,7 @@ class Router(BaseVM): startup_config_path = os.path.join(module_workdir, "configs", "i{}_startup-config.cfg".format(self._dynamips_id)) if os.path.isfile(startup_config_path): try: - with open(startup_config_path, "r+", errors="replace") as f: + with open(startup_config_path, "r+", encoding="utf-8", errors="replace") as f: old_config = f.read() new_config = old_config.replace(self.name, new_name) f.seek(0) @@ -1409,7 +1409,7 @@ class Router(BaseVM): private_config_path = os.path.join(module_workdir, "configs", "i{}_private-config.cfg".format(self._dynamips_id)) if os.path.isfile(private_config_path): try: - with open(private_config_path, "r+", errors="replace") as f: + with open(private_config_path, "r+", encoding="utf-8", errors="replace") as f: old_config = f.read() new_config = old_config.replace(self.name, new_name) f.seek(0) @@ -1484,7 +1484,7 @@ class Router(BaseVM): startup_config_base64, private_config_base64 = yield from self.extract_config() if startup_config_base64: try: - config = base64.b64decode(startup_config_base64).decode(errors='replace') + config = base64.b64decode(startup_config_base64).decode("utf-8", errors="replace") config = "!\n" + config.replace("\r", "") config_path = os.path.join(module_workdir, self.startup_config) with open(config_path, "wb") as f: @@ -1495,7 +1495,7 @@ class Router(BaseVM): if private_config_base64: try: - config = base64.b64decode(private_config_base64).decode(errors='replace') + config = base64.b64decode(private_config_base64).decode("utf-8", errors="replace") config = "!\n" + config.replace("\r", "") config_path = os.path.join(module_workdir, self.private_config) with open(config_path, "wb") as f: diff --git a/gns3server/modules/iou/iou_vm.py b/gns3server/modules/iou/iou_vm.py index 3e0dc064..81c9dd37 100644 --- a/gns3server/modules/iou/iou_vm.py +++ b/gns3server/modules/iou/iou_vm.py @@ -332,8 +332,8 @@ class IOUVM(BaseVM): def iourc_content(self): try: - with open(os.path.join(self.temporary_directory, "iourc")) as f: - return f.read() + with open(os.path.join(self.temporary_directory, "iourc"), "rb") as f: + return f.read().decode("utf-8") except OSError: return None @@ -343,8 +343,8 @@ class IOUVM(BaseVM): if value is not None: path = os.path.join(self.temporary_directory, "iourc") try: - with open(path, "w+") as f: - f.write(value) + with open(path, "wb+") as f: + f.write(value.encode("utf-8")) except OSError as e: raise IOUError("Could not write the iourc file {}: {}".format(path, e)) @@ -378,7 +378,7 @@ class IOUVM(BaseVM): config = configparser.ConfigParser() try: - with open(self.iourc_path) as f: + with open(self.iourc_path, encoding="utf-8") as f: config.read_file(f) except OSError as e: raise IOUError("Could not open iourc file {}: {}".format(self.iourc_path, e)) @@ -455,7 +455,7 @@ class IOUVM(BaseVM): log.info("Starting IOU: {}".format(self._command)) self._iou_stdout_file = os.path.join(self.working_dir, "iou.log") log.info("Logging to {}".format(self._iou_stdout_file)) - with open(self._iou_stdout_file, "w") as fd: + with open(self._iou_stdout_file, "w", encoding="utf-8") as fd: self._iou_process = yield from asyncio.create_subprocess_exec(*self._command, stdout=fd, stderr=subprocess.STDOUT, @@ -499,7 +499,7 @@ class IOUVM(BaseVM): log.info("starting iouyap: {}".format(command)) self._iouyap_stdout_file = os.path.join(self.working_dir, "iouyap.log") log.info("logging to {}".format(self._iouyap_stdout_file)) - with open(self._iouyap_stdout_file, "w") as fd: + with open(self._iouyap_stdout_file, "w", encoding="utf-8") as fd: self._iouyap_process = yield from asyncio.create_subprocess_exec(*command, stdout=fd, stderr=subprocess.STDOUT, @@ -565,7 +565,7 @@ class IOUVM(BaseVM): bay_id += 1 try: - with open(iouyap_ini, "w") as config_file: + with open(iouyap_ini, "w", encoding="utf-8") as config_file: config.write(config_file) log.info("IOU {name} [id={id}]: iouyap.ini updated".format(name=self._name, id=self._id)) @@ -671,7 +671,7 @@ class IOUVM(BaseVM): netmap_path = os.path.join(self.working_dir, "NETMAP") try: - with open(netmap_path, "w") as f: + with open(netmap_path, "w", encoding="utf-8") as f: for bay in range(0, 16): for unit in range(0, 4): f.write("{iouyap_id}:{bay}/{unit}{iou_id:>5d}:{bay}/{unit}\n".format(iouyap_id=str(self.application_id + 512), @@ -741,8 +741,8 @@ class IOUVM(BaseVM): output = "" if self._iou_stdout_file: try: - with open(self._iou_stdout_file, errors="replace") as file: - output = file.read() + with open(self._iou_stdout_file, "rb") as file: + output = file.read().decode("utf-8", errors="replace") except OSError as e: log.warn("could not read {}: {}".format(self._iou_stdout_file, e)) return output @@ -756,8 +756,8 @@ class IOUVM(BaseVM): output = "" if self._iouyap_stdout_file: try: - with open(self._iouyap_stdout_file, errors="replace") as file: - output = file.read() + with open(self._iouyap_stdout_file, "rb") as file: + output = file.read().decode("utf-8", errors="replace") except OSError as e: log.warn("could not read {}: {}".format(self._iouyap_stdout_file, e)) return output @@ -949,8 +949,8 @@ class IOUVM(BaseVM): return None try: - with open(config_file) as f: - return f.read() + with open(config_file, "rb") as f: + return f.read().decode("utf-8", errors="replace") except OSError as e: raise IOUError("Can't read configuration file '{}': {}".format(config_file, e)) @@ -972,7 +972,7 @@ class IOUVM(BaseVM): if len(initial_config) == 0 and os.path.exists(script_file): return - with open(script_file, 'w+') as f: + with open(script_file, "w+", encoding="utf-8") as f: if len(initial_config) == 0: f.write('') else: diff --git a/gns3server/modules/qemu/qemu_vm.py b/gns3server/modules/qemu/qemu_vm.py index 420bd5e0..58febeb7 100644 --- a/gns3server/modules/qemu/qemu_vm.py +++ b/gns3server/modules/qemu/qemu_vm.py @@ -574,7 +574,7 @@ class QemuVM(BaseVM): log.info("Starting QEMU: {}".format(self._command)) self._stdout_file = os.path.join(self.working_dir, "qemu.log") log.info("logging to {}".format(self._stdout_file)) - with open(self._stdout_file, "w") as fd: + with open(self._stdout_file, "w", encoding="utf-8") as fd: self._process = yield from asyncio.create_subprocess_exec(*self._command, stdout=fd, stderr=subprocess.STDOUT, @@ -817,8 +817,8 @@ class QemuVM(BaseVM): output = "" if self._stdout_file: try: - with open(self._stdout_file, errors="replace") as file: - output = file.read() + with open(self._stdout_file, "rb") as file: + output = file.read().decode("utf-8", errors="replace") except OSError as e: log.warn("Could not read {}: {}".format(self._stdout_file, e)) return output diff --git a/gns3server/modules/virtualbox/virtualbox_vm.py b/gns3server/modules/virtualbox/virtualbox_vm.py index 905a2edc..ed1580ab 100644 --- a/gns3server/modules/virtualbox/virtualbox_vm.py +++ b/gns3server/modules/virtualbox/virtualbox_vm.py @@ -276,7 +276,7 @@ class VirtualBoxVM(BaseVM): hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json") try: - with open(hdd_info_file, "r") as f: + with open(hdd_info_file, "r", encoding="utf-8") as f: hdd_table = json.load(f) except OSError as e: raise VirtualBoxError("Could not read HDD info file: {}".format(e)) @@ -354,7 +354,7 @@ class VirtualBoxVM(BaseVM): if hdd_table: try: hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json") - with open(hdd_info_file, "w") as f: + with open(hdd_info_file, "w", encoding="utf-8") as f: json.dump(hdd_table, f, indent=4) except OSError as e: log.warning("VirtualBox VM '{name}' [{id}] could not write HHD info file: {error}".format(name=self.name, diff --git a/gns3server/modules/vpcs/vpcs_vm.py b/gns3server/modules/vpcs/vpcs_vm.py index 4475b97c..b96810bb 100644 --- a/gns3server/modules/vpcs/vpcs_vm.py +++ b/gns3server/modules/vpcs/vpcs_vm.py @@ -166,8 +166,8 @@ class VPCSVM(BaseVM): return None try: - with open(script_file) as f: - return f.read() + with open(script_file, "rb") as f: + return f.read().decode("utf-8", errors="replace") except OSError as e: raise VPCSError('Cannot read the startup script file "{}": {}'.format(script_file, e)) @@ -181,7 +181,7 @@ class VPCSVM(BaseVM): try: script_file = os.path.join(self.working_dir, 'startup.vpc') - with open(script_file, 'w+') as f: + with open(script_file, "w+", encoding="utf-8") as f: if startup_script is None: f.write('') else: @@ -226,7 +226,7 @@ class VPCSVM(BaseVM): flags = 0 if sys.platform.startswith("win32"): flags = subprocess.CREATE_NEW_PROCESS_GROUP - with open(self._vpcs_stdout_file, "w") as fd: + with open(self._vpcs_stdout_file, "w", encoding="utf-8") as fd: self._process = yield from asyncio.create_subprocess_exec(*self._command, stdout=fd, stderr=subprocess.STDOUT, @@ -290,8 +290,8 @@ class VPCSVM(BaseVM): output = "" if self._vpcs_stdout_file: try: - with open(self._vpcs_stdout_file, errors="replace") as file: - output = file.read() + with open(self._vpcs_stdout_file, "rb") as file: + output = file.read().decode("utf-8", errors="replace") except OSError as e: log.warn("Could not read {}: {}".format(self._vpcs_stdout_file, e)) return output diff --git a/gns3server/web/route.py b/gns3server/web/route.py index 2f8fd0c9..875298f5 100644 --- a/gns3server/web/route.py +++ b/gns3server/web/route.py @@ -131,7 +131,7 @@ class Route(object): record_file = server_config.get("record") if record_file: try: - with open(record_file, "a") as f: + with open(record_file, "a", encoding="utf-8") as f: f.write("curl -X {} 'http://{}{}' -d '{}'".format(request.method, request.host, request.path_qs, json.dumps(request.json))) f.write("\n") except OSError as e: