forked from apache/kudu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_version_info.py
executable file
·132 lines (116 loc) · 4.42 KB
/
gen_version_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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.
#
# This script generates a header file which contains definitions
# for the current Kudu build (eg timestamp, git hash, etc)
import hashlib
import logging
import optparse
import os
import re
import subprocess
import sys
import time
from time import strftime, localtime
from kudu_util import check_output, init_logging
def output_up_to_date(path, id_hash):
"""
Return True if the old file seems to be up to date, based on the
identification hash 'id_hash'.
"""
if not os.path.exists(path):
return False
f = open(path).read()
m = re.search("id_hash=(\w+)", f)
if not m:
return False
return m.group(1) == id_hash
def main():
parser = optparse.OptionParser(
usage="usage: %prog --version=<version> <output path>")
parser.add_option("-v", "--version", help="Set version number", type="string",
dest="version", metavar="VERSION")
parser.add_option("-b", "--build-type", help="Set build type", type="string",
dest="build_type", metavar="BUILD_TYPE")
parser.add_option("-g", "--git-hash", help="Set git hash", type="string",
dest="git_hash", metavar="GIT_HASH")
opts, args = parser.parse_args()
if not opts.version:
parser.error("no version number specified")
sys.exit(1)
if len(args) != 1:
parser.error("no output path specified")
sys.exit(1)
output_path = args[0]
hostname = check_output(["hostname", "-f"]).strip().decode('utf-8')
build_time = "%s %s" % (strftime("%d %b %Y %H:%M:%S", localtime()), time.tzname[0])
username = os.getenv("USER")
if opts.git_hash:
# Git hash provided on the command line.
git_hash = opts.git_hash
clean_repo = "true"
else:
try:
# No command line git hash, find it in the local git repository.
git_hash = check_output(["git", "rev-parse", "HEAD"]).strip().decode('utf-8')
clean_repo = subprocess.call("git diff --quiet && git diff --cached --quiet", shell=True) == 0
clean_repo = str(clean_repo).lower()
except Exception:
# If the git commands failed, we're probably building outside of a git
# repository.
logging.info("Build appears to be outside of a git repository... " +
"continuing without repository information.")
git_hash = "non-git-build"
clean_repo = "true"
version_string = opts.version
build_type = opts.build_type
# Add the Jenkins build ID
build_id = os.getenv("BUILD_ID", "")
# Calculate an identifying hash based on all of the variables except for the
# timestamp. We put this hash in a comment, and use it to check whether to
# re-generate the file. If it hasn't changed since a previous run, we don't
# re-write the file. This avoids having to rebuild all binaries on every build.
identifying_hash = hashlib.sha1(repr((git_hash, hostname, username,
clean_repo, build_id)).encode('utf-8')).hexdigest()
if output_up_to_date(output_path, identifying_hash):
return 0
d = os.path.dirname(output_path)
if not os.path.exists(d):
os.makedirs(d)
with open(output_path, "w") as f:
f.write("""
// THIS FILE IS AUTO-GENERATED! DO NOT EDIT!
//
// id_hash=%(identifying_hash)s
#ifndef VERSION_INFO_H_
#define VERSION_INFO_H_
#define KUDU_GIT_HASH "%(git_hash)s"
#define KUDU_BUILD_HOSTNAME "%(hostname)s"
#define KUDU_BUILD_TIMESTAMP "%(build_time)s"
#define KUDU_BUILD_USERNAME "%(username)s"
#define KUDU_BUILD_CLEAN_REPO %(clean_repo)s
#define KUDU_BUILD_ID "%(build_id)s"
#define KUDU_BUILD_TYPE "%(build_type)s"
#define KUDU_VERSION_STRING "%(version_string)s"
#endif
""" % locals())
return 0
if __name__ == "__main__":
init_logging()
sys.exit(main())