dianfu commented on a change in pull request #8472: [FLINK-12327][python] Adds support to submit Python Table API job in CliFrontend URL: https://github.com/apache/flink/pull/8472#discussion_r284999602
########## File path: flink-clients/src/main/java/org/apache/flink/client/python/PythonUtil.java ########## @@ -0,0 +1,257 @@ +/* + * 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.python; + +import org.apache.flink.core.fs.FSDataInputStream; +import org.apache.flink.core.fs.FSDataOutputStream; +import org.apache.flink.core.fs.FileStatus; +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.core.fs.Path; +import org.apache.flink.util.FileUtils; +import org.apache.flink.util.IOUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.StandardCopyOption; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** + * The util class help to prepare python env and run the python process. + */ +public class PythonUtil { + private static final Logger LOG = LoggerFactory.getLogger(PythonUtil.class); + + private static final String FLINK_OPT_DIR = System.getenv("FLINK_OPT_DIR"); + + private static final String FLINK_OPT_DIR_PYTHON = FLINK_OPT_DIR + File.separator + "python"; + + private static final String PYFLINK_LIB_ZIP_FILENAME = "pyflink.zip"; + + private static final String PYFLINK_PY4J_FILENAME = "py4j-0.10.8.1-src.zip"; + + /** + * Wrap python exec environment. + */ + public static class PythonEnvironment { + public String workingDirectory; + + public String pythonExec = "python"; + + public String pythonPath; + + Map<String, String> systemEnv = new HashMap<>(); + } + + /** + * The hook thread that delete the tmp working dir of python process after the python process shutdown. + */ + private static class ShutDownPythonHook extends Thread { + private Process p; + private String pyFileDir; + + public ShutDownPythonHook(Process p, String pyFileDir) { + this.p = p; + this.pyFileDir = pyFileDir; + } + + public void run() { + + p.destroyForcibly(); + + if (pyFileDir != null) { + File pyDir = new File(pyFileDir); + FileUtils.deleteDirectoryQuietly(pyDir); + } + } + } + + + /** + * Prepare PythonEnvironment to start python process. + * + * @param filePathMap + * @return PythonEnvironment + * @throws IOException + */ + public static PythonEnvironment preparePythonEnvironment(Map<String, Path> filePathMap) throws IOException { + PythonEnvironment env = new PythonEnvironment(); + + // 1. setup temporary local directory for the user files + String tmpDir = System.getProperty("java.io.tmpdir") + + File.separator + "pyflink_tmp_" + UUID.randomUUID(); Review comment: pyflink_tmp_ -> pyflink ---------------------------------------------------------------- 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
