(version_git_dir)
| 60 | |
| 61 | |
| 62 | def generate_version_file(version_git_dir): |
| 63 | |
| 64 | # Get the git description. |
| 65 | print("#> Getting git description") |
| 66 | try: |
| 67 | git_description = check_output("git describe HEAD --always --tags --long", shell=True) |
| 68 | git_description = git_description.decode().strip() |
| 69 | git_description = re.sub(r'-0-g([0-9a-f]+)$', r'-g\1', git_description) |
| 70 | except: |
| 71 | print("#> ERROR: Failed to get git description, do you have git.exe in your PATH environment variable?") |
| 72 | return 1 |
| 73 | |
| 74 | output_file = os.path.join(os.path.normpath(version_git_dir),'version_git.h') |
| 75 | print("#> Pre-build script start") |
| 76 | |
| 77 | # Get the git SHA. |
| 78 | print("#> Getting git SHA") |
| 79 | try: |
| 80 | git_sha = check_output("git rev-parse --verify HEAD", shell=True) |
| 81 | git_sha = git_sha.decode().strip() |
| 82 | except: |
| 83 | print("#> ERROR: Failed to get git SHA, do you have git.exe in your PATH environment variable?") |
| 84 | return 1 |
| 85 | |
| 86 | # Check are there any local, uncommitted modifications. |
| 87 | print("#> Checking for local changes") |
| 88 | try: |
| 89 | check_output("git diff --no-ext-diff --quiet --exit-code", shell=True) |
| 90 | except (CalledProcessError, OSError): |
| 91 | git_has_changes = 1 |
| 92 | else: |
| 93 | git_has_changes = 0 |
| 94 | |
| 95 | # Create the version file. Only overwrite an existing file if it changes. |
| 96 | version_text = VERSION_GIT_FILE_TEMPLATE % (git_description, git_sha, git_has_changes) |
| 97 | try: |
| 98 | with open(output_file, 'r') as version_file: |
| 99 | current_version_text = version_file.read() |
| 100 | except IOError: |
| 101 | current_version_text = '' |
| 102 | if version_text != current_version_text: |
| 103 | print("#> Writing git version file") |
| 104 | with open(output_file, 'w+') as version_file: |
| 105 | version_file.write(version_text) |
| 106 | else: |
| 107 | print("#> Keeping git version file since it didn't need to change") |
| 108 | |
| 109 | print("#> Pre-build script completed written %s" % output_file ) |
| 110 | |
| 111 | return 0 |
| 112 | |
| 113 | if __name__ == "__main__": |
| 114 | parser = argparse.ArgumentParser(description='git version generator') |
no test coverage detected