gen_version.py 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import argparse
import os
import subprocess

def get_git_commit(src_dir):
    try:
        return subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=src_dir).decode('ascii').strip()
    except Exception:
        return 'unknown'

def get_mge_version(version_txt_path):
    v = {}
    with open(version_txt_path) as fp:
        exec(fp.read(), v)
    return v

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="generate version.py to build path")
    parser.add_argument("--output", type=str, required=True)
20 21 22
    parser.add_argument("--major", type=int, required=True)
    parser.add_argument("--minor", type=int, required=True)
    parser.add_argument("--patch", type=int, required=True)
M
Megvii Engine Team 已提交
23
    parser.add_argument("--rc", type=str, required=False)
24
    parser.add_argument("--internal", action='store_true')
25 26 27
    args = parser.parse_args()
    python_dir = os.path.dirname(__file__)
    commit_id = get_git_commit(python_dir)
28
    mge_ver = str(args.major) + "." + str(args.minor) + "." + str(args.patch)
M
Megvii Engine Team 已提交
29 30
    if args.rc is not None:
        mge_ver += args.rc
31 32 33
    with open(args.output, 'w') as f:
        f.write("__version__ = '{}'\n".format(mge_ver))
        f.write("git_version = {}\n".format(repr(commit_id)))
34
        if args.internal:
35
            f.write("__internal__ = True\n")