Skip to content

Commit f982c1a

Browse files
author
Matthieu Hog
committed
added support for rez envs
1 parent b3aef33 commit f982c1a

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

meshroom/core/desc.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,13 @@ def _envName(cls):
745745
"""
746746
Get the env name by hashing the env files, overwrite this to use a custom pre-build env
747747
"""
748+
from core.plugin import EnvType
749+
from meshroom.core.plugin import getEnvName #lazy import as to avoid circular dep
750+
if cls.envType.value == EnvType.REZ.value:
751+
return cls.envFile
748752
with open(cls.envFile, 'r') as file:
749753
envContent = file.read()
750-
from meshroom.core.plugin import getEnvName #lazy import as to avoid circular dep
754+
751755
return getEnvName(envContent)
752756

753757
@property

meshroom/core/plugin.py

+34-12
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@
2323
from meshroom.core.graph import loadGraph
2424
from meshroom.core import hashValue
2525

26-
#where the executables are (eg meshroom compute)
26+
#executables def
2727
meshroomBinDir = os.path.abspath(os.path.join(meshroomFolder, "..", "bin"))
28+
condaBin = "conda"
29+
dockerBin = "docker"
2830

2931
class EnvType(Enum):
3032
"""
3133
enum for the type of env used (by degree of encapsulation)
3234
"""
3335
NONE = 0
3436
PIP = 1
37+
REZ = 2
3538
VENV = 10
3639
CONDA = 20
3740
DOCKER = 30
@@ -63,7 +66,7 @@ def _dockerImageExists(image_name, tag='latest'):
6366
Check if the desired image:tag exists
6467
"""
6568
try:
66-
result = subprocess.run( ['docker', 'images', image_name, '--format', '{{.Repository}}:{{.Tag}}'],
69+
result = subprocess.run( [dockerBin, 'images', image_name, '--format', '{{.Repository}}:{{.Tag}}'],
6770
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True )
6871
if result.returncode != 0:
6972
return False
@@ -84,8 +87,8 @@ def _condaEnvExist(envName):
8487
"""
8588
Checks if a specified env exists
8689
"""
87-
cmd = "conda list --name "+envName
88-
return os.system(cmd) == 0
90+
cmd = condaBin+" list --name "+envName
91+
return os.system(cmd) == 0
8992

9093
def _formatPluginName(pluginName):
9194
"""
@@ -116,6 +119,23 @@ def _venvExists(envName):
116119
"""
117120
return os.path.isdir(getVenvPath(envName))
118121

122+
def getActiveRezPackages():
123+
"""
124+
Returns a list containing the active explicit packages
125+
"""
126+
packages = []
127+
if 'REZ_REQUEST' in os.environ:
128+
nondefpackages = [n.split("-")[0] for n in os.environ.get('REZ_REQUEST', '').split()]
129+
resolvedPackages = os.environ.get('REZ_RESOLVE', '').split()
130+
resolvedVersions = {}
131+
for r in resolvedPackages:
132+
if r.startswith('~'):
133+
continue
134+
v = r.split('-')
135+
resolvedVersions[v[0]] = v[1]
136+
packages = [p+"-"+resolvedVersions[p] for p in resolvedVersions.keys() if p in nondefpackages]
137+
return packages
138+
119139
def installPlugin(pluginUrl):
120140
"""
121141
Install plugin from an url or local path.
@@ -219,7 +239,7 @@ def isBuilt(nodeDesc):
219239
"""
220240
Check if the env needs to be build for a specific nodesc.
221241
"""
222-
if nodeDesc.envType == EnvType.NONE:
242+
if nodeDesc.envType in [EnvType.NONE, EnvType.REZ]:
223243
return True
224244
elif nodeDesc.envType == EnvType.PIP:
225245
#NOTE: could find way to check for installed packages instead of rebuilding all the time
@@ -238,7 +258,7 @@ def build(nodeDesc):
238258
if not hasattr(nodeDesc, 'envFile'):
239259
raise RuntimeError("The nodedesc has no env file")
240260
returnValue = 0
241-
if nodeDesc.envType == EnvType.NONE:
261+
if nodeDesc.envType in [EnvType.NONE, EnvType.REZ]:
242262
pass
243263
elif nodeDesc.envType == EnvType.PIP:
244264
#install packages in the same python as meshroom
@@ -263,14 +283,14 @@ def build(nodeDesc):
263283
elif nodeDesc.envType == EnvType.CONDA:
264284
#build a conda env from a yaml file
265285
logging.info("Creating conda env "+nodeDesc._envName+" from "+nodeDesc.envFile)
266-
makeEnvCommand = ( _cleanEnvVarsRez()+" conda config --set channel_priority strict ; "
267-
+" conda env create -v -v --name "+nodeDesc._envName
286+
makeEnvCommand = ( _cleanEnvVarsRez()+condaBin+" config --set channel_priority strict ; "
287+
+condaBin+" env create -v -v --name "+nodeDesc._envName
268288
+" --file "+nodeDesc.envFile+" ")
269289
logging.info("Making conda env")
270290
logging.info(makeEnvCommand)
271291
returnValue = os.system(makeEnvCommand)
272292
#find path to env's folder and add symlink to meshroom
273-
condaPythonExecudable=subprocess.check_output(_cleanEnvVarsRez()+"conda run -n "+nodeDesc._envName
293+
condaPythonExecudable=subprocess.check_output(_cleanEnvVarsRez()+condaBin+" run -n "+nodeDesc._envName
274294
+" python -c \"import sys; print(sys.executable)\"",
275295
shell=True).strip().decode('UTF-8')
276296
condaPythonLibFolder=os.path.join(os.path.dirname(condaPythonExecudable), '..', 'lib')
@@ -281,7 +301,7 @@ def build(nodeDesc):
281301
elif nodeDesc.envType == EnvType.DOCKER:
282302
#build docker image
283303
logging.info("Creating image "+nodeDesc._envName+" from "+ nodeDesc.envFile)
284-
buildCommand = "docker build -f "+nodeDesc.envFile+" -t "+nodeDesc._envName+" "+os.path.dirname(nodeDesc.envFile)
304+
buildCommand = dockerBin+" build -f "+nodeDesc.envFile+" -t "+nodeDesc._envName+" "+os.path.dirname(nodeDesc.envFile)
285305
logging.info("Building with "+buildCommand+" ...")
286306
returnValue = os.system(buildCommand)
287307
logging.info("Done")
@@ -316,9 +336,11 @@ def getCommandLine(chunk):
316336
envExe = getVenvExe(envPath)
317337
#make sure meshroom in in pythonpath and that we call the right python
318338
cmdPrefix = _cleanEnvVarsRez()+pythonsetMeshroomPath+" "+envExe + " "+ meshroomCompute +" "
339+
elif nodeDesc.envType == EnvType.REZ:
340+
cmdPrefix = "rez env "+" ".join(getActiveRezPackages())+" "+nodeDesc._envName+" -- "+ meshroomCompute +" "
319341
elif nodeDesc.envType == EnvType.CONDA:
320342
#NOTE: system env vars are not passed to conda run, we installed it 'manually' before
321-
cmdPrefix = _cleanEnvVarsRez()+" conda run --cwd "+os.path.join(meshroomFolder, "..")\
343+
cmdPrefix = _cleanEnvVarsRez()+condaBin+" run --cwd "+os.path.join(meshroomFolder, "..")\
322344
+" --no-capture-output -n "+nodeDesc._envName+" "+" python "+meshroomCompute
323345
elif nodeDesc.envType == EnvType.DOCKER:
324346
#path to the selected plugin
@@ -337,7 +359,7 @@ def getCommandLine(chunk):
337359
if chunk.node.nodeDesc.gpu != desc.Level.NONE:
338360
runtimeArg="--runtime=nvidia --gpus all"
339361
#compose cl
340-
cmdPrefix = "docker run -it --rm "+runtimeArg+" "+mountCommand+" "+envCommand+" "+nodeDesc._envName +" \"python /meshroomBinDir/meshroom_compute "
362+
cmdPrefix = dockerBin+" run -it --rm "+runtimeArg+" "+mountCommand+" "+envCommand+" "+nodeDesc._envName +" \"python /meshroomBinDir/meshroom_compute "
341363
meshroomComputeArgs="--node "+chunk.node.name+" "+chunk.node.graph.filepath+"\""
342364
else:
343365
raise RuntimeError("NodeType not recognised")

tests/nodes/plugins/dummyNodes.py

+16
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ def processChunk(self, chunk):
8686
chunk.logManager.start("info")
8787
chunk.logger.info(np.abs(-1))
8888

89+
class DummyRez(PluginNode):
90+
91+
category = 'Dummy'
92+
documentation = ''' '''
93+
94+
envType = EnvType.REZ
95+
envFile = "numpy"
96+
97+
inputs = []
98+
outputs = []
99+
100+
def processChunk(self, chunk):
101+
import numpy as np
102+
chunk.logManager.start("info")
103+
chunk.logger.info(np.abs(-1))
104+
89105
#Command line node
90106

91107
class DummyCondaCL(PluginCommandLineNode):

0 commit comments

Comments
 (0)