|
| 1 | +# coding:utf-8 |
| 2 | + |
| 3 | +from inspect import getfile |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +from tempfile import mktemp, gettempdir |
| 7 | + |
| 8 | +from meshroom.core.graph import Graph, loadGraph |
| 9 | +from meshroom.core import desc, loadAllNodes, registerNodeType, unregisterNodeType |
| 10 | +from meshroom.core.node import Node |
| 11 | +import meshroom.core |
| 12 | + |
| 13 | + |
| 14 | +class RelativePaths(desc.Node): |
| 15 | + # Local "RelativePaths" node, with a different source code folder as the one in nodes/test |
| 16 | + documentation="Test node with filepaths that are set relatively to some variables." |
| 17 | + inputs = [ |
| 18 | + desc.File( |
| 19 | + name="relativePathInput", |
| 20 | + label="Relative Input File", |
| 21 | + description="Relative path to the input file.", |
| 22 | + value="${NODE_SOURCECODE_FOLDER}" + "/input.txt", |
| 23 | + ), |
| 24 | + ] |
| 25 | + |
| 26 | + outputs = [ |
| 27 | + desc.File( |
| 28 | + name="output", |
| 29 | + label="Output", |
| 30 | + description="Path to the output file.", |
| 31 | + value="${NODE_CACHE_FOLDER}" + "file.out", |
| 32 | + ), |
| 33 | + ] |
| 34 | + |
| 35 | + def processChunk(self, chunk): |
| 36 | + pass |
| 37 | + |
| 38 | + |
| 39 | +def test_registerSameNodeWithDifferentLocations(): |
| 40 | + """ |
| 41 | + Check that the nodes with the same description but registered at different locations have different evaluations |
| 42 | + for the NODE_SOURCECODE_FOLDER value. |
| 43 | + """ |
| 44 | + loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes")) |
| 45 | + assert "RelativePaths" in meshroom.core.nodesDesc.keys() |
| 46 | + |
| 47 | + # Node loaded from "nodes/test" |
| 48 | + n1 = Node("RelativePaths") |
| 49 | + sourceFolderNode1 = n1.attribute("relativePathInput").getEvalValue() |
| 50 | + assert sourceFolderNode1 == Path(getfile(meshroom.core.nodesDesc["RelativePaths"])).parent.resolve().as_posix() + "/input.txt" |
| 51 | + assert sourceFolderNode1 == n1.sourceCodeFolder + "/input.txt" |
| 52 | + assert Path(sourceFolderNode1).parent.resolve().as_posix() == n1.rootFolder["NODE_SOURCECODE_FOLDER"] |
| 53 | + |
| 54 | + # Unregister that node and replace it with the one from this file |
| 55 | + unregisterNodeType(RelativePaths) |
| 56 | + assert "RelativePaths" not in meshroom.core.nodesDesc.keys() |
| 57 | + |
| 58 | + registerNodeType(RelativePaths) |
| 59 | + assert "RelativePaths" in meshroom.core.nodesDesc.keys() |
| 60 | + |
| 61 | + n2 = Node("RelativePaths") |
| 62 | + sourceFolderNode2 = n2.attribute("relativePathInput").getEvalValue() |
| 63 | + assert sourceFolderNode2 == Path(getfile(meshroom.core.nodesDesc["RelativePaths"])).parent.resolve().as_posix() + "/input.txt" |
| 64 | + assert sourceFolderNode2 == n2.sourceCodeFolder + "/input.txt" |
| 65 | + assert Path(sourceFolderNode2).parent.resolve().as_posix() == n2.rootFolder["NODE_SOURCECODE_FOLDER"] |
| 66 | + |
| 67 | + assert sourceFolderNode1 is not sourceFolderNode2 |
| 68 | + unregisterNodeType(RelativePaths) |
| 69 | + |
| 70 | + |
| 71 | +def test_reloadGraphWithDifferentNodeLocations(): |
| 72 | + """ |
| 73 | + Save a Graph with a node description registered at a specific location, unregister that node type, and register the |
| 74 | + same description from a different location. |
| 75 | + """ |
| 76 | + loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes")) |
| 77 | + assert "RelativePaths" in meshroom.core.nodesDesc.keys() |
| 78 | + |
| 79 | + graph = Graph('') |
| 80 | + node = graph.addNewNode("RelativePaths") |
| 81 | + name = node.name |
| 82 | + |
| 83 | + # Save graph in a file |
| 84 | + filename = mktemp() |
| 85 | + graph.save(filename) |
| 86 | + |
| 87 | + sourceCodeFolderNode = node.attribute("relativePathInput").getEvalValue() |
| 88 | + assert sourceCodeFolderNode == node.rootFolder["NODE_SOURCECODE_FOLDER"] + "/input.txt" |
| 89 | + |
| 90 | + cacheFolderNode = node.attribute("output").value # output attribute, already evaluated upon the node's creation |
| 91 | + node._buildCmdVars() |
| 92 | + assert desc.Node.internalFolder == node.rootFolder["NODE_CACHE_FOLDER"] |
| 93 | + assert cacheFolderNode == desc.Node.internalFolder.format(**node._cmdVars) + "file.out" |
| 94 | + |
| 95 | + # Delete the current graph |
| 96 | + del graph |
| 97 | + |
| 98 | + # Unregister that node and replace it with the one from this file |
| 99 | + unregisterNodeType(meshroom.core.nodesDesc["RelativePaths"]) |
| 100 | + assert "RelativePaths" not in meshroom.core.nodesDesc.keys() |
| 101 | + |
| 102 | + registerNodeType(RelativePaths) |
| 103 | + assert "RelativePaths" in meshroom.core.nodesDesc.keys() |
| 104 | + |
| 105 | + # Reload the graph |
| 106 | + graph = loadGraph(filename) |
| 107 | + assert graph |
| 108 | + node = graph.node(name) |
| 109 | + assert node.nodeType == "RelativePaths" |
| 110 | + |
| 111 | + # Check that the relative path is different for the input |
| 112 | + assert node.attribute("relativePathInput").getEvalValue() != sourceCodeFolderNode |
| 113 | + |
| 114 | + # Check that it is the same for the cache |
| 115 | + assert node.attribute("output").value == cacheFolderNode |
| 116 | + |
| 117 | + os.remove(filename) |
| 118 | + unregisterNodeType(RelativePaths) |
| 119 | + |
| 120 | + |
| 121 | +def test_updateRootFolders(): |
| 122 | + """ |
| 123 | + Check that root folders can be added and removed. |
| 124 | + """ |
| 125 | + loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes")) |
| 126 | + assert "RelativePaths" in meshroom.core.nodesDesc |
| 127 | + |
| 128 | + node = Node("RelativePaths") |
| 129 | + assert len(node.rootFolder) == 2 |
| 130 | + |
| 131 | + # Add a new element in the list of root folders |
| 132 | + node.rootFolder.update({"NODE_TEST_FOLDER": gettempdir()}) |
| 133 | + assert len(node.rootFolder) == 3 |
| 134 | + |
| 135 | + attr = node.attribute("relativePathInput") |
| 136 | + |
| 137 | + sourceCodeFolder = attr.getEvalValue() |
| 138 | + attr.value = "${NODE_TEST_FOLDER}" + "/input.txt" |
| 139 | + assert attr.getEvalValue() == gettempdir() + "/input.txt" |
| 140 | + assert attr.getEvalValue() != sourceCodeFolder |
| 141 | + |
| 142 | + # Remove the extra element in the list of root folders |
| 143 | + node.rootFolder.pop("NODE_TEST_FOLDER", None) |
| 144 | + assert len(node.rootFolder) == 2 |
| 145 | + |
| 146 | + assert attr.getEvalValue() != gettempdir() + "/input.txt" |
| 147 | + assert attr.getEvalValue() == attr.value |
| 148 | + |
| 149 | + attr.value = attr.defaultValue() |
| 150 | + assert attr.getEvalValue() == sourceCodeFolder |
0 commit comments