From cd393491d51490f6a2f311cebc37ae0a01b1d2a7 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Mon, 11 Apr 2016 17:16:06 +0200 Subject: [PATCH] At export use only relative image path Fix https://github.com/GNS3/gns3-gui/issues/1176 --- gns3server/modules/project.py | 20 ++++++++++++++++++- tests/modules/test_project.py | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index e774666f..26b498ca 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -539,7 +539,7 @@ class Project: path = os.path.join(root, file) # We rename the .gns3 project.gns3 to avoid the task to the client to guess the file name if file.endswith(".gns3"): - z.write(path, "project.gns3") + z.writestr("project.gns3", self._export_project_file(path)) else: # We merge the data from all server in the same project-files directory vm_directory = os.path.join(self._path, "servers", "vm") @@ -549,6 +549,24 @@ class Project: z.write(path, os.path.relpath(path, self._path)) return z + def _export_project_file(self, path): + """ + Take a project file (.gns3) and patch it for the export + + :returns: Content of the topology + """ + + with open(path) as f: + topology = json.load(f) + if "topology" in topology and "nodes" in topology["topology"]: + for node in topology["topology"]["nodes"]: + if "properties" in node: + for prop, value in node["properties"].items(): + if prop.endswith("image"): + node["properties"][prop] = os.path.basename(value) + + return json.dumps(topology).encode() + def import_zip(self, stream, gns3vm=True): """ Import a project contain in a zip file diff --git a/tests/modules/test_project.py b/tests/modules/test_project.py index c94ee850..43224a35 100644 --- a/tests/modules/test_project.py +++ b/tests/modules/test_project.py @@ -297,7 +297,41 @@ def test_export(tmpdir): assert 'vm-1/dynamips/test_log.txt' not in myzip.namelist() -def test_export(tmpdir): +def test_export_fix_path(tmpdir): + """ + Fix absolute image path + """ + project = Project() + path = project.path + + topology = { + "topology": { + "nodes": [ + { + "properties": { + "image": "/tmp/c3725-adventerprisek9-mz.124-25d.image" + } + } + ] + } + } + + with open(os.path.join(path, "test.gns3"), 'w+') as f: + json.dump(topology, f) + + z = project.export() + with open(str(tmpdir / 'zipfile.zip'), 'wb') as f: + for data in z: + f.write(data) + + with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip: + with myzip.open("project.gns3") as myfile: + content = myfile.read().decode() + topology = json.loads(content) + assert topology["topology"]["nodes"][0]["properties"]["image"] == "c3725-adventerprisek9-mz.124-25d.image" + + +def test_export_with_vm(tmpdir): project = Project() path = project.path os.makedirs(os.path.join(path, "vm-1", "dynamips"))