|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import re |
| 7 | +import sys |
| 8 | + |
| 9 | +def get_git_revision_hash(): |
| 10 | + try: |
| 11 | + ver = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).strip().decode('utf-8') |
| 12 | + except: |
| 13 | + ver = 'none' |
| 14 | + return ver |
| 15 | + |
| 16 | +def gen_version_git(): |
| 17 | + version_template = " character(LEN=32),parameter :: schism_version = '@{VERSION_SCHISM}', git_version = '@{VERSION_GIT}' " |
| 18 | + version_path = os.path.split( __file__)[0] |
| 19 | + query_path = os.path.join(version_path,"..") |
| 20 | + |
| 21 | + versionscratch_path = os.path.join(version_path,"_version") |
| 22 | + print(__file__) |
| 23 | + print(version_path) |
| 24 | + print(versionscratch_path) |
| 25 | + describe_re = re.compile(r"(?P<version>v?\d+\.\d+\.\d+)(?P<update>-\d+)?(?P<hash>-g[a-z0-9]+)?") |
| 26 | + |
| 27 | + try: |
| 28 | + with open(versionscratch_path, "w") as versionscratch: |
| 29 | + ok = subprocess.check_call(["git","describe","--always","--dirty"],stdout=versionscratch) |
| 30 | + with open(versionscratch_path,"r") as versionscratch: |
| 31 | + version_raw = versionscratch.readlines()[0].strip() |
| 32 | + |
| 33 | + |
| 34 | + is_dirty = "-dirty" in version_raw |
| 35 | + version_raw=version_raw.replace("-dirty","") |
| 36 | + m = describe_re.match(version_raw) |
| 37 | + if m is None: |
| 38 | + git_version = get_git_revision_hash() |
| 39 | + return git_version,None |
| 40 | + if m is None and len(version_raw) > 5: |
| 41 | + schism_version = "semantic version not determined" |
| 42 | + git_version = get_git_revision_hash() |
| 43 | + else: |
| 44 | + schism_version = m.group("version") |
| 45 | + print(m.group("update")) |
| 46 | + if m.lastindex == 1: |
| 47 | + # This is an untouched tag with no commits |
| 48 | + git_version = get_git_revision_hash() |
| 49 | + print(git_version) |
| 50 | + if m.lastindex >= 2: |
| 51 | + git_version = m.group("hash").replace("-g","") |
| 52 | + if is_dirty or len(m.group("update"))>2: |
| 53 | + nmod = m.group("update").replace("-","") |
| 54 | + schism_version = schism_version+"mod" |
| 55 | + git_version = git_version + " ({} commits since semantic tag, edits={})".format(nmod,is_dirty) |
| 56 | + if git_version is None: git_version = get_git_revision_hash() |
| 57 | + return git_version,schism_version |
| 58 | + |
| 59 | + except Exception as inst: |
| 60 | + print(inst) |
| 61 | + if os.path.exists(versionscratch_path): |
| 62 | + os.remove(versionscratch_path) |
| 63 | + print("Error querying \"git describe\", offline from Git utlities?\nYou can create a file called schism_version_user.txt" |
| 64 | + " in this directory with your own version label on the first line.\n") |
| 65 | + return None,None |
| 66 | + |
| 67 | +def gen_version_user(default_version = "develop"): |
| 68 | + schism_user_version_file = "schism_version_user.txt" |
| 69 | + print("Attempting to get version text manually from first line of \nsrc/Core/%s if file exists" % schism_user_version_file) |
| 70 | + user_version_path = os.path.join(os.path.split( __file__)[0],schism_user_version_file) |
| 71 | + if os.path.exists(user_version_path): |
| 72 | + with open(user_version_path,"r") as defaultfile: |
| 73 | + line = defaultfile.readline().strip() |
| 74 | + user_version = line if len(line) >=3 else default_version |
| 75 | + else: |
| 76 | + user_version = default_version |
| 77 | + assert len(user_version) > 3 |
| 78 | + return user_version |
| 79 | + |
| 80 | + |
| 81 | +def gen_version(versionfile_path=None): |
| 82 | + version_template = " character(LEN=32),parameter :: schism_version = '@{VERSION_SCHISM}', git_version = '@{VERSION_GIT}' " |
| 83 | + version_path = os.path.split( __file__)[0] |
| 84 | + template_path = os.path.join(version_path,"schism_version.F90.template") |
| 85 | + query_path = os.path.join(version_path,"..") |
| 86 | + if versionfile_path is None: |
| 87 | + scriptpath=os.path.dirname(os.path.realpath(__file__)) |
| 88 | + versionfile_path=os.path.join(scriptpath,"schism_version.F90") |
| 89 | + |
| 90 | + git_version,schism_version = gen_version_git() |
| 91 | + if schism_version is None: |
| 92 | + print("SCHISM version not available, searching for src/schism_user_version.txt or default") |
| 93 | + schism_version = gen_version_user() |
| 94 | + |
| 95 | + if git_version is None: |
| 96 | + print("Git hash not inferred from git describe, using rev-parse") |
| 97 | + git_version = get_git_revision_hash() |
| 98 | + if git_version is None: git_version = "unavailable" |
| 99 | + |
| 100 | + print(git_version) |
| 101 | + print(' SCHISM version: {0:s}'.format(schism_version)) |
| 102 | + print(' GIT commit {0:s}'.format(git_version)) |
| 103 | + with open(template_path,"r") as template: |
| 104 | + templatetxt = template.read() |
| 105 | + versiontxt = templatetxt.replace("@{VERSION_GIT}", git_version).replace("@{VERSION_SCHISM}",schism_version) |
| 106 | + with open(versionfile_path,"w") as versionfile: |
| 107 | + versionfile.write(versiontxt) |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +if __name__=="__main__": |
| 112 | + if len(sys.argv) > 1: |
| 113 | + outputfile = sys.argv[1] |
| 114 | + else: |
| 115 | + outputfile = None |
| 116 | + gen_version(outputfile) |
0 commit comments