1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-28 11:18:11 +00:00

Explicit utf-8 encoding where necessary to avoid Unicode errors on Windows (we require/set an utf-8 locale on other systems).

This commit is contained in:
grossmj 2015-04-25 11:58:34 -06:00
parent d68bf1c263
commit 80a0e0ebf7
7 changed files with 34 additions and 34 deletions

View File

@ -158,8 +158,8 @@ class Hypervisor(DynamipsHypervisor):
output = "" output = ""
if self._stdout_file and os.access(self._stdout_file, os.R_OK): if self._stdout_file and os.access(self._stdout_file, os.R_OK):
try: try:
with open(self._stdout_file, errors="replace") as file: with open(self._stdout_file, "rb") as file:
output = file.read() output = file.read().decode("utf-8", errors="replace")
except OSError as e: except OSError as e:
log.warn("could not read {}: {}".format(self._stdout_file, e)) log.warn("could not read {}: {}".format(self._stdout_file, e))
return output return output

View File

@ -1396,7 +1396,7 @@ class Router(BaseVM):
startup_config_path = os.path.join(module_workdir, "configs", "i{}_startup-config.cfg".format(self._dynamips_id)) startup_config_path = os.path.join(module_workdir, "configs", "i{}_startup-config.cfg".format(self._dynamips_id))
if os.path.isfile(startup_config_path): if os.path.isfile(startup_config_path):
try: 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() old_config = f.read()
new_config = old_config.replace(self.name, new_name) new_config = old_config.replace(self.name, new_name)
f.seek(0) 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)) private_config_path = os.path.join(module_workdir, "configs", "i{}_private-config.cfg".format(self._dynamips_id))
if os.path.isfile(private_config_path): if os.path.isfile(private_config_path):
try: 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() old_config = f.read()
new_config = old_config.replace(self.name, new_name) new_config = old_config.replace(self.name, new_name)
f.seek(0) f.seek(0)
@ -1484,7 +1484,7 @@ class Router(BaseVM):
startup_config_base64, private_config_base64 = yield from self.extract_config() startup_config_base64, private_config_base64 = yield from self.extract_config()
if startup_config_base64: if startup_config_base64:
try: 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 = "!\n" + config.replace("\r", "")
config_path = os.path.join(module_workdir, self.startup_config) config_path = os.path.join(module_workdir, self.startup_config)
with open(config_path, "wb") as f: with open(config_path, "wb") as f:
@ -1495,7 +1495,7 @@ class Router(BaseVM):
if private_config_base64: if private_config_base64:
try: 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 = "!\n" + config.replace("\r", "")
config_path = os.path.join(module_workdir, self.private_config) config_path = os.path.join(module_workdir, self.private_config)
with open(config_path, "wb") as f: with open(config_path, "wb") as f:

View File

@ -332,8 +332,8 @@ class IOUVM(BaseVM):
def iourc_content(self): def iourc_content(self):
try: try:
with open(os.path.join(self.temporary_directory, "iourc")) as f: with open(os.path.join(self.temporary_directory, "iourc"), "rb") as f:
return f.read() return f.read().decode("utf-8")
except OSError: except OSError:
return None return None
@ -343,8 +343,8 @@ class IOUVM(BaseVM):
if value is not None: if value is not None:
path = os.path.join(self.temporary_directory, "iourc") path = os.path.join(self.temporary_directory, "iourc")
try: try:
with open(path, "w+") as f: with open(path, "wb+") as f:
f.write(value) f.write(value.encode("utf-8"))
except OSError as e: except OSError as e:
raise IOUError("Could not write the iourc file {}: {}".format(path, e)) raise IOUError("Could not write the iourc file {}: {}".format(path, e))
@ -378,7 +378,7 @@ class IOUVM(BaseVM):
config = configparser.ConfigParser() config = configparser.ConfigParser()
try: try:
with open(self.iourc_path) as f: with open(self.iourc_path, encoding="utf-8") as f:
config.read_file(f) config.read_file(f)
except OSError as e: except OSError as e:
raise IOUError("Could not open iourc file {}: {}".format(self.iourc_path, 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)) log.info("Starting IOU: {}".format(self._command))
self._iou_stdout_file = os.path.join(self.working_dir, "iou.log") self._iou_stdout_file = os.path.join(self.working_dir, "iou.log")
log.info("Logging to {}".format(self._iou_stdout_file)) 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, self._iou_process = yield from asyncio.create_subprocess_exec(*self._command,
stdout=fd, stdout=fd,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
@ -499,7 +499,7 @@ class IOUVM(BaseVM):
log.info("starting iouyap: {}".format(command)) log.info("starting iouyap: {}".format(command))
self._iouyap_stdout_file = os.path.join(self.working_dir, "iouyap.log") self._iouyap_stdout_file = os.path.join(self.working_dir, "iouyap.log")
log.info("logging to {}".format(self._iouyap_stdout_file)) 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, self._iouyap_process = yield from asyncio.create_subprocess_exec(*command,
stdout=fd, stdout=fd,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
@ -565,7 +565,7 @@ class IOUVM(BaseVM):
bay_id += 1 bay_id += 1
try: try:
with open(iouyap_ini, "w") as config_file: with open(iouyap_ini, "w", encoding="utf-8") as config_file:
config.write(config_file) config.write(config_file)
log.info("IOU {name} [id={id}]: iouyap.ini updated".format(name=self._name, log.info("IOU {name} [id={id}]: iouyap.ini updated".format(name=self._name,
id=self._id)) id=self._id))
@ -671,7 +671,7 @@ class IOUVM(BaseVM):
netmap_path = os.path.join(self.working_dir, "NETMAP") netmap_path = os.path.join(self.working_dir, "NETMAP")
try: 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 bay in range(0, 16):
for unit in range(0, 4): 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), 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 = "" output = ""
if self._iou_stdout_file: if self._iou_stdout_file:
try: try:
with open(self._iou_stdout_file, errors="replace") as file: with open(self._iou_stdout_file, "rb") as file:
output = file.read() output = file.read().decode("utf-8", errors="replace")
except OSError as e: except OSError as e:
log.warn("could not read {}: {}".format(self._iou_stdout_file, e)) log.warn("could not read {}: {}".format(self._iou_stdout_file, e))
return output return output
@ -756,8 +756,8 @@ class IOUVM(BaseVM):
output = "" output = ""
if self._iouyap_stdout_file: if self._iouyap_stdout_file:
try: try:
with open(self._iouyap_stdout_file, errors="replace") as file: with open(self._iouyap_stdout_file, "rb") as file:
output = file.read() output = file.read().decode("utf-8", errors="replace")
except OSError as e: except OSError as e:
log.warn("could not read {}: {}".format(self._iouyap_stdout_file, e)) log.warn("could not read {}: {}".format(self._iouyap_stdout_file, e))
return output return output
@ -949,8 +949,8 @@ class IOUVM(BaseVM):
return None return None
try: try:
with open(config_file) as f: with open(config_file, "rb") as f:
return f.read() return f.read().decode("utf-8", errors="replace")
except OSError as e: except OSError as e:
raise IOUError("Can't read configuration file '{}': {}".format(config_file, 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): if len(initial_config) == 0 and os.path.exists(script_file):
return return
with open(script_file, 'w+') as f: with open(script_file, "w+", encoding="utf-8") as f:
if len(initial_config) == 0: if len(initial_config) == 0:
f.write('') f.write('')
else: else:

View File

@ -574,7 +574,7 @@ class QemuVM(BaseVM):
log.info("Starting QEMU: {}".format(self._command)) log.info("Starting QEMU: {}".format(self._command))
self._stdout_file = os.path.join(self.working_dir, "qemu.log") self._stdout_file = os.path.join(self.working_dir, "qemu.log")
log.info("logging to {}".format(self._stdout_file)) 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, self._process = yield from asyncio.create_subprocess_exec(*self._command,
stdout=fd, stdout=fd,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
@ -817,8 +817,8 @@ class QemuVM(BaseVM):
output = "" output = ""
if self._stdout_file: if self._stdout_file:
try: try:
with open(self._stdout_file, errors="replace") as file: with open(self._stdout_file, "rb") as file:
output = file.read() output = file.read().decode("utf-8", errors="replace")
except OSError as e: except OSError as e:
log.warn("Could not read {}: {}".format(self._stdout_file, e)) log.warn("Could not read {}: {}".format(self._stdout_file, e))
return output return output

View File

@ -276,7 +276,7 @@ class VirtualBoxVM(BaseVM):
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json") hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json")
try: 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) hdd_table = json.load(f)
except OSError as e: except OSError as e:
raise VirtualBoxError("Could not read HDD info file: {}".format(e)) raise VirtualBoxError("Could not read HDD info file: {}".format(e))
@ -354,7 +354,7 @@ class VirtualBoxVM(BaseVM):
if hdd_table: if hdd_table:
try: try:
hdd_info_file = os.path.join(self.working_dir, self._vmname, "hdd_info.json") 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) json.dump(hdd_table, f, indent=4)
except OSError as e: except OSError as e:
log.warning("VirtualBox VM '{name}' [{id}] could not write HHD info file: {error}".format(name=self.name, log.warning("VirtualBox VM '{name}' [{id}] could not write HHD info file: {error}".format(name=self.name,

View File

@ -166,8 +166,8 @@ class VPCSVM(BaseVM):
return None return None
try: try:
with open(script_file) as f: with open(script_file, "rb") as f:
return f.read() return f.read().decode("utf-8", errors="replace")
except OSError as e: except OSError as e:
raise VPCSError('Cannot read the startup script file "{}": {}'.format(script_file, e)) raise VPCSError('Cannot read the startup script file "{}": {}'.format(script_file, e))
@ -181,7 +181,7 @@ class VPCSVM(BaseVM):
try: try:
script_file = os.path.join(self.working_dir, 'startup.vpc') 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: if startup_script is None:
f.write('') f.write('')
else: else:
@ -226,7 +226,7 @@ class VPCSVM(BaseVM):
flags = 0 flags = 0
if sys.platform.startswith("win32"): if sys.platform.startswith("win32"):
flags = subprocess.CREATE_NEW_PROCESS_GROUP 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, self._process = yield from asyncio.create_subprocess_exec(*self._command,
stdout=fd, stdout=fd,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
@ -290,8 +290,8 @@ class VPCSVM(BaseVM):
output = "" output = ""
if self._vpcs_stdout_file: if self._vpcs_stdout_file:
try: try:
with open(self._vpcs_stdout_file, errors="replace") as file: with open(self._vpcs_stdout_file, "rb") as file:
output = file.read() output = file.read().decode("utf-8", errors="replace")
except OSError as e: except OSError as e:
log.warn("Could not read {}: {}".format(self._vpcs_stdout_file, e)) log.warn("Could not read {}: {}".format(self._vpcs_stdout_file, e))
return output return output

View File

@ -131,7 +131,7 @@ class Route(object):
record_file = server_config.get("record") record_file = server_config.get("record")
if record_file: if record_file:
try: 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("curl -X {} 'http://{}{}' -d '{}'".format(request.method, request.host, request.path_qs, json.dumps(request.json)))
f.write("\n") f.write("\n")
except OSError as e: except OSError as e: