source: 2011/31/build_games.py @ 2620

Revision 2620, 4.8 KB checked in by tekrjant, 8 years ago (diff)

Yhtenäisempi whitespace.

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