|
| 1 | +""" |
| 2 | +Generate header file with macros defining micro:bit version info. |
| 3 | +
|
| 4 | +Adapted from MicroPython's py/makeversionhdr.py file at v1.21.0. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import sys |
| 9 | +import os |
| 10 | +import datetime |
| 11 | +import subprocess |
| 12 | + |
| 13 | + |
| 14 | +def get_version_info_from_git(repo_path): |
| 15 | + # Note: git describe doesn't work if no tag is available |
| 16 | + try: |
| 17 | + git_tag = subprocess.check_output( |
| 18 | + ["git", "describe", "--tags", "--dirty", "--always", "--match", "v[1-9].*"], |
| 19 | + cwd=repo_path, |
| 20 | + stderr=subprocess.STDOUT, |
| 21 | + universal_newlines=True, |
| 22 | + ).strip() |
| 23 | + except subprocess.CalledProcessError as er: |
| 24 | + if er.returncode == 128: |
| 25 | + # git exit code of 128 means no repository found |
| 26 | + return None |
| 27 | + git_tag = "" |
| 28 | + except OSError: |
| 29 | + return None |
| 30 | + try: |
| 31 | + git_hash = subprocess.check_output( |
| 32 | + ["git", "rev-parse", "--short", "HEAD"], |
| 33 | + cwd=repo_path, |
| 34 | + stderr=subprocess.STDOUT, |
| 35 | + universal_newlines=True, |
| 36 | + ).strip() |
| 37 | + except subprocess.CalledProcessError: |
| 38 | + git_hash = "unknown" |
| 39 | + except OSError: |
| 40 | + return None |
| 41 | + |
| 42 | + try: |
| 43 | + # Check if there are any modified files. |
| 44 | + subprocess.check_call( |
| 45 | + ["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], |
| 46 | + cwd=repo_path, |
| 47 | + stderr=subprocess.STDOUT, |
| 48 | + ) |
| 49 | + # Check if there are any staged files. |
| 50 | + subprocess.check_call( |
| 51 | + ["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], |
| 52 | + cwd=repo_path, |
| 53 | + stderr=subprocess.STDOUT, |
| 54 | + ) |
| 55 | + except subprocess.CalledProcessError: |
| 56 | + git_hash += "-dirty" |
| 57 | + except OSError: |
| 58 | + return None |
| 59 | + |
| 60 | + return git_tag, git_hash |
| 61 | + |
| 62 | + |
| 63 | +def make_version_header(repo_path, filename): |
| 64 | + info = None |
| 65 | + if "MICROBIT_GIT_TAG" in os.environ: |
| 66 | + info = [os.environ["MICROBIT_GIT_TAG"], os.environ["MICROBIT_GIT_HASH"]] |
| 67 | + if info is None: |
| 68 | + info = get_version_info_from_git(repo_path) |
| 69 | + |
| 70 | + git_tag, git_hash = info |
| 71 | + |
| 72 | + build_date = datetime.date.today() |
| 73 | + if "SOURCE_DATE_EPOCH" in os.environ: |
| 74 | + build_date = datetime.datetime.utcfromtimestamp( |
| 75 | + int(os.environ["SOURCE_DATE_EPOCH"]) |
| 76 | + ).date() |
| 77 | + |
| 78 | + # Generate the file with the git and version info |
| 79 | + file_data = """\ |
| 80 | +// This file was generated by py/makeversionhdr.py |
| 81 | +#define MICROBIT_GIT_TAG "%s" |
| 82 | +#define MICROBIT_GIT_HASH "%s" |
| 83 | +#define MICROBIT_BUILD_DATE "%s" |
| 84 | +""" % ( |
| 85 | + git_tag, |
| 86 | + git_hash, |
| 87 | + build_date.strftime("%Y-%m-%d"), |
| 88 | + ) |
| 89 | + |
| 90 | + # Check if the file contents changed from last time |
| 91 | + write_file = True |
| 92 | + if os.path.isfile(filename): |
| 93 | + with open(filename, "r") as f: |
| 94 | + existing_data = f.read() |
| 95 | + if existing_data == file_data: |
| 96 | + write_file = False |
| 97 | + |
| 98 | + # Only write the file if we need to |
| 99 | + if write_file: |
| 100 | + print("GEN %s" % filename) |
| 101 | + with open(filename, "w") as f: |
| 102 | + f.write(file_data) |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + parser = argparse.ArgumentParser() |
| 107 | + parser.add_argument( |
| 108 | + "-r", |
| 109 | + "--repo-path", |
| 110 | + default=os.path.join(os.path.dirname(sys.argv[0]), ".."), |
| 111 | + help="path to git repo to query for version", |
| 112 | + ) |
| 113 | + parser.add_argument("dest", nargs=1, help="output file path") |
| 114 | + args = parser.parse_args() |
| 115 | + |
| 116 | + make_version_header(args.repo_path, args.dest[0]) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments