|
| 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 | + """ |
| 16 | + Local "RelativePaths" node, with a different source code folder as the one in nodes/test. |
| 17 | + """ |
| 18 | + |
| 19 | + documentation = "Test node with filepaths that are set relatively to some variables." |
| 20 | + inputs = [ |
| 21 | + desc.File( |
| 22 | + name="relativePathInput", |
| 23 | + label="Relative Input File", |
| 24 | + description="Relative path to the input file.", |
| 25 | + value="${NODE_SOURCECODE_FOLDER}" + "/input.txt", |
| 26 | + ), |
| 27 | + ] |
| 28 | + |
| 29 | + outputs = [ |
| 30 | + desc.File( |
| 31 | + name="output", |
| 32 | + label="Output", |
| 33 | + description="Path to the output file.", |
| 34 | + value="${NODE_CACHE_FOLDER}" + "file.out", |
| 35 | + ), |
| 36 | + ] |
| 37 | + |
| 38 | + def processChunk(self, chunk): |
| 39 | + pass |
| 40 | + |
| 41 | + |
| 42 | +class FakeRelativePaths(desc.Node): |
| 43 | + documentation = """ |
| 44 | + Test node with filepaths that are set relatively to some variables that do not exist. |
| 45 | + """ |
| 46 | + inputs = [ |
| 47 | + desc.File( |
| 48 | + name="relativePathInput", |
| 49 | + label="Relative Input File", |
| 50 | + description="Relative path to the input file.", |
| 51 | + value="${NODE_UNEXISTING_FOLDER}" + "/input.txt", |
| 52 | + ), |
| 53 | + ] |
| 54 | + |
| 55 | + outputs = [ |
| 56 | + desc.File( |
| 57 | + name="output", |
| 58 | + label="Output", |
| 59 | + description="Path to the output file.", |
| 60 | + value="${NODE_RANDOM_FOLDER}" + "file.out", |
| 61 | + ) |
| 62 | + ] |
| 63 | + |
| 64 | + def processChunk(self, chunk): |
| 65 | + pass |
| 66 | + |
| 67 | + |
| 68 | +def test_registerSameNodeWithDifferentLocations(): |
| 69 | + """ |
| 70 | + Check that the nodes with the same description but registered at different locations |
| 71 | + have different evaluations for the NODE_SOURCECODE_FOLDER value. |
| 72 | + """ |
| 73 | + loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes")) |
| 74 | + assert "RelativePaths" in meshroom.core.nodesDesc |
| 75 | + |
| 76 | + # Node loaded from "nodes/test" |
| 77 | + n1 = Node("RelativePaths") |
| 78 | + sourceFolderNode1 = n1.attribute("relativePathInput").getEvalValue() |
| 79 | + assert sourceFolderNode1 == \ |
| 80 | + Path(getfile(meshroom.core.nodesDesc["RelativePaths"])).parent.resolve().as_posix() + \ |
| 81 | + "/input.txt" |
| 82 | + assert sourceFolderNode1 == n1.sourceCodeFolder + "/input.txt" |
| 83 | + assert Path(sourceFolderNode1).parent.resolve().as_posix() == \ |
| 84 | + n1.rootFolder["NODE_SOURCECODE_FOLDER"] |
| 85 | + |
| 86 | + # Unregister that node and replace it with the one from this file |
| 87 | + unregisterNodeType(RelativePaths) |
| 88 | + assert "RelativePaths" not in meshroom.core.nodesDesc |
| 89 | + |
| 90 | + registerNodeType(RelativePaths) |
| 91 | + assert "RelativePaths" in meshroom.core.nodesDesc |
| 92 | + |
| 93 | + n2 = Node("RelativePaths") |
| 94 | + sourceFolderNode2 = n2.attribute("relativePathInput").getEvalValue() |
| 95 | + assert sourceFolderNode2 == \ |
| 96 | + Path(getfile(meshroom.core.nodesDesc["RelativePaths"])).parent.resolve().as_posix() + \ |
| 97 | + "/input.txt" |
| 98 | + assert sourceFolderNode2 == n2.sourceCodeFolder + "/input.txt" |
| 99 | + assert Path(sourceFolderNode2).parent.resolve().as_posix() == \ |
| 100 | + n2.rootFolder["NODE_SOURCECODE_FOLDER"] |
| 101 | + |
| 102 | + assert sourceFolderNode1 is not sourceFolderNode2 |
| 103 | + unregisterNodeType(RelativePaths) |
| 104 | + |
| 105 | + |
| 106 | +def test_reloadGraphWithDifferentNodeLocations(): |
| 107 | + """ |
| 108 | + Save a Graph with a node description registered at a specific location, unregister that node |
| 109 | + type, and register the same description from a different location. |
| 110 | + """ |
| 111 | + loadAllNodes(os.path.join(os.path.dirname(__file__), "nodes")) |
| 112 | + assert "RelativePaths" in meshroom.core.nodesDesc |
| 113 | + |
| 114 | + graph = Graph('') |
| 115 | + node = graph.addNewNode("RelativePaths") |
| 116 | + name = node.name |
| 117 | + |
| 118 | + # Save graph in a file |
| 119 | + filename = mktemp() |
| 120 | + graph.save(filename) |
| 121 | + |
| 122 | + sourceCodeFolderNode = node.attribute("relativePathInput").getEvalValue() |
| 123 | + assert sourceCodeFolderNode == node.rootFolder["NODE_SOURCECODE_FOLDER"] + "/input.txt" |
| 124 | + |
| 125 | + # Output attribute, already evaluated upon the node's creation |
| 126 | + cacheFolderNode = node.attribute("output").value |
| 127 | + |
| 128 | + node._buildCmdVars() |
| 129 | + assert desc.Node.internalFolder == node.rootFolder["NODE_CACHE_FOLDER"] |
| 130 | + assert cacheFolderNode == desc.Node.internalFolder.format(**node._cmdVars) + "file.out" |
| 131 | + |
| 132 | + # Delete the current graph |
| 133 | + del graph |
| 134 | + |
| 135 | + # Unregister that node and replace it with the one from this file |
| 136 | + unregisterNodeType(meshroom.core.nodesDesc["RelativePaths"]) |
| 137 | + assert "RelativePaths" not in meshroom.core.nodesDesc |
| 138 | + |
| 139 | + registerNodeType(RelativePaths) |
| 140 | + assert "RelativePaths" in meshroom.core.nodesDesc |
| 141 | + |
| 142 | + # Reload the graph |
| 143 | + graph = loadGraph(filename) |
| 144 | + assert graph |
| 145 | + node = graph.node(name) |
| 146 | + assert node.nodeType == "RelativePaths" |
| 147 | + |
| 148 | + # Check that the relative path is different for the input |
| 149 | + assert node.attribute("relativePathInput").getEvalValue() != sourceCodeFolderNode |
| 150 | + |
| 151 | + # Check that it is the same for the cache |
| 152 | + assert node.attribute("output").value == cacheFolderNode |
| 153 | + |
| 154 | + os.remove(filename) |
| 155 | + unregisterNodeType(RelativePaths) |
| 156 | + |
| 157 | + |
| 158 | +class TestRootFolderContent(): |
| 159 | + """ Class to test changes on the root folders' values. """ |
| 160 | + @classmethod |
| 161 | + def setup_class(cls): |
| 162 | + """ Register nodes upon class' creation. """ |
| 163 | + registerNodeType(RelativePaths) |
| 164 | + registerNodeType(FakeRelativePaths) |
| 165 | + |
| 166 | + @classmethod |
| 167 | + def teardown_class(cls): |
| 168 | + """ Unregister nodes upon class' destruction. """ |
| 169 | + unregisterNodeType(RelativePaths) |
| 170 | + unregisterNodeType(FakeRelativePaths) |
| 171 | + |
| 172 | + def test_updateRootFolders(self): |
| 173 | + """ |
| 174 | + Check that root folder variables can be added and removed. |
| 175 | + """ |
| 176 | + assert "RelativePaths" in meshroom.core.nodesDesc |
| 177 | + |
| 178 | + node = Node("RelativePaths") |
| 179 | + assert len(node.rootFolder) == 2 |
| 180 | + |
| 181 | + # Add a new element in the list of root folders |
| 182 | + node.rootFolder.update({"NODE_TEST_FOLDER": gettempdir()}) |
| 183 | + assert len(node.rootFolder) == 3 |
| 184 | + |
| 185 | + attr = node.attribute("relativePathInput") |
| 186 | + |
| 187 | + sourceCodeFolder = attr.getEvalValue() |
| 188 | + attr.value = "${NODE_TEST_FOLDER}" + "/input.txt" |
| 189 | + assert attr.getEvalValue() == gettempdir() + "/input.txt" |
| 190 | + assert attr.getEvalValue() != sourceCodeFolder |
| 191 | + |
| 192 | + # Remove the extra element in the list of root folders |
| 193 | + node.rootFolder.pop("NODE_TEST_FOLDER", None) |
| 194 | + assert len(node.rootFolder) == 2 |
| 195 | + |
| 196 | + assert attr.getEvalValue() != gettempdir() + "/input.txt" |
| 197 | + # Unevaluated value as the variable does not exist |
| 198 | + assert attr.getEvalValue() == attr.value |
| 199 | + |
| 200 | + attr.value = attr.defaultValue() |
| 201 | + assert attr.getEvalValue() == sourceCodeFolder |
| 202 | + |
| 203 | + |
| 204 | + def test_nonExistingRootFolder(self): |
| 205 | + """ |
| 206 | + Check that a variable that does not correspond to any root folder is not evaluated. |
| 207 | + """ |
| 208 | + node = Node("FakeRelativePaths") |
| 209 | + |
| 210 | + # The root folder variable cannot be evaluated as it does not exist: it is not substituted |
| 211 | + assert node.attribute("relativePathInput").getEvalValue() == \ |
| 212 | + node.attribute("relativePathInput").value |
| 213 | + assert node.attribute("output").getEvalValue() == \ |
| 214 | + node.attribute("output").value == "${NODE_RANDOM_FOLDER}file.out" |
0 commit comments