File: daily_build.py

package info (click to toggle)
gamera 3.4.1%2Bsvn1423-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 22,292 kB
  • ctags: 25,015
  • sloc: xml: 122,324; ansic: 50,812; cpp: 50,489; python: 34,987; makefile: 119; sh: 101
file content (181 lines) | stat: -rwxr-xr-x 6,205 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python

#
# Copyright (C) 2008-2009 Michael Droettboom
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

# This is a total hack to generate and upload the daily builds.
# Nothing about this file should be expected to be portable between
# machines.

# This script runs successfully on an Ubuntu 8.04 LTS system with
# (at least) the following packages installed:
#     python-dev
#     python2.4
#     python2.4-dev
#     mingw32
#     g++
#     libpng12-dev
#     libtiff4-dev
#     python-docutils
#     python-pygments
#     openssh-client
#     rsync

import datetime
import os
import shutil
import sys
from distutils.util import get_platform
import subprocess

import pysvn

ROOT_PATH = "/home/mdboom/JHU/builds/"
WORKING_PATH = "gamera-daily-build"
REPOS_PATH = "https://gamera.svn.sf.net/svnroot/gamera/trunk/gamera"
STAGING_PATH = "gamera-daily"
RSYNC_TARGET = "mdboom@xs004.mse.jhu.edu:/mnt/gamera-builds/"
KEEP = 10

def mysystem(message, command):
    print message,
    try:
        # process = subprocess.Popen(command, shell=True,
        #                            stdout=subprocess.PIPE,
        #                            stderr=subprocess.PIPE)
        # retcode = process.wait()
        retcode = os.system(command)
        if retcode != 0:
            print "FAILED (%d)" % retcode
            return
    except:
        print "FAILED"
        raise
    else:
        print "SUCCESS"

def myrmtree(path):
    if os.path.exists(path):
        shutil.rmtree(path)

def mymkdir(path):
    if not os.path.exists(path):
        os.mkdir(path)

def update_working_copy():
    client = pysvn.Client()

    push_update = False
    if os.path.exists(WORKING_PATH):
        current_working = client.info(WORKING_PATH)['revision'].number
        current_remote = 0
        for rev in client.update(WORKING_PATH):
            current_remote = max(current_remote, rev.number)
        if current_remote > current_working:
            print "Updates in SVN"
            push_update = True
    else:
        print "Checking out clean"
        client.checkout(REPOS_PATH, WORKING_PATH)
        current_working = client.info(WORKING_PATH)['revision'].number
        push_update = True
    return push_update, current_working

def get_version(now, current_working):
    return "daily-%s-SVN-r%s" % (now, current_working)

def update_version(version):
    fd = open(os.path.join(WORKING_PATH, "version"), "w")
    fd.write(version)
    fd.close()

def build(version):
    build_dir = os.path.abspath(os.path.join(WORKING_PATH, "build", "lib.%s-%s" % (get_platform(), sys.version[0:3])))
    if "--clean" in sys.argv:
        myrmtree(os.path.join(WORKING_PATH, "build"))
        myrmtree(os.path.join(WORKING_PATH, "dist"))
    myrmtree(os.path.join(WORKING_PATH, "doc/html"))
    os.chdir(WORKING_PATH)
    mysystem("Building for Linux (Python 2.6)...", "python setup.py build")
    mysystem("Building source distribution...", "python setup.py sdist")
    mysystem("Building for Windows...", "python setup.py build --compiler=mingw32_cross bdist_wininst")
    os.chdir("doc")
    mysystem("Building docs...", "export PYTHONPATH=%s:$PYTHONPATH; python gendoc.py" % build_dir)
    os.rename("html", "gamera-doc-%s" % version)
    mysystem("tar-gzipping docs...", "tar czvf gamera-doc-%s.tar.gz gamera-doc-%s" % (version, version))
    mysystem("zipping docs...", "zip -r gamera-doc-%s.zip gamera-doc-%s" % (version, version))
    os.rename("gamera-doc-%s" % version, "html")
    os.chdir("../..")

def rotate(path, number=2):
    files = []
    for f in os.listdir(path):
        p = os.path.join(path, f)
        files.append((os.stat(p).st_mtime, p))
    if len(files) < KEEP * number:
        return
    files.sort()
    files.reverse()
    remove = files[KEEP * number:]
    for t, f in remove:
        os.path.remove(f)

def stage(version):
    mymkdir(os.path.join(STAGING_PATH, "src"))
    mymkdir(os.path.join(STAGING_PATH, "win32"))
    mymkdir(os.path.join(STAGING_PATH, "doc"))
    shutil.copy2(os.path.join(WORKING_PATH, "dist", "gamera-%s.tar.gz" % version),
                 os.path.join(STAGING_PATH, "src"))
    shutil.copy2(os.path.join(WORKING_PATH, "dist", "gamera-%s.win32-py%d.%d.exe" %
                              (version, sys.version_info[0], sys.version_info[1])),
                 os.path.join(STAGING_PATH, "win32"))
    shutil.copy2(os.path.join(WORKING_PATH, "doc", "gamera-doc-%s.tar.gz" % version),
                 os.path.join(STAGING_PATH, "doc"))
    shutil.copy2(os.path.join(WORKING_PATH, "doc", "gamera-doc-%s.zip" % version),
                 os.path.join(STAGING_PATH, "doc"))
    rotate(os.path.join(STAGING_PATH, "src"))
    rotate(os.path.join(STAGING_PATH, "win32"))
    rotate(os.path.join(STAGING_PATH, "doc"), 2)

def rsync():
    mysystem("uploading", "rsync -e ssh -arvz --delete %s/* %s" %
             (STAGING_PATH, RSYNC_TARGET))

def main():
    os.chdir(ROOT_PATH)
    now = datetime.datetime.now().strftime("%Y%m%d")
    mymkdir(STAGING_PATH)
    log_file = open(os.path.join(STAGING_PATH, "daily-build.log"), "a")
    sys.stdout = log_file
    sys.stderr = log_file
    print "-" * 76
    print "RUNNING:", now
    push_update, current_working = update_working_copy()
    if '--force' in sys.argv:
        push_update = True
    if not push_update:
        print "No updates today."
    else:
        version = get_version(now, current_working)
        update_version(version)
        build(version)
        stage(version)
    rsync()

if __name__ == '__main__':
    main()