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

Project duplication support.

This commit is contained in:
grossmj 2019-02-27 17:57:07 +07:00
parent a8990c9e89
commit 52bfa636c1
2 changed files with 18 additions and 15 deletions

View File

@ -21,8 +21,10 @@ import json
import uuid import uuid
import copy import copy
import shutil import shutil
import time
import asyncio import asyncio
import aiohttp import aiohttp
import aiofiles
import tempfile import tempfile
import zipfile import zipfile
@ -949,15 +951,6 @@ class Project:
while self._loading: while self._loading:
await asyncio.sleep(0.5) await asyncio.sleep(0.5)
def _create_duplicate_project_file(self, path, zipstream):
"""
Creates the project file (to be run in its own thread)
"""
with open(path, "wb") as f:
for data in zipstream:
f.write(data)
async def duplicate(self, name=None, location=None): async def duplicate(self, name=None, location=None):
""" """
Duplicate a project Duplicate a project
@ -977,13 +970,23 @@ class Project:
self.dump() self.dump()
assert self._status != "closed" assert self._status != "closed"
try: try:
begin = time.time()
with tempfile.TemporaryDirectory() as tmpdir: with tempfile.TemporaryDirectory() as tmpdir:
with aiozipstream.ZipFile(compression=zipfile.ZIP_STORED) as zstream: with aiozipstream.ZipFile(compression=zipfile.ZIP_DEFLATED) as zstream:
zipstream = await export_project(zstream, self, tmpdir, keep_compute_id=True, allow_all_nodes=True, reset_mac_addresses=True) await export_project(zstream, self, tmpdir, keep_compute_id=True, allow_all_nodes=True, reset_mac_addresses=True)
# export the project to a temporary location
project_path = os.path.join(tmpdir, "project.gns3p") project_path = os.path.join(tmpdir, "project.gns3p")
await wait_run_in_executor(self._create_duplicate_project_file, project_path, zipstream) log.info("Exporting project to '{}'".format(project_path))
async with aiofiles.open(project_path, 'wb') as f:
async for chunk in zstream:
await f.write(chunk)
# import the temporary project
with open(project_path, "rb") as f: with open(project_path, "rb") as f:
project = await import_project(self._controller, str(uuid.uuid4()), f, location=location, name=name, keep_compute_id=True) project = await import_project(self._controller, str(uuid.uuid4()), f, location=location, name=name, keep_compute_id=True)
log.info("Project '{}' duplicated in {:.4f} seconds".format(project.id, time.time() - begin))
except (ValueError, OSError, UnicodeEncodeError) as e: except (ValueError, OSError, UnicodeEncodeError) as e:
raise aiohttp.web.HTTPConflict(text="Cannot duplicate project: {}".format(str(e))) raise aiohttp.web.HTTPConflict(text="Cannot duplicate project: {}".format(str(e)))