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