dianfu commented on a change in pull request #11702: [FLINK-16667][python][client] Support new Python dependency configuration options in flink-client. URL: https://github.com/apache/flink/pull/11702#discussion_r407984146
########## File path: flink-clients/src/main/java/org/apache/flink/client/cli/PythonProgramOptions.java ########## @@ -0,0 +1,140 @@ +/* + * 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. + */ + +package org.apache.flink.client.cli; + +import org.apache.flink.client.program.PackagedProgramUtils; +import org.apache.flink.configuration.Configuration; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.apache.flink.client.cli.CliFrontendParser.ARGS_OPTION; +import static org.apache.flink.client.cli.CliFrontendParser.CLASS_OPTION; +import static org.apache.flink.client.cli.CliFrontendParser.PYARCHIVE_OPTION; +import static org.apache.flink.client.cli.CliFrontendParser.PYEXEC_OPTION; +import static org.apache.flink.client.cli.CliFrontendParser.PYFILES_OPTION; +import static org.apache.flink.client.cli.CliFrontendParser.PYMODULE_OPTION; +import static org.apache.flink.client.cli.CliFrontendParser.PYREQUIREMENTS_OPTION; +import static org.apache.flink.client.cli.CliFrontendParser.PY_OPTION; + +/** + * The class for command line options that refer to a Python program or JAR program with Python command line options. + */ +public class PythonProgramOptions extends ProgramOptions { + + private static final Logger LOG = LoggerFactory.getLogger(PythonProgramOptions.class); + + private final Configuration pythonConfiguration; + + PythonProgramOptions(CommandLine line) throws CliArgsException { + super(line); + try { + ClassLoader classLoader; + try { + classLoader = new URLClassLoader( + new URL[]{PackagedProgramUtils.getPythonJar()}, + Thread.currentThread().getContextClassLoader()); + } catch (RuntimeException e) { + LOG.warn( + "An attempt to load the flink-python jar from the \"opt\" directory failed, " + + "fall back to use the context class loader to reflect the flink-python class.", e); + classLoader = Thread.currentThread().getContextClassLoader(); + } + Class<?> utilClazz = Class.forName( + "org.apache.flink.python.util.PythonDependencyUtils", + false, + classLoader); + Method utilMethod = utilClazz.getMethod( + "parseCommandLine", + CommandLine.class); + pythonConfiguration = (Configuration) utilMethod.invoke(null, line); + } catch (NoSuchMethodException | + IllegalAccessException | + InvocationTargetException | + ClassNotFoundException e) { + throw new CliArgsException( + "Python command line option detected but the flink-python module seems to be missing " + + "or not working as expected.", e); + } + // If the job is Python Shell job, the entry point class name is PythonGateWayServer. + // Otherwise, the entry point class of python job is PythonDriver + if (entryPointClass == null) { + entryPointClass = "org.apache.flink.client.python.PythonDriver"; + } + } + + @Override + protected String[] extractProgramArgs(CommandLine line) { + String[] args; + if (isPython(line)) { + String[] rawArgs = line.hasOption(ARGS_OPTION.getOpt()) ? + line.getOptionValues(ARGS_OPTION.getOpt()) : + line.getArgs(); + // copy python related parameters to program args and place them in front of user parameters + List<String> pyArgList = new ArrayList<>(); + Set<Option> pyOptions = new HashSet<>(); + pyOptions.add(PY_OPTION); + pyOptions.add(PYMODULE_OPTION); + for (Option option : line.getOptions()) { + if (pyOptions.contains(option)) { + pyArgList.add("--" + option.getLongOpt()); + pyArgList.add(option.getValue()); + } + } + String[] newArgs = pyArgList.toArray(new String[rawArgs.length + pyArgList.size()]); + System.arraycopy(rawArgs, 0, newArgs, pyArgList.size(), rawArgs.length); + args = newArgs; + } else { + args = super.extractProgramArgs(line); + } + + return args; + } + + @Override + public void validate() throws CliArgsException {} + + public static boolean isPython(CommandLine line) { + return line.hasOption(PY_OPTION.getOpt()) || + line.hasOption(PYMODULE_OPTION.getOpt()) || + "org.apache.flink.client.python.PythonGatewayServer".equals(line.getOptionValue(CLASS_OPTION.getOpt())); + } + + public static boolean containsPythonDependency(CommandLine line) { Review comment: Rename to containsPythonDependencyOptions? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
