source: 2011/31/build_games.py @ 2621

Revision 2621, 5.2 KB checked in by tekrjant, 8 years ago (diff)

--write-results -optio.

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