summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/deploy-release.py
blob: a4b9dcea973cba8cd1831739df0981b248a64415 (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
213
214
#!/usr/bin/python
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#
# Copyright (c) 2009 Dominik Riebeling
#
# All files in this archive are subject to the GNU General Public License.
# See the file COPYING in the source tree root for full license agreement.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
#
# Automate building releases for deployment.
# Run from source folder. Error checking / handling rather is limited.
# If the required Qt installation isn't in PATH use --qmake option.
# Tested on Linux and MinGW / W32
#
# requires python which package (http://code.google.com/p/which/)
# requires upx.exe in PATH on Windows.
#

import re
import os
import sys
import tarfile
import zipfile
import shutil
import subprocess
import getopt
import which

# == Global stuff ==
# Windows nees some special treatment. Differentiate between program name
# and executable filename.
program = "rbutilqt"
if sys.platform == "win32":
    progexe = "Release/rbutilqt.exe"
else:
    progexe = program

programfiles = [ progexe ]


# == Functions ==
def usage(myself):
    print "Usage: %s [options]" % myself
    print "       -q, --qmake=<qmake>   path to qmake"
    print "       -h, --help            this help"


def findversion(versionfile):
    '''figure most recent program version from version.h,
    returns version string.'''
    h = open(versionfile, "r")
    c = h.read()
    h.close()
    r = re.compile("#define +VERSION +\"(.[0-9\.a-z]+)\"")
    m = re.search(r, c)
    s = re.compile("\$Revision: +([0-9]+)")
    n = re.search(s, c)
    if n == None:
        print "WARNING: Revision not found!"
    return m.group(1)


def findqt():
    '''Search for Qt4 installation. Return path to qmake.'''
    print "Searching for Qt"
    bins = ["qmake", "qmake-qt4"]
    result = ""
    for binary in bins:
        q = which.which(binary)
        if len(q) > 0:
            output = subprocess.Popen([q, "-version"], stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE).communicate()
            for ou in output:
                r = re.compile("Qt[^0-9]+([0-9\.]+[a-z]*)")
                m = re.search(r, ou)
                if not m == None:
                    print "Qt found: %s" % m.group(1)
                    s = re.compile("4\..*")
                    n = re.search(s, m.group(1))
                    if not n == None:
                        result = q
    if result == "":
        print "ERROR: No suitable Qt found!"

    return result


def qmake(qmake="qmake"):
    print "Running qmake ..."
    output = subprocess.Popen([qmake, "-config", "static", "-config", "release"],
                stdout=subprocess.PIPE).communicate()


def build():
    # make
    print "Building ..."
    output = subprocess.Popen(["make"], stdout=subprocess.PIPE).communicate()
    # strip
    print "Stripping binary."
    output = subprocess.Popen(["strip", progexe], stdout=subprocess.PIPE).communicate()


def upxfile():
    # run upx on binary
    print "UPX'ing binary ..."
    output = subprocess.Popen(["upx", progexe], stdout=subprocess.PIPE).communicate()


def zipball(versionstring):
    '''package created binary'''
    print "Creating binary zipball."
    outfolder = program + "-v" + versionstring
    archivename = outfolder + ".zip"
    # create output folder
    os.mkdir(outfolder)
    # move program files to output folder
    for f in programfiles:
        shutil.copy(f, outfolder)
    # create zipball from output folder
    zf = zipfile.ZipFile(archivename, mode='w', compression=zipfile.ZIP_DEFLATED)
    for root, dirs, files in os.walk(outfolder):
        for name in files:
            zf.write(os.path.join(root, name))
        for name in dirs:
            zf.write(os.path.join(root, name))
    zf.close()
    # remove output folder
    for root, dirs, files in os.walk(outfolder, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))
    os.rmdir(outfolder)
    st = os.stat(archivename)
    print "done: %s, %i bytes" % (archivename, st.st_size)


def tarball(versionstring):
    '''package created binary'''
    print "Creating binary tarball."
    outfolder = program + "-v" + versionstring
    archivename = outfolder + ".tar.bz2"
    # create output folder
    os.mkdir(outfolder)
    # move program files to output folder
    for f in programfiles:
        shutil.copy(f, outfolder)
    # create tarball from output folder
    tf = tarfile.open(archivename, mode='w:bz2')
    tf.add(outfolder)
    tf.close()
    # remove output folder
    for root, dirs, files in os.walk(outfolder, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))
    os.rmdir(outfolder)
    st = os.stat(archivename)
    print "done: %s, %i bytes" % (archivename, st.st_size)


def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "qh", ["qmake=", "help"])
    except getopt.GetoptError, err:
        print str(err)
        usage(sys.argv[0])
        sys.exit(1)
    qt = ""
    for o, a in opts:
        if o in ("-q", "--qmake"):
            qt = a
        if o in ("-h", "--help"):
            usage(sys.argv[0])
            sys.exit(0)

    # qmake path
    if qt == "":
        qt = findqt()
    if qt == "":
        print "ERROR: No suitable Qt installation found."
        os.exit(1)

    # figure version from sources
    ver = findversion("version.h")
    header = "Building %s %s" % (program, ver)
    print header
    print len(header) * "="

    # build it.
    qmake(qt)
    build()
    if sys.platform == "win32":
        upxfile()
        zipball(ver)
    else:
        tarball(ver)
    print "done."


if __name__ == "__main__":
    main()