1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-25 00:08:11 +00:00

Cleanup the temporary project after modules have been notified of the

path change
This commit is contained in:
Julien Duponchelle 2015-05-26 15:20:14 +02:00
parent 528bb7a7c6
commit c98bcedd39
4 changed files with 40 additions and 12 deletions

View File

@ -84,9 +84,11 @@ class ProjectHandler:
project.name = request.json.get("name", project.name) project.name = request.json.get("name", project.name)
project_path = request.json.get("path", project.path) project_path = request.json.get("path", project.path)
if project_path != project.path: if project_path != project.path:
old_path = project.path
project.path = project_path project.path = project_path
for module in MODULES: for module in MODULES:
yield from module.instance().project_moved(project) yield from module.instance().project_moved(project)
yield from project.clean_old_path(old_path)
# Very important we need to remove temporary flag after moving the project # Very important we need to remove temporary flag after moving the project
project.temporary = request.json.get("temporary", project.temporary) project.temporary = request.json.get("temporary", project.temporary)
response.json(project) response.json(project)

View File

@ -146,11 +146,15 @@ class Project:
self._path = path self._path = path
self._update_temporary_file() self._update_temporary_file()
# The order of operation is important because we want to avoid losing @asyncio.coroutine
# data def clean_old_path(self, old_path):
if old_path: """
Called after a project location change. All the modules should
have been notified before
"""
if self._temporary:
try: try:
shutil.rmtree(old_path) yield from wait_run_in_executor(shutil.rmtree, old_path)
except OSError as e: except OSError as e:
log.warn("Can't remove temporary directory {}: {}".format(old_path, e)) log.warn("Can't remove temporary directory {}: {}".format(old_path, e))

View File

@ -20,6 +20,7 @@ This test suite check /project endpoint
""" """
import uuid import uuid
import os
from unittest.mock import patch from unittest.mock import patch
from tests.utils import asyncio_patch from tests.utils import asyncio_patch
@ -85,18 +86,43 @@ def test_update_temporary_project(server):
assert response.json["temporary"] is False assert response.json["temporary"] is False
def test_update_path_project(server, tmpdir): def test_update_path_project_temporary(server, tmpdir):
os.makedirs(str(tmpdir / "a"))
os.makedirs(str(tmpdir / "b"))
with patch("gns3server.modules.project.Project.is_local", return_value=True): with patch("gns3server.modules.project.Project.is_local", return_value=True):
response = server.post("/projects", {"name": "first_name"}) response = server.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "temporary": True})
assert response.status == 201 assert response.status == 201
assert response.json["name"] == "first_name" assert response.json["name"] == "first_name"
query = {"name": "second_name", "path": str(tmpdir)} query = {"name": "second_name", "path": str(tmpdir / "b")}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True) response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200 assert response.status == 200
assert response.json["path"] == str(tmpdir) assert response.json["path"] == str(tmpdir / "b")
assert response.json["name"] == "second_name" assert response.json["name"] == "second_name"
assert not os.path.exists(str(tmpdir / "a"))
assert os.path.exists(str(tmpdir / "b"))
def test_update_path_project_non_temporary(server, tmpdir):
os.makedirs(str(tmpdir / "a"))
os.makedirs(str(tmpdir / "b"))
with patch("gns3server.modules.project.Project.is_local", return_value=True):
response = server.post("/projects", {"name": "first_name", "path": str(tmpdir / "a")})
assert response.status == 201
assert response.json["name"] == "first_name"
query = {"name": "second_name", "path": str(tmpdir / "b")}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["path"] == str(tmpdir / "b")
assert response.json["name"] == "second_name"
assert os.path.exists(str(tmpdir / "a"))
assert os.path.exists(str(tmpdir / "b"))
def test_update_path_project_non_local(server, tmpdir): def test_update_path_project_non_local(server, tmpdir):

View File

@ -73,10 +73,6 @@ def test_changing_path_temporary_flag(tmpdir):
assert os.path.exists(os.path.join(p.path, ".gns3_temporary")) assert os.path.exists(os.path.join(p.path, ".gns3_temporary"))
p.path = str(tmpdir) 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(): def test_temporary_path():