beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From al...@apache.org
Subject [1/2] beam git commit: [BEAM-1693] Detect supported Python & pip executables in Python-SDK
Date Mon, 27 Mar 2017 22:33:24 GMT
Repository: beam
Updated Branches:
  refs/heads/master 07274bbfe -> affcca669


[BEAM-1693] Detect supported Python & pip executables in Python-SDK


Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/c7c153a5
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/c7c153a5
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/c7c153a5

Branch: refs/heads/master
Commit: c7c153a5945d351c868f6d99f0c809317d16d6b8
Parents: 07274bb
Author: Tibor Kiss <tibor.kiss@gmail.com>
Authored: Tue Mar 14 20:51:35 2017 +0100
Committer: Ahmet Altay <altay@google.com>
Committed: Mon Mar 27 15:32:51 2017 -0700

----------------------------------------------------------------------
 pom.xml                                |  1 +
 sdks/python/findSupportedPython.groovy | 80 +++++++++++++++++++++++++++++
 sdks/python/pom.xml                    | 31 ++++++++++-
 3 files changed, 110 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/c7c153a5/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index bf1d4f0..e7ae4ca 100644
--- a/pom.xml
+++ b/pom.xml
@@ -137,6 +137,7 @@
     <storage.version>v1-rev71-1.22.0</storage.version>
     <woodstox.version>4.4.1</woodstox.version>
     <spring.version>4.3.5.RELEASE</spring.version>
+    <groovy-maven-plugin.version>2.0</groovy-maven-plugin.version>
     
     <compiler.error.flag>-Werror</compiler.error.flag>
     <compiler.default.pkginfo.flag>-Xpkginfo:always</compiler.default.pkginfo.flag>

http://git-wip-us.apache.org/repos/asf/beam/blob/c7c153a5/sdks/python/findSupportedPython.groovy
----------------------------------------------------------------------
diff --git a/sdks/python/findSupportedPython.groovy b/sdks/python/findSupportedPython.groovy
new file mode 100644
index 0000000..6984132
--- /dev/null
+++ b/sdks/python/findSupportedPython.groovy
@@ -0,0 +1,80 @@
+/**
+ * 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.
+ */
+
+/* This (groovy-maven-plugin) script finds the supported python interpreter and pip
+ * binary in the path. As there is no strict naming convention exists amongst OSes
+ * for Python & pip (some call it python2.7, others name it python-2.7),
+ * the script tries to execute the candidates and query their version.
+ * The first matching interpreter & pip is assigned to "python.interpreter.bin"
+ * and "python.pip.bin" (maven) properties respectively.
+ */
+
+import org.apache.maven.plugin.MojoFailureException
+
+requiredPythonVersion = /.*[Pp]ython 2\.7.*/
+
+pythonCandidates = ["python2.7", "python-2.7", "python2", "python-2", "python"]
+pipCandidates = ["pip2.7", "pip-2.7", "pip2", "pip-2", "pip"]
+
+def String findExecutable(String[] candidates, versionRegex) {
+    for (candidate in candidates) {
+        try {
+            def exec = "${candidate} --version".execute()
+
+            def consoleSB = new StringBuilder()
+            exec.waitForProcessOutput(consoleSB, consoleSB)
+            consoleStr = consoleSB.toString().replaceAll("\\r|\\n", "")
+
+            if (exec.exitValue() == 0 && consoleStr ==~ versionRegex) {
+                return candidate
+            }
+        } catch (IOException e) {
+            continue
+        }
+    }
+    return null
+}
+
+def Boolean isWindows() {
+    return System.properties['os.name'].toLowerCase(Locale.ROOT).contains('windows');
+}
+
+/* On MS Windows applications with dots in the filename can only be executed
+ * if the .exe suffix is also included. That is 'pip2.7' will cause an execution error,
+ * while 'pip2.7.exe' will succeed (given that pip2.7.exe is an executable in the PATH).
+ * The specializeCandidateForOS closure takes care of this conversion.
+ */
+def specializeCandidateForOS = { it -> isWindows() ? it + '.exe' : it }
+
+pythonBin = findExecutable(pythonCandidates.collect(specializeCandidateForOS) as String[],
+                           requiredPythonVersion)
+pipBin = findExecutable(pipCandidates.collect(specializeCandidateForOS) as String[],
+                        requiredPythonVersion)
+
+if (pythonBin == null) {
+   throw new MojoFailureException("Unable to find Python 2.7 in path")
+}
+
+if (pipBin == null) {
+   throw new MojoFailureException("Unable to find pip for Python 2.7 in path")
+}
+
+log.info("Using python interpreter binary '" + pythonBin + "' with pip '" + pipBin + "'")
+
+project.properties.setProperty("python.pip.bin", pipBin)
+project.properties.setProperty("python.interpreter.bin", pythonBin)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/beam/blob/c7c153a5/sdks/python/pom.xml
----------------------------------------------------------------------
diff --git a/sdks/python/pom.xml b/sdks/python/pom.xml
index cb8307a..aa19770 100644
--- a/sdks/python/pom.xml
+++ b/sdks/python/pom.xml
@@ -33,8 +33,8 @@
   <name>Apache Beam :: SDKs :: Python</name>
 
   <properties>
-    <python.interpreter.bin>python2</python.interpreter.bin>
-    <python.pip.bin>pip2</python.pip.bin>
+    <!-- python.interpreter.bin & python.pip.bin
+         is set dynamically by findSupportedPython.groovy -->
     <python.build.base>${project.build.directory}/build</python.build.base>
     <python.user.base>${project.build.directory}/python</python.user.base>
   </properties>
@@ -56,6 +56,33 @@
     </pluginManagement>
     <plugins>
       <plugin>
+        <groupId>org.codehaus.gmaven</groupId>
+        <artifactId>groovy-maven-plugin</artifactId>
+        <version>${groovy-maven-plugin.version}</version>
+        <executions>
+          <execution>
+            <id>find-supported-python-for-clean</id>
+            <phase>pre-clean</phase>
+            <goals>
+              <goal>execute</goal>
+            </goals>
+            <configuration>
+              <source>${project.basedir}/findSupportedPython.groovy</source>
+            </configuration>
+          </execution>
+          <execution>
+            <id>find-supported-python-for-compile</id>
+            <phase>initialize</phase>
+            <goals>
+              <goal>execute</goal>
+            </goals>
+            <configuration>
+              <source>${project.basedir}/findSupportedPython.groovy</source>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>exec-maven-plugin</artifactId>
         <executions>


Mime
View raw message