source: 2013/30/build_games.py @ 5521

Revision 4687, 6.4 KB checked in by tojukarp, 6 years ago (diff)

Script and library updates

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