2015-05-30 18:11:16 -04:00
|
|
|
"""
|
|
|
|
Generate header file with macros defining MicroPython version info.
|
|
|
|
|
|
|
|
This script works with Python 2.6, 2.7, 3.3 and 3.4.
|
|
|
|
"""
|
2015-04-22 12:38:05 -04:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
import subprocess
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2015-05-25 08:26:47 -04:00
|
|
|
def get_version_info_from_git():
|
2015-05-30 18:11:16 -04:00
|
|
|
# Python 2.6 doesn't have check_output, so check for that
|
|
|
|
try:
|
|
|
|
subprocess.check_output
|
|
|
|
subprocess.check_call
|
|
|
|
except AttributeError:
|
|
|
|
return None
|
|
|
|
|
2015-04-22 12:38:05 -04:00
|
|
|
# Note: git describe doesn't work if no tag is available
|
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
git_tag = subprocess.check_output(
|
|
|
|
["git", "describe", "--dirty", "--always", "--tags"],
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
universal_newlines=True,
|
|
|
|
).strip()
|
2015-08-11 07:27:38 -04:00
|
|
|
except subprocess.CalledProcessError as er:
|
2015-08-12 18:28:16 -04:00
|
|
|
if er.returncode == 128:
|
2015-08-11 07:27:38 -04:00
|
|
|
# git exit code of 128 means no repository found
|
|
|
|
return None
|
2015-04-22 12:38:05 -04:00
|
|
|
git_tag = ""
|
2015-05-25 08:26:47 -04:00
|
|
|
except OSError:
|
|
|
|
return None
|
2015-04-22 12:38:05 -04:00
|
|
|
try:
|
2021-03-15 09:57:36 -04:00
|
|
|
git_hash = subprocess.check_output(
|
|
|
|
["git", "rev-parse", "--short", "HEAD"],
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
universal_newlines=True,
|
|
|
|
).strip()
|
2015-04-22 12:38:05 -04:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
git_hash = "unknown"
|
2015-05-25 08:26:47 -04:00
|
|
|
except OSError:
|
|
|
|
return None
|
2015-04-22 12:38:05 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
# Check if there are any modified files.
|
2021-03-15 09:57:36 -04:00
|
|
|
subprocess.check_call(
|
|
|
|
["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT
|
|
|
|
)
|
2015-04-22 12:38:05 -04:00
|
|
|
# Check if there are any staged files.
|
2021-03-15 09:57:36 -04:00
|
|
|
subprocess.check_call(
|
|
|
|
["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT
|
|
|
|
)
|
2015-04-22 12:38:05 -04:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
git_hash += "-dirty"
|
2015-05-25 08:26:47 -04:00
|
|
|
except OSError:
|
|
|
|
return None
|
2015-04-22 12:38:05 -04:00
|
|
|
|
|
|
|
# Try to extract MicroPython version from git tag
|
2017-02-13 10:23:46 -05:00
|
|
|
ver = git_tag.split("-")[0].split(".")
|
2015-04-22 12:38:05 -04:00
|
|
|
|
2015-05-25 08:26:47 -04:00
|
|
|
return git_tag, git_hash, ver
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2021-04-22 20:55:39 -04:00
|
|
|
|
2015-05-25 08:26:47 -04:00
|
|
|
def get_version_info_from_docs_conf():
|
2018-05-02 08:22:35 -04:00
|
|
|
with open(os.path.join(os.path.dirname(sys.argv[0]), "..", "conf.py")) as f:
|
2015-05-25 08:26:47 -04:00
|
|
|
for line in f:
|
2017-07-04 08:37:41 -04:00
|
|
|
if line.startswith("version = release = '"):
|
|
|
|
ver = line.strip().split(" = ")[2].strip("'")
|
2015-05-25 08:26:47 -04:00
|
|
|
git_tag = "v" + ver
|
|
|
|
ver = ver.split(".")
|
|
|
|
if len(ver) == 2:
|
|
|
|
ver.append("0")
|
|
|
|
return git_tag, "<no hash>", ver
|
|
|
|
return None
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2015-05-25 08:26:47 -04:00
|
|
|
def make_version_header(filename):
|
|
|
|
# Get version info using git, with fallback to docs/conf.py
|
|
|
|
info = get_version_info_from_git()
|
|
|
|
if info is None:
|
|
|
|
info = get_version_info_from_docs_conf()
|
|
|
|
|
|
|
|
git_tag, git_hash, ver = info
|
2017-05-01 16:40:48 -04:00
|
|
|
if len(ver) < 3:
|
|
|
|
ver = ("0", "0", "0")
|
|
|
|
version_string = git_hash
|
|
|
|
else:
|
|
|
|
version_string = ".".join(ver)
|
2015-05-25 08:26:47 -04:00
|
|
|
|
2015-04-22 12:38:05 -04:00
|
|
|
# Generate the file with the git and version info
|
|
|
|
file_data = """\
|
|
|
|
// This file was generated by py/makeversionhdr.py
|
|
|
|
#define MICROPY_GIT_TAG "%s"
|
|
|
|
#define MICROPY_GIT_HASH "%s"
|
|
|
|
#define MICROPY_BUILD_DATE "%s"
|
|
|
|
#define MICROPY_VERSION_MAJOR (%s)
|
|
|
|
#define MICROPY_VERSION_MINOR (%s)
|
|
|
|
#define MICROPY_VERSION_MICRO (%s)
|
2017-05-01 16:40:48 -04:00
|
|
|
#define MICROPY_VERSION_STRING "%s"
|
2018-01-02 21:25:41 -05:00
|
|
|
#define MICROPY_FULL_VERSION_INFO ("Adafruit CircuitPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME)
|
2021-03-15 09:57:36 -04:00
|
|
|
""" % (
|
|
|
|
git_tag,
|
|
|
|
git_hash,
|
|
|
|
datetime.date.today().strftime("%Y-%m-%d"),
|
|
|
|
ver[0].replace("v", ""),
|
|
|
|
ver[1],
|
|
|
|
ver[2],
|
|
|
|
version_string,
|
|
|
|
)
|
2015-04-22 12:38:05 -04:00
|
|
|
|
|
|
|
# Check if the file contents changed from last time
|
|
|
|
write_file = True
|
|
|
|
if os.path.isfile(filename):
|
2021-03-15 09:57:36 -04:00
|
|
|
with open(filename, "r") as f:
|
2015-04-22 12:38:05 -04:00
|
|
|
existing_data = f.read()
|
|
|
|
if existing_data == file_data:
|
|
|
|
write_file = False
|
|
|
|
|
|
|
|
# Only write the file if we need to
|
|
|
|
if write_file:
|
2021-03-15 09:57:36 -04:00
|
|
|
with open(filename, "w") as f:
|
2015-04-22 12:38:05 -04:00
|
|
|
f.write(file_data)
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2015-05-30 18:11:16 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
make_version_header(sys.argv[1])
|