Copilot commented on code in PR #5034: URL: https://github.com/apache/solr/pull/5034#discussion_r4192857880
########## gradle/python.gradle: ########## @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Python support for the build. A system interpreter is used when one is available; otherwise the +// build falls back to GraalPy, resolved as a regular Gradle dependency and run on the JVM. +// See dev-docs/solr-source-code.adoc. +// +// Groovy rather than Kotlin: PythonExecTask lives in build-infra, whose classes are not +// importable from build scripts, so its properties have to be set dynamically. + +import java.util.Locale + +configure(rootProject) { + // Resolved lazily, and only if a task actually takes the GraalPy path. + configurations { + graalpy { + description = "GraalPy runtime, used when no system python3 is available." + canBeConsumed = false + } + } + + dependencies { + // Deliberately not org.graalvm.python:python (the aggregator): it pulls in + // org.graalvm.truffle:truffle-runtime, the optimizing runtime, which is unsupported on a + // stock OpenJDK. Naming the three jars directly keeps it out. + graalpy libs.graalvm.python.launcher + graalpy libs.graalvm.python.language + graalpy libs.graalvm.python.resources + } + + ext { + // -Psolr.python.mode / SOLR_PYTHON_MODE: + // auto - -Ppython3.exe, else python3 on PATH, else GraalPy (default) + // system - require a system interpreter; never resolves GraalPy + // graalpy - always use GraalPy (to exercise the fallback) + solrPythonMode = propertyOrEnvOrDefault("solr.python.mode", "SOLR_PYTHON_MODE", "auto") + .toString().toLowerCase(Locale.ROOT) + + if (!(solrPythonMode in ["auto", "system", "graalpy"])) { + throw new GradleException("Invalid -Psolr.python.mode=${solrPythonMode}; " + + "expected one of: auto, system, graalpy") + } + + // Absolute path of 'name' on PATH, or null. A plain PATH walk rather than spawning + // '<exe> --version': far cheaper, and safe to do at configuration time. + findOnPath = {String name -> + def isWindows = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win") + def extensions = isWindows + ? ((System.getenv("PATHEXT") ?: ".EXE;.BAT;.CMD").split(";") as List) + : [""] + for (String dir : (System.getenv("PATH") ?: "").split(File.pathSeparator)) { + if (!dir) { + continue + } + for (String ext : extensions) { + def candidate = new File(dir, name + ext) + if (candidate.isFile() && candidate.canExecute()) { + return candidate.absolutePath + } + } + } + return null + } + + // The system interpreter to use, or null to use the GraalPy fallback. + solrPythonExecutable = { + if (solrPythonMode == "graalpy") { + return null + } + // Honour the convention shared with externalTool("python3"). An explicitly configured + // interpreter is validated rather than silently ignored: pointing the build at a path that + // is not there is a mistake worth reporting, not a reason to download GraalPy. + def explicit = propertyOrDefault("python3.exe", null) + if (explicit) { + def resolved = (explicit as String).contains(File.separator) + ? (new File(explicit as String).with {isFile() && canExecute() ? absolutePath : null}) + : findOnPath(explicit as String) Review Comment: On Windows, a normal explicit path such as `C:/Python/python.exe` contains `/`, not `File.separator` (`\\`), so this branch treats it as a PATH name and rejects it unless that full string is also on PATH. Detect a path using `File.isAbsolute()` and/or both slash characters before falling back to the PATH lookup, otherwise the documented `-Ppython3.exe=<path>` override fails for common Windows path syntax. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
