|
| 1 | +# coding:utf-8 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +from pathlib import Path |
| 6 | +import logging |
| 7 | + |
| 8 | +from meshroom.core.graph import Graph |
| 9 | +from meshroom.core import desc |
| 10 | +from .utils import registerNodeDesc, unregisterNodeDesc |
| 11 | + |
| 12 | +LOGGER = logging.getLogger("TestCompute") |
| 13 | + |
| 14 | + |
| 15 | +def executeChunks(node, tmpPath, size): |
| 16 | + nodeCache = os.path.join(tmpPath, node.internalFolder) |
| 17 | + os.makedirs(nodeCache) |
| 18 | + logFiles = {} |
| 19 | + for chunkIndex in range(size): |
| 20 | + logFileName = "log" |
| 21 | + if size > 1: |
| 22 | + logFileName = f"{chunkIndex}.log" |
| 23 | + logFile = Path(nodeCache) / logFileName |
| 24 | + logFiles[chunkIndex] = logFile |
| 25 | + logFile.touch() |
| 26 | + node.preprocess() |
| 27 | + if size > 1: |
| 28 | + chunk = node.chunks[chunkIndex] |
| 29 | + chunk.process(True, True) |
| 30 | + else: |
| 31 | + node.process(True, True) |
| 32 | + node.postprocess() |
| 33 | + return logFiles |
| 34 | + |
| 35 | + |
| 36 | +class TestNodeA(desc.BaseNode): |
| 37 | + __test__ = False |
| 38 | + |
| 39 | + size = desc.StaticNodeSize(2) |
| 40 | + parallelization = desc.Parallelization(blockSize=1) |
| 41 | + |
| 42 | + inputs = [ |
| 43 | + desc.IntParam( |
| 44 | + name="input", |
| 45 | + label="Input", |
| 46 | + description="input", |
| 47 | + value=0, |
| 48 | + ), |
| 49 | + ] |
| 50 | + outputs = [ |
| 51 | + desc.IntParam( |
| 52 | + name="output", |
| 53 | + label="Output", |
| 54 | + description="Output", |
| 55 | + value=None, |
| 56 | + ), |
| 57 | + ] |
| 58 | + |
| 59 | + def processChunk(self, chunk): |
| 60 | + chunk.logManager.start("info") |
| 61 | + chunk.logger.info(f"> {chunk.node.name}") |
| 62 | + chunk.logManager.end() |
| 63 | + |
| 64 | + |
| 65 | +class TestNodeB(desc.BaseNode): |
| 66 | + __test__ = False |
| 67 | + |
| 68 | + inputs = [ |
| 69 | + desc.IntParam( |
| 70 | + name="input", |
| 71 | + label="Input", |
| 72 | + description="input", |
| 73 | + value=0, |
| 74 | + ), |
| 75 | + ] |
| 76 | + outputs = [ |
| 77 | + desc.IntParam( |
| 78 | + name="output", |
| 79 | + label="Output", |
| 80 | + description="Output", |
| 81 | + value=None, |
| 82 | + ), |
| 83 | + ] |
| 84 | + |
| 85 | + def process(self, node): |
| 86 | + LOGGER.info(f"> {node.name}") |
| 87 | + |
| 88 | + |
| 89 | +class TestNodeLogger: |
| 90 | + |
| 91 | + reA = re.compile(r"> TestNodeA_1") |
| 92 | + reB = re.compile(r"> TestNodeB_1") |
| 93 | + |
| 94 | + @classmethod |
| 95 | + def setup_class(cls): |
| 96 | + registerNodeDesc(TestNodeA) |
| 97 | + registerNodeDesc(TestNodeB) |
| 98 | + |
| 99 | + @classmethod |
| 100 | + def teardown_class(cls): |
| 101 | + unregisterNodeDesc(TestNodeA) |
| 102 | + unregisterNodeDesc(TestNodeB) |
| 103 | + |
| 104 | + def test_processChunk(self, tmp_path): |
| 105 | + graph = Graph("") |
| 106 | + graph._cacheDir = tmp_path |
| 107 | + node = graph.addNewNode(TestNodeA.__name__) |
| 108 | + # Compute |
| 109 | + logFiles = executeChunks(node, tmp_path, 2) |
| 110 | + for chunkId, logFile in logFiles.items(): |
| 111 | + with open(logFile, "r") as f: |
| 112 | + content = f.read() |
| 113 | + assert len(self.reA.findall(content)) == 1 |
| 114 | + |
| 115 | + def test_process(self, tmp_path): |
| 116 | + graph = Graph("") |
| 117 | + graph._cacheDir = tmp_path |
| 118 | + node = graph.addNewNode(TestNodeB.__name__) |
| 119 | + # Compute |
| 120 | + logFiles = executeChunks(node, tmp_path, 1) |
| 121 | + for _, logFile in logFiles.items(): |
| 122 | + with open(logFile, "r") as f: |
| 123 | + content = f.read() |
| 124 | + # TODO : not working yet because the logger is not available outside of chunks. |
| 125 | + # > fixed by #2845 |
| 126 | + # assert len(self.reB.findall(content)) == 1 |
| 127 | + pass |
0 commit comments