version.py 1.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import subprocess as _S
from os.path import dirname as _d
_d = _d(__file__)

def _git_call(*args):
    return _S.call(("git",) + args, cwd=_d)

def _git_output(*args):
    p = _S.Popen(("git",) + args, cwd=_d, stdout=_S.PIPE, stderr=_S.PIPE)
    out, err = p.communicate()
    if p.returncode:
        raise _S.CalledProcessError(p.returncode, "git", err)
    return out.strip()

_git_call("update-index", "-q", "--refresh")
dirty = _git_call("diff-index", "--quiet", "HEAD", "--")
if dirty not in (0, 1):
    raise _S.CalledProcessError(dirty, "git")

20 21 22 23
try:
  revision = int(_git_output("rev-list", "--count", "HEAD"))
except _S.CalledProcessError: # BBB: Git too old
  revision = len(_git_output("rev-list", "HEAD").split())
24 25 26 27 28 29
short = _git_output("rev-parse", "--short", "HEAD")
version = "0-%s.g%s" % (revision, short)

if dirty:
    version += ".dirty"

30 31 32 33 34
# Because the software could be forked or have local changes/commits, above
# properties can't be used to decide whether a peer runs an appropriate version:
# they are intended to the network admin.
# Only 'protocol' is important and it must be increased whenever they would be
# a wish to force an update of nodes.
35
protocol = 7
36 37
min_protocol = 1

38 39
if __name__ == "__main__":
    print version