From 872515fa787bad79431cda5f3a0a68884c2fedbb Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 12 Apr 2014 16:46:02 -0600 Subject: [PATCH] Use the FileExistsError exception. --- gns3server/handlers/file_upload_handler.py | 13 +++++----- gns3server/modules/dynamips/__init__.py | 24 ++++++++++--------- .../modules/dynamips/hypervisor_manager.py | 7 ------ gns3server/modules/iou/__init__.py | 16 +++++++------ gns3server/modules/iou/iou_device.py | 11 +++++---- gns3server/server.py | 13 +++++----- 6 files changed, 42 insertions(+), 42 deletions(-) diff --git a/gns3server/handlers/file_upload_handler.py b/gns3server/handlers/file_upload_handler.py index 93f5b031..edaf9488 100644 --- a/gns3server/handlers/file_upload_handler.py +++ b/gns3server/handlers/file_upload_handler.py @@ -45,12 +45,13 @@ class FileUploadHandler(tornado.web.RequestHandler): # default projects directory is "~/Documents/GNS3/images" self._upload_dir = os.path.expandvars(os.path.expanduser(server_config.get("upload_directory", "~/Documents/GNS3/images"))) - if not os.path.isdir(self._upload_dir): - try: - os.makedirs(self._upload_dir) - log.info("upload directory '{}' created".format(self._upload_dir)) - except OSError as e: - log.error("could not create the upload directory {}: {}".format(self._upload_dir, e)) + try: + os.makedirs(self._upload_dir) + log.info("upload directory '{}' created".format(self._upload_dir)) + except FileExistsError: + pass + except OSError as e: + log.error("could not create the upload directory {}: {}".format(self._upload_dir, e)) tornado.websocket.WebSocketHandler.__init__(self, application, request) diff --git a/gns3server/modules/dynamips/__init__.py b/gns3server/modules/dynamips/__init__.py index 0ef7dc23..8bced160 100644 --- a/gns3server/modules/dynamips/__init__.py +++ b/gns3server/modules/dynamips/__init__.py @@ -205,11 +205,12 @@ class Dynamips(IModule): if not os.access(self._dynamips, os.X_OK): raise DynamipsError("Dynamips {} is not executable".format(self._dynamips)) - if not os.path.isdir(self._working_dir): - try: - os.makedirs(self._working_dir) - except OSError as e: - raise DynamipsError("Could not create working directory {}".format(e)) + try: + os.makedirs(self._working_dir) + except FileExistsError: + pass + except OSError as e: + raise DynamipsError("Could not create working directory {}".format(e)) # check if the working directory is writable if not os.access(self._working_dir, os.W_OK): @@ -248,7 +249,7 @@ class Dynamips(IModule): self._dynamips = request.pop("path") if "working_dir" in request: - self._working_dir = request.pop("working_dir") + self._working_dir = os.path.join(request.pop("working_dir"), "dynamips") log.info("this server is local") else: self._remote_server = True @@ -407,11 +408,12 @@ class Dynamips(IModule): config = "!\n" + config.replace("\r", "") config = config.replace('%h', router.name) config_dir = os.path.join(router.hypervisor.working_dir, "configs") - if not os.path.isdir(config_dir): - try: - os.makedirs(config_dir) - except OSError as e: - raise DynamipsError("Could not create configs directory: {}".format(e)) + try: + os.makedirs(config_dir) + except FileExistsError: + pass + except OSError as e: + raise DynamipsError("Could not create configs directory: {}".format(e)) config_path = os.path.join(config_dir, config_filename) try: with open(config_path, "w") as f: diff --git a/gns3server/modules/dynamips/hypervisor_manager.py b/gns3server/modules/dynamips/hypervisor_manager.py index fead21f2..d016afb4 100644 --- a/gns3server/modules/dynamips/hypervisor_manager.py +++ b/gns3server/modules/dynamips/hypervisor_manager.py @@ -461,13 +461,6 @@ class HypervisorManager(object): """ port = self.allocate_tcp_port() -# working_dir = os.path.join(self._working_dir, "instance-{}".format(port)) -# if not os.path.isdir(working_dir): -# try: -# os.makedirs(working_dir) -# except OSError as e: -# raise DynamipsError("{}".format(e)) - hypervisor = Hypervisor(self._path, self._working_dir, self._host, diff --git a/gns3server/modules/iou/__init__.py b/gns3server/modules/iou/__init__.py index af0198b1..c67a9ff3 100644 --- a/gns3server/modules/iou/__init__.py +++ b/gns3server/modules/iou/__init__.py @@ -201,8 +201,9 @@ class IOU(IModule): log.info("iouyap path set to {}".format(self._iouyap)) if "working_dir" in request: - if self._working_dir != request["working_dir"]: - self._working_dir = request["working_dir"] + new_working_dir = os.path.join(request["working_dir"], "iou") + if self._working_dir != new_working_dir: + self._working_dir = new_working_dir log.info("this server is local with working directory path to {}".format(self._working_dir)) for iou_id in self._iou_instances: iou_instance = self._iou_instances[iou_id] @@ -246,11 +247,12 @@ class IOU(IModule): iou_path = request["path"] try: - if not os.path.isdir(self._working_dir): - try: - os.makedirs(self._working_dir) - except OSError as e: - raise IOUError("Could not create working directory {}".format(e)) + try: + os.makedirs(self._working_dir) + except FileExistsError: + pass + except OSError as e: + raise IOUError("Could not create working directory {}".format(e)) iou_instance = IOUDevice(iou_path, self._working_dir, host=self._host, name=name) # find a console port diff --git a/gns3server/modules/iou/iou_device.py b/gns3server/modules/iou/iou_device.py index 6a53039d..0df1e3d3 100644 --- a/gns3server/modules/iou/iou_device.py +++ b/gns3server/modules/iou/iou_device.py @@ -246,11 +246,12 @@ class IOUDevice(object): # create our own working directory working_dir = os.path.join(working_dir, "device-{}".format(self._id)) - if not os.path.isdir(working_dir): - try: - os.makedirs(working_dir) - except OSError as e: - raise IOUError("Could not create working directory {}: {}".format(working_dir, e)) + try: + os.makedirs(working_dir) + except FileExistsError: + pass + except OSError as e: + raise IOUError("Could not create working directory {}: {}".format(working_dir, e)) self._working_dir = working_dir log.info("IOU {name} [id={id}]: working directory changed to {wd}".format(name=self._name, diff --git a/gns3server/server.py b/gns3server/server.py index 4919a46f..0904f705 100644 --- a/gns3server/server.py +++ b/gns3server/server.py @@ -78,12 +78,13 @@ class Server(object): self._projects_dir = os.path.expandvars(os.path.expanduser(server_config.get("projects_directory", "~/Documents/GNS3/projects"))) self._temp_dir = server_config.get("temporary_directory", tempfile.gettempdir()) - if not os.path.isdir(self._projects_dir): - try: - os.makedirs(self._projects_dir) - log.info("projects directory '{}' created".format(self._projects_dir)) - except OSError as e: - log.error("could not create the projects directory {}: {}".format(self._projects_dir, e)) + try: + os.makedirs(self._projects_dir) + log.info("projects directory '{}' created".format(self._projects_dir)) + except FileExistsError: + pass + except OSError as e: + log.error("could not create the projects directory {}: {}".format(self._projects_dir, e)) def load_modules(self): """