Damien George | 26b512e | 2015-05-30 23:11:16 +0100 | [diff] [blame] | 1 | """ |
| 2 | Generate header file with macros defining MicroPython version info. |
| 3 | |
| 4 | This script works with Python 2.6, 2.7, 3.3 and 3.4. |
| 5 | """ |
Damien George | 95f5346 | 2015-04-22 17:38:05 +0100 | [diff] [blame] | 6 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import sys |
| 10 | import os |
| 11 | import datetime |
| 12 | import subprocess |
| 13 | |
Damien George | 1a97f67 | 2015-05-25 13:26:47 +0100 | [diff] [blame] | 14 | def get_version_info_from_git(): |
Damien George | 26b512e | 2015-05-30 23:11:16 +0100 | [diff] [blame] | 15 | # Python 2.6 doesn't have check_output, so check for that |
| 16 | try: |
| 17 | subprocess.check_output |
| 18 | subprocess.check_call |
| 19 | except AttributeError: |
| 20 | return None |
| 21 | |
Damien George | 95f5346 | 2015-04-22 17:38:05 +0100 | [diff] [blame] | 22 | # Note: git describe doesn't work if no tag is available |
| 23 | try: |
| 24 | git_tag = subprocess.check_output(["git", "describe", "--dirty", "--always"], universal_newlines=True).strip() |
| 25 | except subprocess.CalledProcessError: |
| 26 | git_tag = "" |
Damien George | 1a97f67 | 2015-05-25 13:26:47 +0100 | [diff] [blame] | 27 | except OSError: |
| 28 | return None |
Damien George | 95f5346 | 2015-04-22 17:38:05 +0100 | [diff] [blame] | 29 | try: |
| 30 | git_hash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], stderr=subprocess.STDOUT, universal_newlines=True).strip() |
| 31 | except subprocess.CalledProcessError: |
| 32 | git_hash = "unknown" |
Damien George | 1a97f67 | 2015-05-25 13:26:47 +0100 | [diff] [blame] | 33 | except OSError: |
| 34 | return None |
Damien George | 95f5346 | 2015-04-22 17:38:05 +0100 | [diff] [blame] | 35 | |
| 36 | try: |
| 37 | # Check if there are any modified files. |
| 38 | subprocess.check_call(["git", "diff", "--no-ext-diff", "--quiet", "--exit-code"], stderr=subprocess.STDOUT) |
| 39 | # Check if there are any staged files. |
| 40 | subprocess.check_call(["git", "diff-index", "--cached", "--quiet", "HEAD", "--"], stderr=subprocess.STDOUT) |
| 41 | except subprocess.CalledProcessError: |
| 42 | git_hash += "-dirty" |
Damien George | 1a97f67 | 2015-05-25 13:26:47 +0100 | [diff] [blame] | 43 | except OSError: |
| 44 | return None |
Damien George | 95f5346 | 2015-04-22 17:38:05 +0100 | [diff] [blame] | 45 | |
| 46 | # Try to extract MicroPython version from git tag |
| 47 | if git_tag.startswith("v"): |
| 48 | ver = git_tag[1:].split("-")[0].split(".") |
| 49 | if len(ver) == 2: |
| 50 | ver.append("0") |
| 51 | else: |
| 52 | ver = ["0", "0", "1"] |
| 53 | |
Damien George | 1a97f67 | 2015-05-25 13:26:47 +0100 | [diff] [blame] | 54 | return git_tag, git_hash, ver |
| 55 | |
| 56 | def get_version_info_from_docs_conf(): |
| 57 | with open("%s/docs/conf.py" % sys.argv[0].rsplit("/", 2)[0]) as f: |
| 58 | for line in f: |
| 59 | if line.startswith("release = '"): |
| 60 | ver = line.strip()[10:].strip("'") |
| 61 | git_tag = "v" + ver |
| 62 | ver = ver.split(".") |
| 63 | if len(ver) == 2: |
| 64 | ver.append("0") |
| 65 | return git_tag, "<no hash>", ver |
| 66 | return None |
| 67 | |
| 68 | def make_version_header(filename): |
| 69 | # Get version info using git, with fallback to docs/conf.py |
| 70 | info = get_version_info_from_git() |
| 71 | if info is None: |
| 72 | info = get_version_info_from_docs_conf() |
| 73 | |
| 74 | git_tag, git_hash, ver = info |
| 75 | |
Damien George | 95f5346 | 2015-04-22 17:38:05 +0100 | [diff] [blame] | 76 | # Generate the file with the git and version info |
| 77 | file_data = """\ |
| 78 | // This file was generated by py/makeversionhdr.py |
| 79 | #define MICROPY_GIT_TAG "%s" |
| 80 | #define MICROPY_GIT_HASH "%s" |
| 81 | #define MICROPY_BUILD_DATE "%s" |
| 82 | #define MICROPY_VERSION_MAJOR (%s) |
| 83 | #define MICROPY_VERSION_MINOR (%s) |
| 84 | #define MICROPY_VERSION_MICRO (%s) |
| 85 | #define MICROPY_VERSION_STRING "%s.%s.%s" |
| 86 | """ % (git_tag, git_hash, datetime.date.today().strftime("%Y-%m-%d"), |
| 87 | ver[0], ver[1], ver[2], ver[0], ver[1], ver[2]) |
| 88 | |
| 89 | # Check if the file contents changed from last time |
| 90 | write_file = True |
| 91 | if os.path.isfile(filename): |
| 92 | with open(filename, 'r') as f: |
| 93 | existing_data = f.read() |
| 94 | if existing_data == file_data: |
| 95 | write_file = False |
| 96 | |
| 97 | # Only write the file if we need to |
| 98 | if write_file: |
| 99 | print("Generating %s" % filename) |
| 100 | with open(filename, 'w') as f: |
| 101 | f.write(file_data) |
| 102 | |
Damien George | 26b512e | 2015-05-30 23:11:16 +0100 | [diff] [blame] | 103 | if __name__ == "__main__": |
| 104 | make_version_header(sys.argv[1]) |