2024-01-23 20:42:34 +00:00
|
|
|
#!/usr/bin/env python3
|
2016-02-22 21:07:19 +00:00
|
|
|
|
2016-09-01 17:04:00 +00:00
|
|
|
# Copyright (c) 2016 Google Inc.
|
2016-02-22 21:07:19 +00:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-04-22 00:50:11 +00:00
|
|
|
# Updates an output file with version info unless the new content is the same
|
|
|
|
# as the existing content.
|
2016-02-22 21:07:19 +00:00
|
|
|
#
|
2023-03-09 17:24:20 +00:00
|
|
|
# Args: <changes-file> <output-file>
|
2016-02-22 21:07:19 +00:00
|
|
|
#
|
2016-04-22 00:50:11 +00:00
|
|
|
# The output file will contain a line of text consisting of two C source syntax
|
|
|
|
# string literals separated by a comma:
|
2023-03-09 17:24:20 +00:00
|
|
|
# - The software version deduced from the given CHANGES file.
|
2016-04-22 00:50:11 +00:00
|
|
|
# - A longer string with the project name, the software version number, and
|
2023-03-09 17:24:20 +00:00
|
|
|
# git commit information for the CHANGES file's directory. The commit
|
|
|
|
# information is the output of "git describe" if that succeeds, or "git
|
|
|
|
# rev-parse HEAD" if that succeeds, or otherwise a message containing the
|
|
|
|
# phrase "unknown hash".
|
2016-04-22 00:50:11 +00:00
|
|
|
# The string contents are escaped as necessary.
|
2016-02-22 21:07:19 +00:00
|
|
|
|
|
|
|
import datetime
|
2016-08-12 18:19:17 +00:00
|
|
|
import errno
|
2017-01-17 18:32:28 +00:00
|
|
|
import os
|
2016-02-22 21:07:19 +00:00
|
|
|
import os.path
|
2016-04-22 00:50:11 +00:00
|
|
|
import re
|
2016-02-22 21:07:19 +00:00
|
|
|
import subprocess
|
2023-01-27 14:36:09 +00:00
|
|
|
import logging
|
2016-02-22 21:07:19 +00:00
|
|
|
import sys
|
2017-01-17 18:32:28 +00:00
|
|
|
import time
|
2016-02-22 21:07:19 +00:00
|
|
|
|
2023-01-27 14:36:09 +00:00
|
|
|
# Format of the output generated by this script. Example:
|
|
|
|
# "v2023.1", "SPIRV-Tools v2023.1 0fc5526f2b01a0cc89192c10cf8bef77f1007a62, 2023-01-18T14:51:49"
|
|
|
|
OUTPUT_FORMAT = '"{version_tag}", "SPIRV-Tools {version_tag} {description}"\n'
|
2016-03-06 18:00:58 +00:00
|
|
|
|
2016-08-12 18:19:17 +00:00
|
|
|
def mkdir_p(directory):
|
|
|
|
"""Make the directory, and all its ancestors as required. Any of the
|
|
|
|
directories are allowed to already exist."""
|
|
|
|
|
2017-03-20 15:23:35 +00:00
|
|
|
if directory == "":
|
|
|
|
# We're being asked to make the current directory.
|
|
|
|
return
|
|
|
|
|
2016-08-12 18:19:17 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(directory)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.EEXIST and os.path.isdir(directory):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2016-08-04 22:25:42 +00:00
|
|
|
def command_output(cmd, directory):
|
2016-02-24 17:41:29 +00:00
|
|
|
"""Runs a command in a directory and returns its standard output stream.
|
|
|
|
|
2023-11-10 23:43:11 +00:00
|
|
|
Returns (False, None) if the command fails to launch or otherwise fails.
|
2016-02-24 17:41:29 +00:00
|
|
|
"""
|
2023-01-27 14:36:09 +00:00
|
|
|
try:
|
2023-02-23 03:24:34 +00:00
|
|
|
# Set shell=True on Windows so that Chromium's git.bat can be found when
|
|
|
|
# 'git' is invoked.
|
2023-01-27 14:36:09 +00:00
|
|
|
p = subprocess.Popen(cmd,
|
|
|
|
cwd=directory,
|
|
|
|
stdout=subprocess.PIPE,
|
2023-02-23 03:24:34 +00:00
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
shell=os.name == 'nt')
|
2023-11-10 23:43:11 +00:00
|
|
|
(stdout, _) = p.communicate()
|
2023-01-27 14:36:09 +00:00
|
|
|
if p.returncode != 0:
|
2023-11-10 23:43:11 +00:00
|
|
|
return False, None
|
2023-01-27 14:36:09 +00:00
|
|
|
except Exception as e:
|
|
|
|
return False, None
|
|
|
|
return p.returncode == 0, stdout
|
2016-02-24 17:41:29 +00:00
|
|
|
|
2023-03-09 17:24:20 +00:00
|
|
|
def deduce_software_version(changes_file):
|
|
|
|
"""Returns a tuple (success, software version number) parsed from the
|
|
|
|
given CHANGES file.
|
2016-04-22 00:50:11 +00:00
|
|
|
|
2023-03-09 17:24:20 +00:00
|
|
|
Success is set to True if the software version could be deduced.
|
|
|
|
Software version is undefined if success if False.
|
|
|
|
Function expects the CHANGES file to describes most recent versions first.
|
|
|
|
"""
|
2016-04-22 00:50:11 +00:00
|
|
|
|
2023-03-09 17:24:20 +00:00
|
|
|
# Match the first well-formed version-and-date line
|
|
|
|
# Allow trailing whitespace in the checked-out source code has
|
|
|
|
# unexpected carriage returns on a linefeed-only system such as
|
|
|
|
# Linux.
|
|
|
|
pattern = re.compile(r'^(v\d+\.\d+(-dev)?) \d\d\d\d-\d\d-\d\d\s*$')
|
|
|
|
with open(changes_file, mode='r') as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
match = pattern.match(line)
|
|
|
|
if match:
|
|
|
|
return True, match.group(1)
|
2023-01-30 18:23:41 +00:00
|
|
|
return False, None
|
2016-04-22 00:50:11 +00:00
|
|
|
|
2023-01-30 18:23:41 +00:00
|
|
|
|
2023-03-09 17:24:20 +00:00
|
|
|
def describe(repo_path):
|
2023-01-30 18:23:41 +00:00
|
|
|
"""Returns a string describing the current Git HEAD version as descriptively
|
2023-03-09 17:24:20 +00:00
|
|
|
as possible.
|
|
|
|
|
|
|
|
Runs 'git describe', or alternately 'git rev-parse HEAD', in directory. If
|
|
|
|
successful, returns the output; otherwise returns 'unknown hash, <date>'."""
|
2016-02-24 17:41:29 +00:00
|
|
|
|
2023-09-11 20:52:31 +00:00
|
|
|
# if we're in a git repository, attempt to extract version info
|
2023-10-18 16:49:51 +00:00
|
|
|
success, output = command_output(["git", "rev-parse", "--show-toplevel"], repo_path)
|
|
|
|
if success:
|
2023-11-13 16:28:30 +00:00
|
|
|
success, output = command_output(["git", "describe", "--tags", "--match=v*", "--long"], repo_path)
|
2023-09-11 20:52:31 +00:00
|
|
|
if not success:
|
2023-10-18 16:49:51 +00:00
|
|
|
success, output = command_output(["git", "rev-parse", "HEAD"], repo_path)
|
2023-09-11 20:52:31 +00:00
|
|
|
|
|
|
|
if success:
|
|
|
|
# decode() is needed here for Python3 compatibility. In Python2,
|
|
|
|
# str and bytes are the same type, but not in Python3.
|
|
|
|
# Popen.communicate() returns a bytes instance, which needs to be
|
|
|
|
# decoded into text data first in Python3. And this decode() won't
|
|
|
|
# hurt Python2.
|
|
|
|
return output.rstrip().decode()
|
2023-01-27 14:36:09 +00:00
|
|
|
|
|
|
|
# This is the fallback case where git gives us no information,
|
2023-11-10 23:43:11 +00:00
|
|
|
# e.g. because the source tree might not be in a git tree or
|
|
|
|
# git is not available on the system.
|
2023-01-27 14:36:09 +00:00
|
|
|
# In this case, usually use a timestamp. However, to ensure
|
|
|
|
# reproducible builds, allow the builder to override the wall
|
|
|
|
# clock time with environment variable SOURCE_DATE_EPOCH
|
|
|
|
# containing a (presumably) fixed timestamp.
|
2023-03-09 17:24:20 +00:00
|
|
|
timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
|
2023-11-15 15:36:36 +00:00
|
|
|
iso_date = datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc).isoformat()
|
2023-03-09 17:24:20 +00:00
|
|
|
return "unknown hash, {}".format(iso_date)
|
2023-01-30 18:23:41 +00:00
|
|
|
|
2016-02-22 21:07:19 +00:00
|
|
|
def main():
|
2023-01-27 14:36:09 +00:00
|
|
|
FORMAT = '%(asctime)s %(message)s'
|
|
|
|
logging.basicConfig(format="[%(asctime)s][%(levelname)-8s] %(message)s", datefmt="%H:%M:%S")
|
2016-04-22 00:50:11 +00:00
|
|
|
if len(sys.argv) != 3:
|
2023-01-27 14:36:09 +00:00
|
|
|
logging.error("usage: {} <repo-path> <output-file>".format(sys.argv[0]))
|
2016-02-22 21:07:19 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2023-03-09 17:24:20 +00:00
|
|
|
changes_file_path = os.path.realpath(sys.argv[1])
|
2023-01-27 14:36:09 +00:00
|
|
|
output_file_path = sys.argv[2]
|
|
|
|
|
2023-03-09 17:24:20 +00:00
|
|
|
success, version = deduce_software_version(changes_file_path)
|
2023-01-27 14:36:09 +00:00
|
|
|
if not success:
|
2023-03-09 17:24:20 +00:00
|
|
|
logging.error("Could not deduce latest release version from {}.".format(changes_file_path))
|
|
|
|
sys.exit(1)
|
2023-01-27 14:36:09 +00:00
|
|
|
|
2023-03-09 17:24:20 +00:00
|
|
|
repo_path = os.path.dirname(changes_file_path)
|
|
|
|
description = describe(repo_path)
|
2023-01-27 14:36:09 +00:00
|
|
|
content = OUTPUT_FORMAT.format(version_tag=version, description=description)
|
2016-04-22 00:50:11 +00:00
|
|
|
|
2023-01-27 14:36:09 +00:00
|
|
|
# Escape file content.
|
|
|
|
content.replace('"', '\\"')
|
2016-08-04 22:26:22 +00:00
|
|
|
|
2023-01-27 14:36:09 +00:00
|
|
|
if os.path.isfile(output_file_path):
|
|
|
|
with open(output_file_path, 'r') as f:
|
|
|
|
if content == f.read():
|
|
|
|
return
|
2016-08-04 22:26:22 +00:00
|
|
|
|
2023-01-27 14:36:09 +00:00
|
|
|
mkdir_p(os.path.dirname(output_file_path))
|
|
|
|
with open(output_file_path, 'w') as f:
|
|
|
|
f.write(content)
|
2016-02-22 21:07:19 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|