source: 2013/30/build_games.py @ 4685

Revision 4685, 6.0 KB checked in by tojukarp, 6 years ago (diff)

Updated build scripts

Line 
1# -*- coding: latin-1 -*-
2"""
3Builds all the .csproj-projects in all subdirectories, copies
4compiled games to directory games and optionally writes the
5results of the build.
6"""
7
8from __future__ import with_statement
9import sys
10import os
11from os.path import *
12import shutil
13from shutil import *
14from glob import *
15from subprocess import *
16from datetime import *
17import re
18
19should_write_results = True
20
21output_dir = abspath("pelit")
22build_result_dir = abspath("build")
23lib_dir_x86 = abspath("lib")
24personal_dirs = [d for d in os.listdir(".") if isdir(d) and d not in ("lib", basename(output_dir), ".svn")]
25projects_that_did_not_build = []
26users_that_did_not_build = []
27msbuild = "msbuild"
28
29
30
31def check_requirements():
32    global msbuild
33    if not exists("lib\\Jypeli.dll"):
34        raise Exception("lib\\Jypeli.dll expected")
35    return_code = call(msbuild + " /? 1> NUL 2> NUL", shell=True)
36    if return_code != 0:
37        msbuild = join(os.getenv("SystemRoot"), "Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe")
38        if not exists(msbuild):
39            raise Exception("MSBuild not found in " + msbuild)
40
41
42def create_output_directory():
43    if not exists(output_dir):
44        os.mkdir(output_dir)
45    for d in personal_dirs:
46        if not exists(join(output_dir, d)):
47            os.mkdir(join(output_dir, d))
48
49def create_build_result_directory():
50    if not exists(build_result_dir):
51        os.mkdir(build_result_dir)
52
53
54def parse_last_changed_date(svn_info_output):
55    match = re.search("Last Changed Date: (\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d) (\\d\\d):(\\d\\d)", svn_info_output)
56    if not match:
57        raise Exception("Last Changed Date not found from svn info output!")
58    year = int(match.group(1))
59    month = int(match.group(2))
60    day = int(match.group(3))
61    hour = int(match.group(4))
62    minute = int(match.group(5))
63    return datetime(year=year, month=month, day=day, hour=hour, minute=minute)
64
65
66def build_games():
67    for d in personal_dirs:
68        path = abspath(d)
69        time = datetime(2000, 1, 1)
70        if should_write_results and exists(join(path, ".svn")):
71            call("svn update " + path, shell=True)
72            outputs = Popen("svn info " + path, stdout=PIPE).communicate()
73            time = parse_last_changed_date(outputs[0])
74            try:
75                build_games_in_personal_dir(path, time)
76            except Exception, e:
77                print
78                print "NOT SUCCESSFUL FOR USER ", d, ": ", e
79                users_that_did_not_build.append(d)
80
81
82counter = 1
83
84
85def get_weekday_name(n):
86    if n == 0: return "Maanantai"
87    if n == 1: return "Tiistai"
88    if n == 2: return "Keskiviikko"
89    if n == 3: return "Torstai"
90    if n == 4: return "Perjantai"
91    if n == 5: return "Lauantai"
92    if n == 6: return "Sunnuntai"
93    raise Exception("Invalid weekday number")
94
95
96def write_result(author, name, success, last_changed_date):
97    global counter
98    with open(join(build_result_dir, str(counter)+".txt"), "w") as f:
99        f.write(author+"\n")
100        f.write(name+"\n")
101        result = "success"
102        if not success:
103            result = "fail"
104        f.write(result+"\n")
105        formatted_time = get_weekday_name(last_changed_date.weekday()) + last_changed_date.strftime(" %H:%M")
106        f.write(formatted_time+"\n")
107    counter += 1
108
109
110def build_games_in_personal_dir(personal_dir, last_changed_date):
111    for root, dirs, files in os.walk(personal_dir):
112        if '.svn' in dirs:
113            dirs.remove('.svn')
114        projects = glob(join(root, '*.csproj'))
115        if projects:
116            if len(projects) > 1:
117                print "NOTE: There is more than one project file in", root
118
119            try:
120                project_name = splitext(basename(projects[0]))[0]
121                author = basename(personal_dir)
122
123                success = build(projects[0])
124
125                if should_write_results:
126                    write_result(author, project_name, success, last_changed_date)
127
128                if success:
129                    dst_dir = join(output_dir, author)
130                    copy_game(project_name, root, dst_dir)
131                    print author + ": " + project_name
132                else:
133                    projects_that_did_not_build.append(projects[0])
134            except Exception, e:
135                print
136                print "PROJECT CAUSED EXCEPTION:", project_name, " :", e
137                projects_that_did_not_build.append(projects[0])
138
139
140def build(project_file_path):
141    build_cmd = msbuild + ' /nologo /verbosity:quiet /p:XnaProfile=Reach /p:Configuration=Release /p:"ReferencePath=%s" /t:Build "%s"' % (lib_dir_x86, project_file_path)
142    return call(build_cmd, shell=True) == 0
143
144
145def copy_game(project_name, project_dir, dst_dir):
146    bin = join(project_dir, "bin\\x86\\Release")
147    if not exists(bin):
148        print "NO BIN DIR FOUND IN", project_dir
149    dst = join(dst_dir, project_name)
150    if exists(dst):
151        rmtree(dst)
152    copytree(bin, dst, ignore=shutil.ignore_patterns('.svn', '*.pdb', 'jypeli.xml'))
153
154
155def parse_arguments():
156    for arg in sys.argv[1:]:
157        if arg == "--write-results":
158            global should_write_results
159            should_write_results = True
160        else:
161            raise Exception("Unknown argument: " + arg)
162
163
164def main():
165    try:
166        parse_arguments()
167        check_requirements()
168        create_output_directory()
169        if should_write_results: create_build_result_directory()
170        build_games()
171    except KeyboardInterrupt:
172        print "BUILD CANCELLED"
173    except Exception, e:
174        print
175        print "NOT SUCCESSFUL:", e
176        return 1
177
178    print 'Build failed for users (%d):' % len(users_that_did_not_build)
179    for p in users_that_did_not_build:
180        print p
181
182    print
183    print 'Build failed for projects (%d):' % len(projects_that_did_not_build)
184    for p in projects_that_did_not_build:
185        print p
186
187
188if __name__ == '__main__':
189    sys.exit(main())
Note: See TracBrowser for help on using the repository browser.