starrocks/build-support/gen_build_version.py

240 lines
9.3 KiB
Python
Executable File

#! /usr/bin/python3
# Copyright 2021-present StarRocks, Inc. All rights reserved.
#
# Licensed 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
#
# https:#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 argparse
import hashlib
import os
import platform
import re
import subprocess
import sys
from datetime import datetime
OS_RELEASE_PATH = "/etc/os-release"
def get_version():
version = os.getenv("STARROCKS_VERSION")
if not version:
version = "UNKNOWN"
return version
def get_commit_hash():
commit_hash = os.getenv("STARROCKS_COMMIT_HASH")
if not commit_hash:
commit_hash = "UNKNOWN"
return commit_hash
def get_build_type():
build_type = os.getenv("BUILD_TYPE")
if not build_type:
build_type = "UNKNOWN"
return build_type.upper()
def get_current_time():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def get_user():
user = os.getenv("USER")
if not user:
user = "StarRocks"
return user
def get_hostname():
if os.path.exists('/.dockerenv'):
return "docker"
res = subprocess.Popen(["hostname", "-f"], stdout=subprocess.PIPE)
out, _ = res.communicate()
return out.decode('utf-8').strip()
def get_build_distro_info():
""" parse /etc/os-release and load the info into a dictionary
Different linux distributor may have different contents in the file.
This script is only interested in `ID` and `PRETTY_NAME` which are available
on both centos7 and ubuntu22.04.
"""
distro_info = dict()
if not os.path.exists(OS_RELEASE_PATH):
return distro_info
with open(OS_RELEASE_PATH) as fp_handle:
line_pattern = re.compile(r'^(?P<name>\w+)=(?P<value>.*)$')
for line in fp_handle:
result = line_pattern.match(line.strip())
if result:
key = result.group("name").strip()
value = result.group("value").strip()
if value.startswith('"') and value.endswith('"'):
# expect the value is well-formatted, either with "" or without
value = value[1:-1]
distro_info[key] = value
return distro_info
def get_build_arch():
return platform.uname().machine
def get_java_version():
java_home = os.getenv("JAVA_HOME")
java_cmd = None
if java_home:
java_cmd = java_home + "/bin/java"
else:
# Fallback to system java on PATH
java_cmd = "java"
try:
java_res = subprocess.Popen([java_cmd, "-fullversion"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = java_res.communicate()
if java_res.returncode == 0:
return out.decode('utf-8').replace("\"", "\\\"").strip()
except Exception:
pass
return "unknown jdk"
def get_fingerprint(items):
if not isinstance(items, list):
items = [items]
return hashlib.md5(",".join(items).encode()).hexdigest()
def skip_write_if_fingerprint_unchanged(file_name, file_content, fingerprint):
if os.path.exists(file_name):
with open(file_name) as fh:
data = fh.read()
m = re.search(r"FINGERPRINT: (?P<fingerprint>\w+)", data)
old_fingerprint = m.group('fingerprint') if m else None
print('gen_build_version.py {}: old fingerprint = {}, new fingerprint = {}'.format(file_name, old_fingerprint, fingerprint))
if old_fingerprint == fingerprint:
return
with open(file_name, 'w') as fh:
fh.write(file_content)
def generate_java_file(java_path, version, commit_hash, build_type, build_time, user, host, java_version, build_distro_id, build_arch):
file_format = '''
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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 is a generated file, DO NOT EDIT IT.
// FINGERPRINT: {FINGERPRINT}
package com.starrocks.common;
public class Version {{
public static final String STARROCKS_VERSION = "{VERSION}";
public static final String STARROCKS_COMMIT_HASH = "{COMMIT_HASH}";
public static final String STARROCKS_BUILD_TYPE = "{BUILD_TYPE}";
public static final String STARROCKS_BUILD_TIME = "{BUILD_TIME}";
public static final String STARROCKS_BUILD_USER = "{BUILD_USER}";
public static final String STARROCKS_BUILD_HOST = "{BUILD_HOST}";
public static final String STARROCKS_BUILD_DISTRO_ID = "{BUILD_DISTRO_ID}";
public static final String STARROCKS_BUILD_ARCH = "{BUILD_ARCH}";
public static final String STARROCKS_JAVA_COMPILE_VERSION = "{JAVA_VERSION}";
}}
'''
fingerprint = get_fingerprint([version, commit_hash, build_type, user, host, java_version, build_distro_id, build_arch])
file_content = file_format.format(VERSION=version, COMMIT_HASH=commit_hash,
BUILD_TYPE=build_type, BUILD_TIME=build_time,
BUILD_USER=user, BUILD_HOST=host, BUILD_DISTRO_ID=build_distro_id, BUILD_ARCH=build_arch,
JAVA_VERSION=java_version, FINGERPRINT=fingerprint)
file_name = java_path + "/com/starrocks/common/Version.java"
d = os.path.dirname(file_name)
if not os.path.exists(d):
os.makedirs(d)
skip_write_if_fingerprint_unchanged(file_name, file_content, fingerprint)
def generate_cpp_file(cpp_path, version, commit_hash, build_type, build_time, user, host, build_distro_id, build_arch):
file_format = '''
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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.
// NOTE: This is a generated file, DO NOT EDIT IT
// FINGERPRINT: {FINGERPRINT}
namespace starrocks {{
const char* STARROCKS_VERSION = "{VERSION}";
const char* STARROCKS_COMMIT_HASH = "{COMMIT_HASH}";
const char* STARROCKS_BUILD_TIME = "{BUILD_TIME}";
const char* STARROCKS_BUILD_USER = "{BUILD_USER}";
const char* STARROCKS_BUILD_HOST = "{BUILD_HOST}";
const char* STARROCKS_BUILD_DISTRO_ID = "{BUILD_DISTRO_ID}";
const char* STARROCKS_BUILD_ARCH = "{BUILD_ARCH}";
}}
'''
fingerprint = get_fingerprint([version, commit_hash, build_type, user, host, build_distro_id, build_arch])
file_content = file_format.format(VERSION=version, COMMIT_HASH=commit_hash,
BUILD_TYPE=build_type, BUILD_TIME=build_time,
BUILD_USER=user, BUILD_HOST=host, BUILD_DISTRO_ID=build_distro_id, BUILD_ARCH=build_arch, FINGERPRINT=fingerprint)
file_name = cpp_path + "/version.cpp"
d = os.path.dirname(file_name)
if not os.path.exists(d):
os.makedirs(d)
skip_write_if_fingerprint_unchanged(file_name, file_content, fingerprint)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--cpp", dest='cpp_path', default="", help="Path of generated cpp file", type=str)
parser.add_argument("--java", dest='java_path', default="", help="Path of generated java file", type=str)
args = parser.parse_args()
if not args.cpp_path and not args.java_path:
print("Neither --cpp nor --java provided, do nothing!", file=sys.stderr)
return False
version = get_version()
commit_hash = get_commit_hash()
build_type = get_build_type()
build_time = get_current_time()
distro_info = get_build_distro_info()
build_distro_id = distro_info.get("ID", "unknown")
build_pretty_name = distro_info.get("PRETTY_NAME", build_distro_id)
build_arch = get_build_arch()
user = get_user()
# append build distro pretty name into hostname
hostname = '%s (%s)' % (get_hostname(), build_pretty_name)
java_version = get_java_version()
if args.cpp_path:
generate_cpp_file(args.cpp_path, version, commit_hash, build_type, build_time, user, hostname, build_distro_id, build_arch)
if args.java_path:
generate_java_file(args.java_path, version, commit_hash, build_type, build_time, user, hostname, java_version, build_distro_id, build_arch)
if __name__ == '__main__':
main()