Skip to content

Commit 574bd10

Browse files
committed
[roottest] Inherit Python executable from main process in nbdiff.py
This makes sure that we're using the exact Python version in the notebook tests that ROOT was built against. Also, always use the new canonical `jupyter-nbconvert` executable for notebook conversion.
1 parent 62d931d commit 574bd10

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

roottest/python/JupyROOT/nbdiff.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,11 @@
22
import json
33
import os
44
import shutil
5-
import subprocess
65
import sys
76
import tempfile
87

98
nbExtension = ".ipynb"
10-
convCmdTmpl = (
11-
"%s nbconvert "
12-
"--to notebook "
13-
"--ExecutePreprocessor.kernel_name=%s "
14-
"--ExecutePreprocessor.enabled=True "
15-
"--ExecutePreprocessor.timeout=3600 "
16-
"--ExecutePreprocessor.startup_timeout=180 "
17-
"%s "
18-
"--output %s"
19-
)
20-
pythonInterpName = "python3"
9+
pythonInterpName = sys.executable
2110

2211
rootKernelFileContent = (
2312
"""{
@@ -149,28 +138,28 @@ def addEtcToEnvironment(inNBDirName):
149138
return ipythondir
150139

151140

152-
def getInterpreterName():
153-
"""Find if the 'jupyter' executable is available on the platform. If
154-
yes, return its name else return 'ipython'
155-
"""
156-
ret = subprocess.call("type jupyter", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
157-
return "jupyter" if ret == 0 else "i%s" % pythonInterpName
158-
159-
160141
def getKernelName(inNBName):
161142
with open(inNBName) as f:
162143
nbj = json.load(f)
163144
if nbj["metadata"]["kernelspec"]["language"] == "python":
164-
return pythonInterpName
145+
return "python3"
165146
else: # we support only Python and C++
166147
return "root"
167148

168149

169-
def canReproduceNotebook(inNBName, kernelName, needsCompare):
150+
def canReproduceNotebook(inNBName, needsCompare):
170151
tmpDir = addEtcToEnvironment(os.path.dirname(inNBName))
171152
outNBName = inNBName.replace(nbExtension, "_out" + nbExtension)
172-
interpName = getInterpreterName()
173-
convCmd = convCmdTmpl % (interpName, kernelName, inNBName, outNBName)
153+
convCmd = (
154+
"%s -m jupyter-nbconvert "
155+
"--to notebook "
156+
"--ExecutePreprocessor.kernel_name=%s "
157+
"--ExecutePreprocessor.enabled=True "
158+
"--ExecutePreprocessor.timeout=3600 "
159+
"--ExecutePreprocessor.startup_timeout=180 "
160+
"%s "
161+
"--output %s" % (pythonInterpName, getKernelName(inNBName), inNBName, outNBName)
162+
)
174163
exitStatus = os.system(convCmd) # we use system to inherit the environment in os.environ
175164
shutil.rmtree(tmpDir)
176165
if needsCompare:
@@ -206,7 +195,5 @@ def isInputNotebookFileName(filename):
206195
except:
207196
raise ImportError("Cannot import jupyter")
208197

209-
kernelName = getKernelName(nbFileName)
210-
211-
retCode = canReproduceNotebook(nbFileName, kernelName, needsCompare)
198+
retCode = canReproduceNotebook(nbFileName, needsCompare)
212199
sys.exit(retCode)

0 commit comments

Comments
 (0)