aboutsummaryrefslogtreecommitdiff
path: root/buildtools/make-version.py
blob: af513b5dd8cfba7563065d0567c4a81c28b1ab46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# $Id$

# Copyright (C) 2013  Internet Systems Consortium ("ISC")
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

# Kludge to extract Subversion revision number from environment at
# build time, if possible, using it to construct the software version
# number that one of our users so desperately wants us to have.  This
# is a bit tricky because we want this to be automatic (hence use of
# Subversion revision number, which isn't really intended for this
# purpose), want it to work whether installing from binary package or
# subversion checkout or frozen tarball, and need to be careful both
# to update it whenever the revision changes and not to stomp on it
# when the subversion revision number isn't available.
#
# I did say this was a kludge.

# One could argue that we should be throwing a fatal error when we
# can't determine the version, rather than just issuing a warning and
# writing a version of "Unknown".  Maybe later.

import subprocess
import sys

unknown = "Unknown"

try:
  v = subprocess.Popen(("svnversion", "-c"), stdout = subprocess.PIPE).communicate()[0]
  err = None
except Exception, e:
  v = unknown
  err = e

if any(s in v for s in ("Unversioned", "Uncommitted", unknown)):
  v = unknown
else:
  v = "0." + v.strip().split(":")[-1].translate(None, "SMP")

try:
  old = open("VERSION", "r").read().strip()
except:
  old = None

if err is not None and (old is None or old == unknown):
  sys.stderr.write("Warning: No saved version and svnversion failed: %s\n" % err)

if v == unknown:
  if old is not None and old != unknown:
    v = old
  else:
    sys.stderr.write("Warning: Could not determine software version\n")

if old is None or v != old:
  with open("VERSION", "w") as f:
    f.write(v + "\n")
  with open("rpkid/rpki/version.py", "w") as f:
    f.write("VERSION = \"%s\"\n" % v)