|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Created by FozzTexx |
| 4 | + |
| 5 | +import argparse |
| 6 | +import subprocess |
| 7 | +import os, sys |
| 8 | +import re |
| 9 | +import shlex |
| 10 | +from datetime import datetime |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +VERSION_H = "include/version.h" |
| 14 | +PLATFORMS = "build-platforms" |
| 15 | +MACRO_PATTERN = r"^\s*#define\s+(.*?)\s+(.*?)\s*(?://.*|/\*[\s\S]*?\*/)?$" |
| 16 | + |
| 17 | +def build_argparser(): |
| 18 | + parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 19 | + parser.add_argument("--update_version_h", action="store_true", |
| 20 | + help="update include/version.h with current commit information") |
| 21 | + return parser |
| 22 | + |
| 23 | +def run_command(cmd): |
| 24 | + return subprocess.check_output(cmd, universal_newlines=True).strip() |
| 25 | + |
| 26 | +def get_repo_base(): |
| 27 | + return run_command(["git", "rev-parse", "--show-toplevel"]) |
| 28 | + |
| 29 | +def is_fujinet_repo(base): |
| 30 | + if os.path.exists(os.path.join(base, VERSION_H)) \ |
| 31 | + and os.path.isdir(os.path.join(base, PLATFORMS)): |
| 32 | + return True |
| 33 | + return False |
| 34 | + |
| 35 | +def get_commit_version(): |
| 36 | + return run_command(["git", "describe", "--always", "HEAD"]) |
| 37 | + |
| 38 | +def get_modified_files(): |
| 39 | + files = run_command(["git", "diff", "--name-only"]).split("\n") |
| 40 | + files = [f for f in files if f] |
| 41 | + return files |
| 42 | + |
| 43 | +def load_version_macros(path): |
| 44 | + txt = [line for line in open(path)] |
| 45 | + macros = {} |
| 46 | + for line in txt: |
| 47 | + m = re.match(MACRO_PATTERN, line) |
| 48 | + if m: |
| 49 | + name = m.group(1) |
| 50 | + if '(' in name: |
| 51 | + continue |
| 52 | + value = m.group(2) |
| 53 | + if not value: |
| 54 | + value = None |
| 55 | + macros[name] = value |
| 56 | + return macros |
| 57 | + |
| 58 | +def main(): |
| 59 | + base = get_repo_base() |
| 60 | + if not is_fujinet_repo(base): |
| 61 | + print("Not in FujiNet firmware repo") |
| 62 | + return 0 |
| 63 | + |
| 64 | + args = build_argparser().parse_args() |
| 65 | + # FIXME - don't allow update_version_h unless on the actual tag? |
| 66 | + |
| 67 | + version_h_path = os.path.join(base, VERSION_H) |
| 68 | + version = get_commit_version() |
| 69 | + modified = get_modified_files() |
| 70 | + commit_id_long = run_command(["git", "rev-parse", "HEAD"]) |
| 71 | + |
| 72 | + mtime = int(run_command(["git", "show", "--no-patch", "--format=%ct", "HEAD"])) |
| 73 | + for path in modified: |
| 74 | + if os.path.exists(path): |
| 75 | + mt = os.path.getmtime(path) |
| 76 | + if not mtime or mt > mtime: |
| 77 | + mtime = mt |
| 78 | + |
| 79 | + macros = { |
| 80 | + 'FN_VERSION_BUILD': commit_id_long, |
| 81 | + } |
| 82 | + cur_macros = load_version_macros(version_h_path) |
| 83 | + ver_major = int(cur_macros['FN_VERSION_MAJOR']) |
| 84 | + ver_minor = int(cur_macros['FN_VERSION_MINOR']) |
| 85 | + |
| 86 | + m = re.match(r"^v([0-9]+)[.]([0-9]+)[.]([0-9]+)-([0-9]+)-g(.*)", version) |
| 87 | + if m: |
| 88 | + ver_major = macros['FN_VERSION_MAJOR'] = int(m.group(1)) |
| 89 | + ver_minor = macros['FN_VERSION_MINOR'] = int(m.group(2)) |
| 90 | + version = f"v{m.group(1)}.{m.group(2)}-{m.group(5)}" |
| 91 | + else: |
| 92 | + m = re.match(r"^([a-z0-9]{8})$", version) |
| 93 | + if m: |
| 94 | + version = f"v{ver_major}.{ver_minor}-{version}" |
| 95 | + |
| 96 | + if modified: |
| 97 | + version += "*" |
| 98 | + macros['FN_VERSION_FULL'] = version |
| 99 | + macros['FN_VERSION_DATE'] = datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M:%S") |
| 100 | + |
| 101 | + new_macros = cur_macros.copy() |
| 102 | + new_macros.update(macros) |
| 103 | + |
| 104 | + if new_macros != cur_macros: |
| 105 | + for key in macros: |
| 106 | + if isinstance(macros[key], str): |
| 107 | + macros[key] = f'"{macros[key]}"' |
| 108 | + |
| 109 | + cur_macros.update(macros) |
| 110 | + macro_defs = [] |
| 111 | + for key in cur_macros: |
| 112 | + value = cur_macros[key] |
| 113 | + mdef = f"-D{key}" |
| 114 | + if value is not None: |
| 115 | + mdef += f"={value}" |
| 116 | + macro_defs.append(shlex.quote(mdef)) |
| 117 | + |
| 118 | + # FIXME - if args.update_version_h then update, don't print |
| 119 | + macro_defs = " ".join(macro_defs) |
| 120 | + print(macro_defs) |
| 121 | + #Path(version_h_path).touch() |
| 122 | + |
| 123 | + return |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + exit(main() or 0) |
| 127 | +elif __name__ == "SCons.Script": |
| 128 | + print("Running as build script") |
0 commit comments