|
7 | 7 | import tempfile |
8 | 8 |
|
9 | 9 | 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" |
| 10 | +pythonInterpName = sys.executable |
21 | 11 |
|
22 | 12 | rootKernelFileContent = ( |
23 | 13 | """{ |
@@ -154,23 +144,32 @@ def getInterpreterName(): |
154 | 144 | yes, return its name else return 'ipython' |
155 | 145 | """ |
156 | 146 | ret = subprocess.call("type jupyter", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
157 | | - return "jupyter" if ret == 0 else "i%s" % pythonInterpName |
| 147 | + return "jupyter nbconvert" if ret == 0 else "nbconvert" |
158 | 148 |
|
159 | 149 |
|
160 | 150 | def getKernelName(inNBName): |
161 | 151 | with open(inNBName) as f: |
162 | 152 | nbj = json.load(f) |
163 | 153 | if nbj["metadata"]["kernelspec"]["language"] == "python": |
164 | | - return pythonInterpName |
| 154 | + return "python3" |
165 | 155 | else: # we support only Python and C++ |
166 | 156 | return "root" |
167 | 157 |
|
168 | 158 |
|
169 | | -def canReproduceNotebook(inNBName, kernelName, needsCompare): |
| 159 | +def canReproduceNotebook(inNBName, needsCompare): |
170 | 160 | tmpDir = addEtcToEnvironment(os.path.dirname(inNBName)) |
171 | 161 | outNBName = inNBName.replace(nbExtension, "_out" + nbExtension) |
172 | 162 | interpName = getInterpreterName() |
173 | | - convCmd = convCmdTmpl % (interpName, kernelName, inNBName, outNBName) |
| 163 | + convCmd = ( |
| 164 | + "%s -m %s " |
| 165 | + "--to notebook " |
| 166 | + "--ExecutePreprocessor.kernel_name=%s " |
| 167 | + "--ExecutePreprocessor.enabled=True " |
| 168 | + "--ExecutePreprocessor.timeout=3600 " |
| 169 | + "--ExecutePreprocessor.startup_timeout=180 " |
| 170 | + "%s " |
| 171 | + "--output %s" % (pythonInterpName, interpName, getKernelName(inNBName), inNBName, outNBName) |
| 172 | + ) |
174 | 173 | exitStatus = os.system(convCmd) # we use system to inherit the environment in os.environ |
175 | 174 | shutil.rmtree(tmpDir) |
176 | 175 | if needsCompare: |
@@ -206,7 +205,5 @@ def isInputNotebookFileName(filename): |
206 | 205 | except: |
207 | 206 | raise ImportError("Cannot import jupyter") |
208 | 207 |
|
209 | | - kernelName = getKernelName(nbFileName) |
210 | | - |
211 | | - retCode = canReproduceNotebook(nbFileName, kernelName, needsCompare) |
| 208 | + retCode = canReproduceNotebook(nbFileName, needsCompare) |
212 | 209 | sys.exit(retCode) |
0 commit comments