Created
November 17, 2019 17:38
-
-
Save mofosyne/a0e96035e230d86dd7f94070890d027c to your computer and use it in GitHub Desktop.
This is a sketch of a python3 script that checks git tag written in a modified semantic version format and returns a C file with the parsed version code and the git hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# This script checks git tag written in a modified semantic version format | |
# and returns a C file with the parsed version code and the git hash | |
# The git tag MUST conform to this format v<major>.<minor>.<patch>-<rcX> | |
import subprocess | |
import sys, os | |
import re | |
tagname = subprocess.check_output(["git", "describe", "--tags"]).strip().decode("utf-8") ; | |
tagnamelong = subprocess.check_output(["git", "describe", "--tags", "--long"]).strip().decode("utf-8") ; | |
shorthash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).strip().decode("utf-8") ; | |
################################################################################ | |
# Constants | |
MAGIC_NUMBER = "0x43505300"; | |
HEADER_VERSION = 0; | |
HEADER_LENGTH = 1024; | |
FW_LENGTH = 896*1024; | |
VENDOR_NAME = "VENDOR_NAME"; | |
FW_NAME = "APP"; | |
################################################################################ | |
# Based on https://github.com/python-semver/python-semver/blob/master/semver.py | |
# format: v<major>.<minor>.<patch>-<rc.X> e.g. v0.0.1-rc13 or v0.0.1-rc.13 | |
_REGEX = re.compile( | |
r""" | |
^ | |
v | |
(?P<major>(?:0|[1-9][0-9]*)) | |
\. | |
(?P<minor>(?:0|[1-9][0-9]*)) | |
\. | |
(?P<patch>(?:0|[1-9][0-9]*)) | |
(\-(?P<prerelease> | |
(?:0|[1-9A-Za-z-][0-9A-Za-z-]*) | |
(\.(?:0|[1-9A-Za-z-][0-9A-Za-z-]*))* | |
))? | |
(\+(?P<build> | |
[0-9A-Za-z-]+ | |
(\.[0-9A-Za-z-]+)* | |
))? | |
$ | |
""", re.VERBOSE) | |
match = _REGEX.match(tagname); | |
if match is None: | |
print(f"git tag is not valid semantic version: {tagname}"); | |
sys.exit(os.EX_USAGE); # Failed | |
# Version | |
version_parts = match.groupdict(); | |
VERSION_MAJOR = version_parts["major"]; # Required | |
VERSION_MINOR = version_parts["minor"]; # Required | |
VERSION_PATCH = version_parts["patch"]; # Required | |
VERSION_PRERELEASE = 0; # Optional | |
if version_parts["prerelease"] != None: | |
release_candidate = re.search('rc\.?([0-9]+)', version_parts["prerelease"], re.IGNORECASE) | |
if release_candidate: | |
VERSION_PRERELEASE = release_candidate.group(1) | |
VERSION_BUILD = shorthash; | |
################################################################################ | |
if VERSION_PRERELEASE == 0: | |
SEM_VER_PRINT = f"{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}+{VERSION_BUILD}"; | |
else: | |
SEM_VER_PRINT = f"{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}-rc{VERSION_PRERELEASE}+{VERSION_BUILD}"; | |
c_source = f"""\ | |
/* | |
Autogenerated Firmware Header | |
`git describe --tags --long` --> {tagnamelong} | |
`git rev-parse --short HEAD` --> {shorthash} | |
Version (https://semver.org/ Semantic Versioning 2.0.0): | |
{SEM_VER_PRINT} | |
*/ | |
fw_header_t __attribute__((section(".firmware_header"))) | |
running_firmware_header = {{ | |
.magic = {MAGIC_NUMBER} | |
.hdrver = {HEADER_VERSION}, | |
.hdrlen = {HEADER_LENGTH}, | |
.applen = {FW_LENGTH}, | |
.name = "{FW_NAME}", | |
.version_major = {VERSION_MAJOR}, | |
.version_minor = {VERSION_MINOR}, | |
.version_patch = {VERSION_PATCH}, | |
.version_prerelease = {VERSION_PRERELEASE}, | |
.version_build = "{VERSION_BUILD}" | |
}}; | |
""" | |
print(c_source); | |
sys.exit(os.EX_OK); # Completed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: