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

Set a location by default

This commit is contained in:
Julien Duponchelle 2015-01-23 17:39:17 +01:00
parent 7bed9f56bc
commit 8e249b670d
4 changed files with 52 additions and 2 deletions

View File

@ -122,6 +122,17 @@ class Config(object):
return self._config["DEFAULT"] return self._config["DEFAULT"]
return self._config[section] return self._config[section]
def set_section_config(self, section, content):
"""
Set a specific configuration section. It's not
dumped on the disk.
:param section: Section name
:param content: A dictonary with section content
"""
self._config[section] = content
@staticmethod @staticmethod
def instance(): def instance():
""" """

View File

@ -44,11 +44,11 @@ class Project:
raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(uuid)) raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(uuid))
self._uuid = uuid self._uuid = uuid
config = Config.instance().get_section_config("Server")
self._location = location self._location = location
if location is None: if location is None:
self._location = tempfile.mkdtemp() self._location = config.get("project_directory", self._get_default_project_directory())
else: else:
config = Config.instance().get_section_config("Server")
if config.get("local", False) is False: if config.get("local", False) is False:
raise aiohttp.web.HTTPForbidden(text="You are not allowed to modifiy the project directory location") raise aiohttp.web.HTTPForbidden(text="You are not allowed to modifiy the project directory location")
@ -61,6 +61,19 @@ class Project:
except OSError as e: except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e)) raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
def _get_default_project_directory(self):
"""
Return the default location for the project directory
depending of the operating system
"""
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
try:
os.makedirs(path, exist_ok=True)
except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
return path
@property @property
def uuid(self): def uuid(self):

View File

@ -18,7 +18,11 @@
import pytest import pytest
import socket import socket
import asyncio import asyncio
import tempfile
import shutil
from aiohttp import web from aiohttp import web
from gns3server.config import Config
from gns3server.web.route import Route from gns3server.web.route import Route
# TODO: get rid of * # TODO: get rid of *
from gns3server.handlers import * from gns3server.handlers import *
@ -100,3 +104,17 @@ def free_console_port(request, port_manager):
# the test do whatever the test want # the test do whatever the test want
port_manager.release_console_port(port) port_manager.release_console_port(port)
return port return port
@pytest.yield_fixture(autouse=True)
def run_around_tests():
tmppath = tempfile.mkdtemp()
config = Config.instance()
server_section = config.get_section_config("Server")
server_section["project_directory"] = tmppath
config.set_section_config("Server", server_section)
yield
shutil.rmtree(tmppath)

View File

@ -130,3 +130,11 @@ def test_project_close_temporary_project(manager):
assert os.path.exists(directory) assert os.path.exists(directory)
project.close() project.close()
assert os.path.exists(directory) is False assert os.path.exists(directory) is False
def test_get_default_project_directory():
project = Project()
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
assert project._get_default_project_directory() == path
assert os.path.exists(path)