Revert "Add logic to handle remote service requests by serving a separate HTML front...
[armadillo.git] / build.py
1 #!/usr/bin/env python2.5
2 #
3 # Armadillo File Manager
4 # Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
5 #
6 # This program is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free Software
8 # Foundation, either version 3 of the License, or any later version.
9 #
10 import optparse
11 import os
12 import re
13 import shutil
14 import string
15 import subprocess
16 import sys
17 import time
18
19 ROOT = os.path.dirname(os.path.realpath(__file__))
20 SRC_PATH = os.path.join(ROOT, 'src')
21 PROD_PATH = os.path.join(ROOT, 'out')
22 FE_PATH = os.path.join(ROOT, 'web_frontend')
23
24 CLOSURE_SVN = 'http://closure-library.googlecode.com/svn/trunk/'
25 CLOSURE_REV = '235'
26 CLOSURE_DEST = os.path.join(ROOT, 'closure')
27 CLOSURE_COMPILER = os.path.join(ROOT, 'closure-compiler.jar')
28 CLOSURE_CALCDEPS = os.path.join(CLOSURE_DEST, 'closure', 'bin', 'calcdeps.py')
29
30 VERSION_FILE = os.path.join(FE_PATH, 'version.js.proto')
31
32 SOURCES = [
33 'config.go',
34 'paths.go',
35 'server.go',
36 'main.go'
37 ]
38 SOURCES_FE = [
39 'version.js',
40 'tv_renamer.js',
41 'path_control.js',
42 'actor.js',
43 'file.js',
44 'main.js',
45 ]
46 RESOURCES_FE = [
47 'index.html',
48 'screen.css',
49 'reset.css'
50 ]
51 RESOURCES_CLOSURE = [
52 'common.css',
53 'dialog.css',
54 'menu.css',
55 'menuitem.css',
56 'menubutton.css',
57 ]
58 PRODUCT_NAME = 'armadillo'
59
60 # The Golang version (hg id).
61 BACK_END_COMPILER_VERSION = 'a7800e20064a release/release.2010-11-10'
62
63 COMPILER = '8g'
64 LINKER = '8l'
65 O_EXTENSION = '8'
66
67 def _ObjFileName(gofile):
68 gofile = os.path.basename(gofile)
69 return os.path.join(PROD_PATH, os.path.splitext(gofile)[0] + '.' + O_EXTENSION)
70
71 def _PullDeps():
72 print '=== Pulling Dependencies ==='
73 if os.path.exists(CLOSURE_DEST):
74 handle = subprocess.Popen([ 'svn', 'info', CLOSURE_DEST ], stdout = subprocess.PIPE)
75 handle.wait()
76 for line in handle.stdout:
77 if line.startswith('Revision'):
78 if not line.startswith('Revision: ' + CLOSURE_REV):
79 subprocess.Popen([ 'svn', 'update', '-r', CLOSURE_REV, CLOSURE_DEST ]).wait()
80 else:
81 print ' Closure @ ' + CLOSURE_REV
82 else:
83 subprocess.Popen([ 'svn', 'checkout', '-r', CLOSURE_REV, CLOSURE_SVN, CLOSURE_DEST ]).wait()
84
85 def _CompileBackEnd():
86 for gofile in SOURCES:
87 gofile = os.path.join(SRC_PATH, gofile)
88 args = [ COMPILER, gofile ]
89 print ' ' + ' '.join(args)
90 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
91 handle.wait()
92
93 # Link
94 objects = map(_ObjFileName, SOURCES)
95 args = [ LINKER, '-o', os.path.join(PROD_PATH, PRODUCT_NAME), 'main.8' ]
96 print ' ' + ' ' .join(args)
97 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
98 handle.wait()
99
100 def _StampVersion(options):
101 print '=== Version Stamp ==='
102 if os.path.exists(VERSION_FILE):
103 gitcrement = subprocess.Popen([ 'gitcrement', 'next' ], stdout = subprocess.PIPE, cwd = ROOT)
104 gitcrement.wait()
105 build_stamp = gitcrement.stdout.read().strip()
106 time_stamp = str(int(time.time()))
107
108 fd = open(VERSION_FILE, 'a+')
109 fd.seek(0)
110 lines = fd.readlines()
111 fd.seek(0)
112 fd.truncate()
113 for line in lines:
114 line = re.sub(r'(BUILD =) ([0-9\.]+)', r'\1 ' + build_stamp, line)
115 line = re.sub(r'(STAMP =) ([0-9]+)', r'\1 ' + time_stamp, line)
116 fd.write(line)
117 fd.close()
118 print ' BUILD ' + build_stamp + ' @ ' + time_stamp
119 if options.compile_fe:
120 mfiles = subprocess.Popen([ 'git', 'ls-files', '-m' ], stdout = subprocess.PIPE, cwd = ROOT)
121 mfiles.wait()
122 versioned_stamp_file = string.replace(VERSION_FILE, '.proto', '')
123 shutil.copy(VERSION_FILE, versioned_stamp_file)
124 print ' COPY version.js.proto -> version.js'
125 if not len(mfiles.stdout.readlines()):
126 subprocess.Popen([ 'git', 'commit', '--author=Armadillo Build Script <armadillo@bluestatic.org>',
127 '-m', 'Stamp version.js @ ' + build_stamp + '.', versioned_stamp_file ], stdout = sys.stdout,
128 stderr = sys.stderr).wait()
129
130 def _CompileFrontEnd(options):
131 _PullDeps()
132
133 # Copy
134 print '=== Copying Resources ==='
135 fe_resources = os.path.join(PROD_PATH, 'fe')
136 subprocess.Popen([ 'rm', '-rf', fe_resources ]).wait()
137 os.mkdir(fe_resources)
138 for resource in RESOURCES_FE:
139 print ' COPY ' + resource
140 shutil.copy(os.path.join(FE_PATH, resource), fe_resources)
141 fd = open(os.path.join(fe_resources, 'closure.css'), 'w+')
142 fd.write('/*=== Generated Resources for Closure Library ===*/')
143 for resource in RESOURCES_CLOSURE:
144 print ' COPY closure/' + resource
145 respath = os.path.join(CLOSURE_DEST, 'closure', 'goog', 'css', resource)
146 ofd = open(respath, 'r')
147 fd.write('\n\n/*=== File: ' + respath.replace(ROOT, '/') + ' ===*/\n')
148 fd.writelines(ofd.readlines())
149 ofd.close()
150 fd.close()
151
152 # Version
153 _StampVersion(options)
154
155 # Compile JS.
156 print '=== Compiling Front End ==='
157 outfile = os.path.join(PROD_PATH, 'fe', PRODUCT_NAME + '.js')
158 fe_sources = map(lambda f: '-i' + os.path.join(FE_PATH, f), SOURCES_FE)
159 closure_sources = os.path.join(CLOSURE_DEST, 'closure', 'goog')
160 args = [ CLOSURE_CALCDEPS ]
161 args.extend(fe_sources)
162 output = "script"
163 if options.compile_fe:
164 output = "compiled"
165 args.extend([ '-p', closure_sources, '-o', output, '-c', CLOSURE_COMPILER,
166 '--output_file', outfile ])
167 print ' ' + ' '.join(args)
168 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
169 handle.wait()
170
171
172 def Main():
173 parser = optparse.OptionParser()
174 parser.add_option('-c', '--closure_fe', action="store_true", dest="compile_fe",
175 help="Run the Front End inputs through the Closure Compiler")
176 parser.add_option('-b', '--back-end', action="store_true", dest="backend_only",
177 help="Compiles only the back-end")
178 parser.add_option('-f', '--front-end', action="store_true", dest="frontend_only",
179 help="Compiles only the front-end")
180 (options, args) = parser.parse_args()
181
182 print '=== Starting Build ==='
183 os.chdir(PROD_PATH)
184
185 if not options.frontend_only:
186 _CompileBackEnd()
187
188 if not options.backend_only:
189 _CompileFrontEnd(options)
190
191 if __name__ == '__main__':
192 Main()