Skip to content

Instantly share code, notes, and snippets.

@mundry
Created October 22, 2016 12:43
Show Gist options
  • Save mundry/a7bb11c685c146b9a458b34174d163f7 to your computer and use it in GitHub Desktop.
Save mundry/a7bb11c685c146b9a458b34174d163f7 to your computer and use it in GitHub Desktop.
Script to append a git revision hash to the nginx server version.
#!/usr/bin/env python2.7
#-*- encoding: utf-8 -*-
# Usage:
# ./nginx_version.py /path/to/nginx/src/core/nginx.h <git hash>
# or:
# /path/to/nginx-codebase-root$ /path/to/nginx_version.py <git hash>
from os.path import abspath
from re import compile
from sys import argv
NGINX_VERSION_RE = compile(r'^(#define\s+NGINX_VERSION\s+)"(\d\.\d{1,2}\.\d{1,2})(?:-g([a-fA-F0-9]{,20}))?"$')
def set_version(nginx_h, git_hash):
output = []
with open(nginx_h) as fin:
for line in fin:
m = NGINX_VERSION_RE.match(line.strip())
if m is None:
output.append(line)
else:
if m.group(3) == git_hash:
return
output.append('{}"{}-g{}"\n'.format(m.group(1), m.group(2), git_hash))
with open(nginx_h, 'w') as out:
out.write("".join(output))
def print_usage(script_path):
print "Usage:"
print "{} /path/to/nginx/src/core/nginx.h <git hash>".format(script_path)
print "or:"
print "/path/to/nginx-codebase-root$ {} <git hash>".format(script_path)
if __name__ == "__main__":
argc = len(argv)
if argc < 2:
print_usage(abspath(argv[0]))
exit(1)
nginx_h = abspath("src/core/nginx.h" if argc == 2 else argv[1])
# Ensure the git hash is always read from the expected position,
# instead of relying on that the command wasn't invoked with
# extranous arguments by simply reading the last argument.
git_hash = argv[min(argc - 1, 2)]
set_version(nginx_h, git_hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment