summaryrefslogtreecommitdiff
path: root/CreateVersionFile.py
blob: 59063addb1aa936f434328034ef4e994a0116ea5 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# libavg - Media Playback Engine.
# Copyright (C) 2003-2014 Ulrich von Zadow
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Current versions can be found at www.libavg.de
#

# The script generates a header file that contains versioning data for
# the current build.

import os
import sys
import subprocess
import errno
import re
import socket
import getpass
import platform
import datetime
import pickle

CHECK_FIELDS = ('releaseVersion', 'revision', 'branchurl')
OUTPUT_TEMPLATE = '''// version.h
// This file is automatically generated by CreateVersionFile.py

#define AVG_VERSION_RELEASE     "%(releaseVersion)s"
#define AVG_VERSION_FULL        "%(fullVersion)s"
#define AVG_VERSION_BRANCH_URL  "%(branchurl)s"
#define AVG_VERSION_BUILDER     "%(builder)s"
#define AVG_VERSION_BUILDTIME   "%(buildtime)s"
#define AVG_VERSION_REVISION    %(revision)s
#define AVG_VERSION_MAJOR       "%(major)s"
#define AVG_VERSION_MINOR       "%(minor)s"
#define AVG_VERSION_MICRO       "%(micro)s"
'''
TOPDIR = os.path.dirname(os.path.abspath(__file__))
INPUT_FILE = os.path.join(TOPDIR, 'm4', 'avg_version.m4')
CACHE_FILE = os.path.join(TOPDIR, 'versioninfo.cache')
OUTPUT_FILENAME = 'version.h'

def err(text):
    print >>sys.stderr, text

def getSvnRevision():
    revision = 0
    
    try:
        process = subprocess.Popen(['svnversion'],
                cwd=TOPDIR,
                stdout=subprocess.PIPE)

        output, discard = process.communicate()
    except OSError, e:
        print e, dir(e)
        if e.errno == errno.ENOENT:
            err('Cannot query current revision number via svninfo: '
                    '"svnversion" executable cannot be found.')
    else:
        match = re.search(r'^(\d+)(?::(\d+))?', output)
        if match:
            if match.group(2):
                revision = match.group(2)
            else:
                revision = match.group(1)
            
    return int(revision)

def getSvnBranch():
    url = ''
    branch = 'exported'
    
    try:
        env = os.environ.copy()
        env['LC_ALL'] = 'C'
        process = subprocess.Popen(['svn', 'info', '.'],
            cwd=TOPDIR,
            stdout=subprocess.PIPE,
            env=env)

        output, discard = process.communicate()
    except OSError, e:
        if e.errno == errno.ENOENT:
            err('Cannot query current branch via svn: '
                    '"svn" executable cannot be found.')
            branch = 'unknown'
    else:
        match = re.search(r'^URL: (.+)$', output, re.M)
        if match:
            url = match.group(1)
            
            if 'svn/trunk' in url:
                branch = 'trunk'
            elif 'svn/branches/' in url:
                match = re.search(r'svn\/branches\/(.+)/?', url)
                if match:
                    branch = match.group(1)

    return (url, branch)

def getBuilder():
    user = getpass.getuser()
    hostname = socket.gethostname()
    
    return '%s@%s %s' % (user, hostname, platform.platform())

def extractComponentFromM4(text, component):
    match = re.search(r'%s\s*\].*\[\s*([A-Za-z0-9\.]+)\s*\]' % component, text, re.M)
    if match:
        return match.group(1)
    else:
        err('Cannot identify %s version component in %s' % (component, INPUT_FILE))
        sys.exit(1)
    
def getVersionComponents():
    f = open(INPUT_FILE)
    contents = f.read()
    f.close()
    
    major = extractComponentFromM4(contents, 'VERSION_MAJOR')
    minor = extractComponentFromM4(contents, 'VERSION_MINOR')
    micro = extractComponentFromM4(contents, 'VERSION_MICRO')
    
    return (major, minor, micro)
    
def assembleVersionInfo(major, minor, micro):
    releaseVersion = '%s.%s.%s' % (major, minor, micro)
    revision = getSvnRevision()
    branchurl, branch = getSvnBranch()
    builder = getBuilder()
    buildtime = datetime.datetime.now().isoformat()
    
    if revision and branch:
        fullVersion = '%s-%s/r%s' % (releaseVersion, branch, revision)
    elif revision:
        fullVersion = '%s-r%s' % (releaseVersion, revision)
    else:
        fullVersion = releaseVersion

    return locals()

def dumpVersionInfo(versionInfo):
    for k, v in versionInfo.iteritems():
        print '  %s: %s' % (k, v)

def hasChanged(versionInfo):
    try:
        cachef = open(CACHE_FILE)
    except IOError:
        return True
    
    try:
        cachedVersionInfo = pickle.load(cachef)
    except Exception, e:
        err('Corrupted %s file, forcing rewrite (%s)' % (CACHE_FILE, str(e)))
        cachef.close()
        return True
    
    cachef.close()

    for field in CHECK_FIELDS:
        if versionInfo.get(field) != cachedVersionInfo.get(field):
            return True
    
    return False
    
def writeVersionHeader(versionInfo, originalFile):
    outf = open(originalFile, 'w')
    outf.write(OUTPUT_TEMPLATE % versionInfo)
    outf.close()
    
    try:
        cachef = open(CACHE_FILE, 'w')
    except IOError:
        pass
    else:
        pickle.dump(versionInfo, cachef)
        cachef.close()

def main(topBuildDir=TOPDIR):
    versionInfo = assembleVersionInfo(*getVersionComponents())
    outputFile = os.path.join(topBuildDir, OUTPUT_FILENAME)
    
    # Avoid to write again if the content hasn't significantly changed
    if not os.path.exists(outputFile) or hasChanged(versionInfo):
        dumpVersionInfo(versionInfo)
        writeVersionHeader(versionInfo, outputFile)


if __name__ == '__main__':
    if len(sys.argv) == 2:
        main(sys.argv[1])
    elif len(sys.argv) == 1:
        main()
    else:
        err('%s [top build dir]' % sys.argv[0])
        sys.exit(1)