Skip to content

Commit

Permalink
Use python xml to parse pom for version instead of xmllint (apache#3562)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat authored Feb 13, 2019
1 parent 06e911d commit f0fba94
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
4 changes: 1 addition & 3 deletions docker/get-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ ROOT_DIR=$(git rev-parse --show-toplevel)
pushd $ROOT_DIR > /dev/null

# Get the project version from the Maven pom.xml
cat pom.xml | xmllint --format - \
| sed "s/xmlns=\".*\"//g" | xmllint --stream --pattern /project/version --debug - \
| grep -A 2 "matches pattern" | grep text | sed "s/.* [0-9] //g"
src/get-project-version.py

popd > /dev/null
2 changes: 1 addition & 1 deletion pulsar-client-cpp/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

file(GLOB PULSAR_SOURCES *.cc lz4/*.c checksum/*.cc stats/*.cc c/*.cc auth/*.cc auth/athenz/*.cc)

execute_process(COMMAND cat ../pom.xml COMMAND xmllint --format - COMMAND sed "s/xmlns=\".*\"//g" COMMAND xmllint --stream --pattern /project/version --debug - COMMAND grep -A 2 "matches pattern" COMMAND grep text COMMAND sed "s/.* [0-9] //g" OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE PV)
execute_process(COMMAND ../src/get-project-version.py OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE PV)
set (CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -D_PULSAR_VERSION_=\\\"${PV}\\\"")

# Protobuf generation is only supported natively starting from CMake 3.8
Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-cpp/pkg/deb/build-deb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cd /pulsar
SRC_ROOT_DIR=$(git rev-parse --show-toplevel)
cd $SRC_ROOT_DIR/pulsar-client-cpp/pkg/deb

POM_VERSION=`cat ../../../pom.xml | xmllint --format - | sed "s/xmlns=\".*\"//g" | xmllint --stream --pattern /project/version --debug - | grep -A 2 "matches pattern" | grep text | sed "s/.* [0-9] //g"`
POM_VERSION=`$SRC_ROOT_DIR/src/get-project-version.py`
# Sanitize VERSION by removing `SNAPSHOT` if any since it's not legal in DEB
VERSION=`echo $POM_VERSION | awk -F- '{print $1}'`

Expand Down
2 changes: 1 addition & 1 deletion pulsar-client-cpp/pkg/rpm/build-rpm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cd /pulsar
ROOT_DIR=$(git rev-parse --show-toplevel)
cd $ROOT_DIR/pulsar-client-cpp/pkg/rpm

POM_VERSION=`cat ../../../pom.xml | xmllint --format - | sed "s/xmlns=\".*\"//g" | xmllint --stream --pattern /project/version --debug - | grep -A 2 "matches pattern" | grep text | sed "s/.* [0-9] //g"`
POM_VERSION=`$ROOT_DIR/src/get-project-version.py`

# Sanitize VERSION by removing `-incubating` since it's not legal in RPM
VERSION=`echo $POM_VERSION | awk -F- '{print $1}'`
Expand Down
15 changes: 7 additions & 8 deletions pulsar-client-cpp/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@

from distutils.command import build_ext

import xml.etree.ElementTree as ET
from os.path import dirname, realpath, join

def get_version():
# Get the pulsar version from pom.xml
command = '''cat ../../pom.xml | xmllint --format - | \\
sed "s/xmlns=\\".*\\"//g" | xmllint --stream --pattern /project/version --debug - | \\
grep -A 2 "matches pattern" | grep text | sed "s/.* [0-9] //g"'''
process = subprocess.Popen(['bash', '-c', command], stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
raise 'Failed to get version: ' + error
TOP_LEVEL_PATH = dirname(dirname(dirname(realpath(__file__))))
POM_PATH = join(TOP_LEVEL_PATH, 'pom.xml')
root = ET.XML(open(POM_PATH).read())
version = root.find('{http://maven.apache.org/POM/4.0.0}version').text.strip()

# Strip the '-incubating' suffix, since it prevents the packages
# from being uploaded into PyPI
return output.strip().decode('utf-8', 'strict').split('-')[0]
return version.split('-')[0]


VERSION = get_version()
Expand Down
29 changes: 29 additions & 0 deletions src/get-project-version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env 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.
#

import xml.etree.ElementTree as ET
from os.path import dirname, realpath, join

# Derive the POM path from the current script location
TOP_LEVEL_PATH = dirname(dirname(realpath(__file__)))
POM_PATH = join(TOP_LEVEL_PATH, 'pom.xml')

root = ET.XML(open(POM_PATH).read())
print(root.find('{http://maven.apache.org/POM/4.0.0}version').text)
4 changes: 2 additions & 2 deletions src/stage-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ fi

DEST_PATH=$1

pushd $(dirname "$0")/..
pushd $(dirname "$0")
PULSAR_PATH=$(git rev-parse --show-toplevel)
VERSION=`cat pom.xml | xmllint --format - | sed "s/xmlns=\".*\"//g" | xmllint --stream --pattern /project/version --debug - | grep -A 2 "matches pattern" | grep text | sed "s/.* [0-9] //g"`
VERSION=`./get-project-version.py`
popd

cp $PULSAR_PATH/distribution/server/target/apache-pulsar-$VERSION-src.tar.gz $DEST_PATH
Expand Down

0 comments on commit f0fba94

Please sign in to comment.