| 202 | return connectionMetadata.kind === 'connectToLiveRemoteKernel'; |
| 203 | } |
| 204 | export function getKernelId(spec: IJupyterKernelSpec, interpreter?: PythonEnvironment, serverId?: string) { |
| 205 | // Non-Python kernels cannot contain an interpreter (even in their id). |
| 206 | interpreter = isPythonKernelSpec(spec) ? interpreter : undefined; |
| 207 | // Do not include things like display names, as they aren't unique & can change over time. |
| 208 | // if the spec name is generated by us, then exclude the leading bit (as it can contain version numbers). |
| 209 | // & sometimes the kernelspec might not have the version number (if it wasn't available at the time of generation of spec) |
| 210 | // See getInterpreterKernelSpecName for details of this logic. |
| 211 | let specName = spec.name; |
| 212 | const kernelRegistrationKind = getKernelRegistrationInfo(spec); |
| 213 | // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 214 | if (kernelRegistrationKind && specName.includes(autoGeneratedKernelNameIdentifier)) { |
| 215 | // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 216 | specName = specName.substring(specName.indexOf(autoGeneratedKernelNameIdentifier)); |
| 217 | // When users create kernelspecs, we need to include the name of custom kernelspecs in the id. |
| 218 | if (kernelRegistrationKind === 'registeredByNewVersionOfExtForCustomKernelSpec') { |
| 219 | const originalSpecFile = spec.metadata?.vscode?.originalSpecFile || spec.metadata?.originalSpecFile; |
| 220 | if (originalSpecFile) { |
| 221 | specName = `${specName}#${path.basename(path.dirname(originalSpecFile))}`; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | // The arguments in the spec can be different for multiple kernel specs pointing to the same interpreter. |
| 226 | // E.g. we can have pyspark kernels with `"-m", "sparkmagic.kernels.pysparkkernel.pysparkkernel",` |
| 227 | // & another with "-m","jupyter_nb_ext.kernels.synapse_pyspark.kernel" |
| 228 | let argsForGenerationOfId = ''; |
| 229 | if (isPythonKernelSpec(spec)) { |
| 230 | // Ignore the first argument (its the python path). |
| 231 | // Ignore the common bits such as `-f` & `connection_file` |
| 232 | argsForGenerationOfId = spec.argv |
| 233 | .slice(1) |
| 234 | .filter((item) => !['-f', '{connection_file}'].includes(item)) |
| 235 | .join('#') |
| 236 | .toLowerCase(); |
| 237 | } else { |
| 238 | // Lets not assume that non-python kernels cannot have such issues |
| 239 | argsForGenerationOfId = spec.argv.join('#').toLowerCase(); |
| 240 | } |
| 241 | const prefixForRemoteKernels = serverId ? `${serverId}.` : ''; |
| 242 | const specPath = getFilePath( |
| 243 | getNormalizedInterpreterPath(spec.interpreterPath ? Uri.file(spec.interpreterPath) : Uri.file(spec.executable)) |
| 244 | ); |
| 245 | const interpreterPath = getFilePath(getNormalizedInterpreterPath(interpreter?.uri)) || ''; |
| 246 | return `${prefixForRemoteKernels}${ |
| 247 | spec.id || '' |
| 248 | }.${specName}.${specPath}.${interpreterPath}.${argsForGenerationOfId}`; |
| 249 | } |
| 250 | |
| 251 | export function getDisplayNameOrNameOfKernelConnection(kernelConnection: KernelConnectionMetadata | undefined) { |
| 252 | const oldDisplayName = getOldFormatDisplayNameOrNameOfKernelConnection(kernelConnection); |