From d2d91ebdea0e44019beeb61b81842fc71248d14e Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 4 May 2015 14:04:57 +0200 Subject: [PATCH] Fix temporary project not cleanup with save as --- gns3server/handlers/api/project_handler.py | 3 ++- gns3server/modules/project.py | 13 +++++++++++++ tests/modules/test_project.py | 9 ++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/gns3server/handlers/api/project_handler.py b/gns3server/handlers/api/project_handler.py index e31d32bc..a67d9c57 100644 --- a/gns3server/handlers/api/project_handler.py +++ b/gns3server/handlers/api/project_handler.py @@ -81,13 +81,14 @@ class ProjectHandler: pm = ProjectManager.instance() project = pm.get_project(request.match_info["project_id"]) - project.temporary = request.json.get("temporary", project.temporary) project.name = request.json.get("name", project.name) project_path = request.json.get("path", project.path) if project_path != project.path: project.path = project_path for module in MODULES: yield from module.instance().project_moved(project) + # Very important we need to remove temporary flag after moving the project + project.temporary = request.json.get("temporary", project.temporary) response.json(project) @classmethod diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index edb1b6cf..cc2215d9 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -139,9 +139,22 @@ class Project: if path != self._path and self.is_local() is False: raise aiohttp.web.HTTPForbidden(text="You are not allowed to modify the project directory location") + old_path = None + if hasattr(self, "_path"): + old_path = self._path + self._path = path self._update_temporary_file() + # The order of operation is important because we want to avoid losing + # data + if old_path: + try: + shutil.rmtree(old_path) + except OSError as e: + raise aiohttp.web.HTTPConflict("Can't remove temporary directory {}: {}".format(old_path ,e)) + + @property def name(self): diff --git a/tests/modules/test_project.py b/tests/modules/test_project.py index 60ec1765..c4eaee4b 100644 --- a/tests/modules/test_project.py +++ b/tests/modules/test_project.py @@ -69,15 +69,14 @@ def test_changing_path_temporary_flag(tmpdir): with patch("gns3server.modules.project.Project.is_local", return_value=True): p = Project(temporary=True) assert os.path.exists(p.path) + original_path = p.path assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) - p.temporary = False - assert not os.path.exists(os.path.join(p.path, ".gns3_temporary")) - - with open(str(tmpdir / ".gns3_temporary"), "w+") as f: - f.write("1") p.path = str(tmpdir) + p.temporary = False + assert not os.path.exists(os.path.join(p.path, ".gns3_temporary")) assert not os.path.exists(os.path.join(str(tmpdir), ".gns3_temporary")) + assert not os.path.exists(original_path) def test_temporary_path():