|
| 1 | +# This script works with Python 2 and 3 |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +import sys |
| 6 | +import os |
| 7 | +import datetime |
| 8 | +import subprocess |
| 9 | + |
| 10 | +def make_version_header(filename): |
| 11 | + # Note: git describe doesn't work if no tag is available |
| 12 | + try: |
| 13 | + git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], universal_newlines=True).strip() |
| 14 | + except subprocess.CalledProcessError: |
| 15 | + git_tag = "" |
| 16 | + try: |
| 17 | + git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip() |
| 18 | + except subprocess.CalledProcessError: |
| 19 | + git_hash = "unknown" |
| 20 | + |
| 21 | + try: |
| 22 | + # Check if there are any modified files. |
| 23 | + subprocess.check_call(["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT) |
| 24 | + # Check if there are any staged files. |
| 25 | + subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT) |
| 26 | + except subprocess.CalledProcessError: |
| 27 | + git_hash += "-dirty" |
| 28 | + |
| 29 | + # Try to extract MicroPython version from git tag |
| 30 | + if git_tag.startswith("v"): |
| 31 | + ver = git_tag[1:].split("-")[0].split(".") |
| 32 | + if len(ver) == 2: |
| 33 | + ver.append("0") |
| 34 | + else: |
| 35 | + ver = ["0", "0", "1"] |
| 36 | + |
| 37 | + # Generate the file with the git and version info |
| 38 | + file_data = """\ |
| 39 | +// This file was generated by py/makeversionhdr.py |
| 40 | +#define MICROPY_GIT_TAG "%s" |
| 41 | +#define MICROPY_GIT_HASH "%s" |
| 42 | +#define MICROPY_BUILD_DATE "%s" |
| 43 | +#define MICROPY_VERSION_MAJOR (%s) |
| 44 | +#define MICROPY_VERSION_MINOR (%s) |
| 45 | +#define MICROPY_VERSION_MICRO (%s) |
| 46 | +#define MICROPY_VERSION_STRING "%s.%s.%s" |
| 47 | +""" % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"), |
| 48 | + ver[0], ver[1], ver[2], ver[0], ver[1], ver[2]) |
| 49 | + |
| 50 | + # Check if the file contents changed from last time |
| 51 | + write_file = True |
| 52 | + if os.path.isfile(filename): |
| 53 | + with open(filename, 'r') as f: |
| 54 | + existing_data = f.read() |
| 55 | + if existing_data == file_data: |
| 56 | + write_file = False |
| 57 | + |
| 58 | + # Only write the file if we need to |
| 59 | + if write_file: |
| 60 | + print("Generating %s" % filename) |
| 61 | + with open(filename, 'w') as f: |
| 62 | + f.write(file_data) |
| 63 | + |
| 64 | +make_version_header(sys.argv[1]) |
0 commit comments