Ver código fonte

HADOOP-8924. Hadoop Common creating package-info.java must not depend on sh. Contributed by Chris Nauroth.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-trunk-win@1398865 13f79535-47bb-0310-9956-ffa450edef68
Suresh Srinivas 12 anos atrás
pai
commit
92343af0b8

+ 154 - 0
dev-support/saveVersion.py

@@ -0,0 +1,154 @@
+#! /usr/bin/python
+
+# 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 file is used to generate package-info.java with an annotation that
+# records the version, revision, user, date, url, and srcChecksum
+
+import os
+import sys
+import re
+import subprocess
+import getpass
+import time
+import hashlib
+
+def usage():
+  print >>sys.stderr, "Usage: saveVersion.py <annotation> <version> <src_checksum_root_dir> <package> <build_dir>"
+  print >>sys.stderr, "Eg:    saveVersion.py @HadoopVersionAnnotation 1.1.0 src org.apache.hadoop build"
+
+
+# This template is what gets written to package-info.java.
+# It is filled out by (annotation, version, revision, user, date, url,
+# srcChecksum, package)
+
+template_string = \
+'''/*
+ * Generated by saveVersion.py
+ */
+%s(version="%s", revision="%s",
+  user="%s", date="%s", url="%s",
+  srcChecksum="%s")
+package %s;
+'''
+
+# Convert Windows paths with back-slashes into canonical Unix paths
+# with forward slashes.  Unix paths are unchanged.  Mixed formats
+# just naively have back-slashes converted to forward slashes.
+def canonicalpath(path):
+  return re.sub('\\\\','/',path)
+
+# This is an implementation of md5sum-like functionality in pure python.
+# The path name is output in canonical form to get the same aggregate
+# checksum as Linux.
+def md5sum(fname):
+  block_size = 2**20
+  f = open(os.path.normpath(fname), 'rb')  # open in binary mode to make sure EOLs don't get munged
+  md5 = hashlib.md5()
+  while True:
+    data = f.read(block_size)
+    if not data:
+      break
+    md5.update(data)
+  f.close()
+  return md5.hexdigest()
+
+# Roughly emulate the Python 2.7 functionality of "subprocess.check_output()"
+# by accepting a CLI vector [cmd, arg1, arg2, ...], and returning its stdout result,
+# but without the "checking".  For use in lower version Python interpreters.
+def subprocessOutput(args):
+  # result = subprocess.check_output(args, shell=True) # requires py2.7
+  process = subprocess.Popen(args, stdout=subprocess.PIPE)
+  result = process.communicate()[0].strip()
+  return result
+
+# Strip surrounding quotes from a string object that carried its quotes with it.
+def stripquotes(s):
+  if (s[0] == '"' and s[-1] == '"') or (s[0] == "'" and s[-1] == "'") :
+    return s[1:-1]
+  elif (s[0] == '\\' and s[-1] == '"') :
+    return s[1:-1]
+  else:
+    return s
+
+
+def main(argv=None):
+  if argv is None:
+    argv = sys.argv[1:]
+  if (len(argv) != 5) or (argv[0] == "--help") or (argv[0] == "-h"):
+    usage()
+    return -1
+
+  annotation = argv[0]
+  version = argv[1]
+  src_checksum_root_dir = argv[2]
+  package = argv[3]
+  build_dir = argv[4]
+
+  user = getpass.getuser()
+  date = time.strftime("%a %b %d %H:%M:%S %Z %Y")  # simulate `date`
+
+  os.chdir(os.path.normpath(os.path.join(os.path.dirname(sys.argv[0]), "..")))
+  if os.path.isdir(".git"):
+    revision = stripquotes(subprocessOutput(['git', 'log', '-1', '--pretty=format:"%H"']))
+    origin = subprocessOutput(['git', 'config', '--get', 'remote.origin.url'])
+    branch = subprocessOutput(['git', 'branch'])
+
+    filter_current_branch = re.compile(r'^\* (.*)$', re.MULTILINE)
+    current_branch = filter_current_branch.search(branch).group(1).strip()
+    url = "%s on branch %s" % (origin, current_branch)
+  else:
+    svn_info = subprocessOutput(['svn', 'info'])
+    filter_last_revision = re.compile(r'^Last Changed Rev: (.*)$', re.MULTILINE)
+    revision = filter_last_revision.search(svn_info).group(1).strip()
+
+    filter_url = re.compile(r'^URL: (.*)$', re.MULTILINE)
+    url = filter_url.search(svn_info).group(1).strip()
+
+  filter_java = re.compile(r'.+\.java$')
+  file_list = []
+  for root, dirs, files in os.walk(os.path.normpath(src_checksum_root_dir)):
+    for name in files:
+      if filter_java.match(name):
+        canonical_name = canonicalpath(os.path.join(root, name))
+        if not 'generated-sources' in canonical_name:
+          file_list.append(canonical_name)
+
+  # Sorting is done on unix-format names, case-folded, in order to get a platform-independent sort.
+  file_list.sort(key=str.upper)
+  file_count = len(file_list)
+  hash = hashlib.md5()
+  for name in file_list:
+    file_hash_string = md5sum(name)
+    hash.update(file_hash_string)
+
+  srcChecksum = hash.hexdigest()
+
+  target_dir = os.path.normpath(build_dir)
+  if not os.path.exists(target_dir):
+    os.makedirs(target_dir)
+
+  target_file = os.path.join(target_dir, 'package-info.java')
+  fout = open(target_file, "w")
+  fout.write(template_string % (annotation, version, revision, user, date, url, srcChecksum, package))
+  fout.close()
+
+  print("Checksummed %s src/**.java files" % file_count)
+  return 0
+
+##########################
+if __name__ == "__main__":
+  sys.exit(main())

+ 5 - 0
hadoop-common-project/hadoop-common/CHANGES.branch-trunk-win.txt

@@ -0,0 +1,5 @@
+branch-trunk-win changes - unreleased
+
+  HADOOP-8924. Hadoop Common creating package-info.java must not depend on sh.
+  (Chris Nauroth via suresh)
+

+ 0 - 67
hadoop-common-project/hadoop-common/dev-support/saveVersion.sh

@@ -1,67 +0,0 @@
-#!/bin/sh
-
-# 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 file is used to generate the package-info.java class that
-# records the version, revision, branch, user, timestamp, and url
-unset LANG
-unset LC_CTYPE
-unset LC_TIME
-version=$1
-build_dir=$2
-user=`whoami | tr '\n\r' '\n'`
-date=`date`
-cwd=`pwd`
-if git rev-parse HEAD 2>/dev/null > /dev/null ; then
-  revision=`git log -1 --pretty=format:"%H"`
-  hostname=`hostname`
-  branch=`git branch | sed -n -e 's/^* //p'`
-  url="git://${hostname}${cwd}"
-elif [ -d .svn ]; then
-  revision=`svn info | sed -n -e 's/Last Changed Rev: \(.*\)/\1/p'`
-  url=`svn info | sed -n -e 's/^URL: \(.*\)/\1/p'`
-  # Get canonical branch (branches/X, tags/X, or trunk)
-  branch=`echo $url | sed -n -e 's,.*\(branches/.*\)$,\1,p' \
-                             -e 's,.*\(tags/.*\)$,\1,p' \
-                             -e 's,.*trunk$,trunk,p'`
-else
-  revision="Unknown"
-  branch="Unknown"
-  url="file://$cwd"
-fi
-
-which md5sum > /dev/null
-if [ "$?" = "0" ] ; then
-  srcChecksum=`find src/main/java -name '*.java' | LC_ALL=C sort | xargs md5sum | md5sum | cut -d ' ' -f 1`
-else
-  srcChecksum="Not Available"
-fi
-
-mkdir -p $build_dir/org/apache/hadoop
-cat << EOF | \
-  sed -e "s/VERSION/$version/" -e "s/USER/$user/" -e "s/DATE/$date/" \
-      -e "s|URL|$url|" -e "s/REV/$revision/" \
-      -e "s|BRANCH|$branch|" -e "s/SRCCHECKSUM/$srcChecksum/" \
-      > $build_dir/org/apache/hadoop/package-info.java
-/*
- * Generated by src/saveVersion.sh
- */
-@HadoopVersionAnnotation(version="VERSION", revision="REV", branch="BRANCH",
-                         user="USER", date="DATE", url="URL",
-                         srcChecksum="SRCCHECKSUM")
-package org.apache.hadoop;
-EOF

+ 18 - 16
hadoop-common-project/hadoop-common/pom.xml

@@ -288,22 +288,6 @@
               </target>
             </configuration>
           </execution>
-          <execution>
-            <id>save-version</id>
-            <phase>generate-sources</phase>
-            <goals>
-              <goal>run</goal>
-            </goals>
-            <configuration>
-              <target>
-                <mkdir dir="${project.build.directory}/generated-sources/java"/>
-                <exec executable="sh">
-                  <arg
-                      line="${basedir}/dev-support/saveVersion.sh ${project.version} ${project.build.directory}/generated-sources/java"/>
-                </exec>
-              </target>
-            </configuration>
-          </execution>
           <execution>
             <id>generate-test-sources</id>
             <phase>generate-test-sources</phase>
@@ -364,6 +348,24 @@
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>exec-maven-plugin</artifactId>
         <executions>
+          <execution>
+            <id>save-version</id>
+            <phase>generate-sources</phase>
+            <goals>
+              <goal>exec</goal>
+            </goals>
+            <configuration>
+              <executable>python</executable>
+              <arguments>
+                <argument>${basedir}/../../dev-support/saveVersion.py</argument>
+                <argument>@HadoopVersionAnnotation</argument>
+                <argument>${project.version}</argument>
+                <argument>${basedir}/src/main/java</argument>
+                <argument>org.apache.hadoop</argument>
+                <argument>${project.build.directory}/generated-sources/java/org/apache/hadoop</argument>
+              </arguments>
+            </configuration>
+          </execution>
           <execution>
             <id>compile-proto</id>
             <phase>generate-sources</phase>