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_r285000918
 
 

 ##########
 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();
+
+               Path tmpDirPath = new Path(tmpDir);
+               FileSystem fs = tmpDirPath.getFileSystem();
+               if (fs.exists(tmpDirPath)) {
+                       fs.delete(tmpDirPath, true);
+               }
+               fs.mkdirs(tmpDirPath);
+
+               env.workingDirectory = tmpDirPath.toString();
+
+               StringBuilder pythonPathEnv = new StringBuilder();
+
+               pythonPathEnv.append(env.workingDirectory);
+
+               // 2. copy flink python libraries to tmp dir and set them in 
PYTHONPATH.
+               final String[] libs = {PYFLINK_PY4J_FILENAME, 
PYFLINK_LIB_ZIP_FILENAME};
+               for (String lib : libs) {
+                       String sourceFilePath = FLINK_OPT_DIR_PYTHON + 
File.separator + lib;
+                       String targetFilePath = env.workingDirectory + 
File.separator + lib;
+                       copyPyflinkLibToTarget(sourceFilePath, targetFilePath);
+                       pythonPathEnv.append(File.pathSeparator);
+                       pythonPathEnv.append(targetFilePath);
+               }
+
+               // 3. copy relevant python files to tmp dir and set them in 
PYTHONPATH.
+               filePathMap.forEach((sourceFileName, sourcePath) -> {
+                       Path targetPath = new Path(tmpDirPath, sourceFileName);
+                       try {
+                               PythonUtil.copy(sourcePath, targetPath);
+                       } catch (IOException e) {
+                               LOG.error("copy the file {} to tmp dir failed", 
sourceFileName);
+                       }
+
+                       String targetFileName = targetPath.toString();
+                       if (isNeedAddPath(targetFileName, libs)) {
+                               pythonPathEnv.append(File.pathSeparator);
+                               pythonPathEnv.append(targetFileName);
+                       }
+               });
+
+               env.pythonPath = pythonPathEnv.toString();
+               return env;
+       }
+
+       /**
+        * Is need to add the path to PYTHONPATH.
+        *
+        * @param fileName
+        * @param libs
+        * @return
+        */
+       private static boolean isNeedAddPath(String fileName, String[] libs) {
+               if (fileName.endsWith(".zip")) {
+                       for (String lib : libs) {
+                               if (fileName.endsWith(lib)) {
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+               return false;
+       }
+
+       /**
+        * Copy local pyflink lib to tmp file.
+        *
+        * @param sourceFilePath
+        * @param targetFilePath
+        * @throws IOException
+        */
+       public static void copyPyflinkLibToTarget(String sourceFilePath, String 
targetFilePath) throws IOException {
 
 Review comment:
   Can use FileUtils.copy

----------------------------------------------------------------
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

Reply via email to