Non blocking project exportation.

This commit is contained in:
grossmj committed 2019-02-26 15:55:07 +07:00
1 parent 135d56371d
commit a8990c9e89
5 files changed
+459 -21

No files matched your search

+4 -8
View File
@@ -22,7 +22,6 @@ import asyncio
import aiohttp
import zipfile
import tempfile
import zipstream
from datetime import datetime
@@ -30,7 +29,7 @@ import logging
log = logging.getLogger(__name__)
async def export_project(project, temporary_dir, include_images=False, keep_compute_id=False, allow_all_nodes=False, reset_mac_addresses=False):
async def export_project(zstream, project, temporary_dir, include_images=False, keep_compute_id=False, allow_all_nodes=False, reset_mac_addresses=False):
"""
Export a project to a zip file.
@@ -53,8 +52,6 @@ async def export_project(project, temporary_dir, include_images=False, keep_comp
# Make sure we save the project
project.dump()
zstream = zipstream.ZipFile(allowZip64=True)
if not os.path.exists(project._path):
raise aiohttp.web.HTTPNotFound(text="Project could not be found at '{}'".format(project._path))
@@ -80,7 +77,7 @@ async def export_project(project, temporary_dir, include_images=False, keep_comp
if file.endswith(".gns3"):
continue
_patch_mtime(path)
zstream.write(path, os.path.relpath(path, project._path), compress_type=zipfile.ZIP_DEFLATED)
zstream.write(path, os.path.relpath(path, project._path))
# Export files from remote computes
downloaded_files = set()
@@ -103,11 +100,9 @@ async def export_project(project, temporary_dir, include_images=False, keep_comp
response.close()
f.close()
_patch_mtime(temp_path)
zstream.write(temp_path, arcname=compute_file["path"], compress_type=zipfile.ZIP_DEFLATED)
zstream.write(temp_path, arcname=compute_file["path"])
downloaded_files.add(compute_file['path'])
return zstream
def _patch_mtime(path):
"""
@@ -232,6 +227,7 @@ async def _patch_project_file(project, path, zstream, include_images, keep_compu
zstream.writestr("project.gns3", json.dumps(topology).encode())
return images
def _export_local_image(image, zstream):
"""
Exports a local image to the zip file.
+6 -3
View File
@@ -24,6 +24,7 @@ import shutil
import asyncio
import aiohttp
import tempfile
import zipfile
from uuid import UUID, uuid4
@@ -38,6 +39,7 @@ from ..utils.path import check_path_allowed, get_default_project_directory
from ..utils.asyncio.pool import Pool
from ..utils.asyncio import locking
from ..utils.asyncio import wait_run_in_executor
from ..utils.asyncio import aiozipstream
from .export_project import export_project
from .import_project import import_project
@@ -976,9 +978,10 @@ class Project:
assert self._status != "closed"
try:
with tempfile.TemporaryDirectory() as tmpdir:
zipstream = await export_project(self, tmpdir, keep_compute_id=True, allow_all_nodes=True, reset_mac_addresses=True)
project_path = os.path.join(tmpdir, "project.gns3p")
await wait_run_in_executor(self._create_duplicate_project_file, project_path, zipstream)
with aiozipstream.ZipFile(compression=zipfile.ZIP_STORED) as zstream:
zipstream = await export_project(zstream, self, tmpdir, keep_compute_id=True, allow_all_nodes=True, reset_mac_addresses=True)
project_path = os.path.join(tmpdir, "project.gns3p")
await wait_run_in_executor(self._create_duplicate_project_file, project_path, zipstream)
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)
except (ValueError, OSError, UnicodeEncodeError) as e: