Remove closure resources step
[armadillo.git] / build.py
1 #!/usr/bin/env python2.5
2 #
3 # Armadillo File Manager
4 # Copyright (c) 2010-2011, 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
11 import optparse
12 import os
13 import re
14 import shutil
15 import string
16 import subprocess
17 import sys
18 import time
19
20 ROOT = os.path.dirname(os.path.realpath(__file__))
21 SRC_PATH = os.path.join(ROOT, 'src')
22 PROD_PATH = os.path.join(ROOT, 'out')
23 FE_PATH = os.path.join(ROOT, 'web_frontend')
24
25 CLOSURE_SVN = 'http://closure-library.googlecode.com/svn/trunk/'
26 CLOSURE_REV = '886'
27 CLOSURE_DEST = os.path.join(ROOT, 'closure')
28 CLOSURE_COMPILER = os.path.join(ROOT, 'closure-compiler.jar')
29 CLOSURE_CALCDEPS = os.path.join(CLOSURE_DEST, 'closure', 'bin', 'calcdeps.py')
30
31 VERSION_FILE = os.path.join(FE_PATH, 'version.js.proto')
32
33 SOURCES = [
34 'config.go',
35 'paths.go',
36 'tv_rename.go',
37 'server.go',
38 'main.go'
39 ]
40 SOURCES_FE = [
41 'jquery-1.7.1.js',
42 'utils.js',
43 'version.js',
44 'tv_renamer.js',
45 'path_control.js',
46 'actor.js',
47 'file.js',
48 'main.js',
49 ]
50 RESOURCES_FE = [
51 'index.html',
52 'screen.css',
53 'reset.css'
54 ]
55 PRODUCT_NAME = 'armadillo'
56
57 # The Golang version (hg id).
58 BACK_END_COMPILER_VERSION = '95d2ce135523 (release-branch.r57) release/release.r57.1'
59
60 COMPILER = '6g'
61 LINKER = '6l'
62 O_EXTENSION = '6'
63
64 def _PullDeps():
65 print '=== Pulling Dependencies ==='
66 if os.path.exists(CLOSURE_DEST):
67 handle = subprocess.Popen([ 'svn', 'info', CLOSURE_DEST ], stdout = subprocess.PIPE)
68 handle.wait()
69 for line in handle.stdout:
70 if line.startswith('Revision'):
71 if not line.strip().endswith(CLOSURE_REV):
72 subprocess.Popen([ 'svn', 'update', '-r', CLOSURE_REV, CLOSURE_DEST ]).wait()
73 else:
74 print ' Closure @ ' + CLOSURE_REV
75 else:
76 subprocess.Popen([ 'svn', 'checkout', '-r', CLOSURE_REV, CLOSURE_SVN, CLOSURE_DEST ]).wait()
77
78 def _CompileBackEnd():
79 for gofile in SOURCES:
80 gofile = os.path.join(SRC_PATH, gofile)
81 args = [ COMPILER, gofile ]
82 print ' ' + ' '.join(args)
83 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
84 handle.wait()
85
86 # Link
87 args = [ LINKER, '-o', os.path.join(PROD_PATH, PRODUCT_NAME), 'main.' + O_EXTENSION ]
88 print ' ' + ' ' .join(args)
89 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
90 handle.wait()
91
92 def _StampVersion(options):
93 print '=== Version Stamp ==='
94 if os.path.exists(VERSION_FILE):
95 gitcrement = subprocess.Popen([ 'gitcrement', 'next' ], stdout = subprocess.PIPE, cwd = ROOT)
96 gitcrement.wait()
97 build_stamp = gitcrement.stdout.read().strip()
98 time_stamp = str(int(time.time()))
99
100 fd = open(VERSION_FILE, 'a+')
101 fd.seek(0)
102 lines = fd.readlines()
103 fd.seek(0)
104 fd.truncate()
105 for line in lines:
106 line = re.sub(r'(BUILD =) ([0-9\.]+)', r'\1 ' + build_stamp, line)
107 line = re.sub(r'(STAMP =) ([0-9]+)', r'\1 ' + time_stamp, line)
108 fd.write(line)
109 fd.close()
110 print ' BUILD ' + build_stamp + ' @ ' + time_stamp
111 if options.compile_fe:
112 mfiles = subprocess.Popen([ 'git', 'ls-files', '-m' ], stdout = subprocess.PIPE, cwd = ROOT)
113 mfiles.wait()
114 versioned_stamp_file = string.replace(VERSION_FILE, '.proto', '')
115 shutil.copy(VERSION_FILE, versioned_stamp_file)
116 print ' COPY version.js.proto -> version.js'
117 if not len(mfiles.stdout.readlines()):
118 subprocess.Popen([ 'git', 'commit', '--author=Armadillo Build Script <armadillo@bluestatic.org>',
119 '-m', 'Stamp version.js @ ' + build_stamp + '.', versioned_stamp_file ], stdout = sys.stdout,
120 stderr = sys.stderr).wait()
121
122 def _CompileFrontEnd(options):
123 _PullDeps()
124
125 # Copy
126 print '=== Copying Resources ==='
127 fe_resources = os.path.join(PROD_PATH, 'fe')
128 subprocess.Popen([ 'rm', '-rf', fe_resources ]).wait()
129 os.mkdir(fe_resources)
130 for resource in RESOURCES_FE:
131 print ' COPY ' + resource
132 shutil.copy(os.path.join(FE_PATH, resource), fe_resources)
133
134 # Version
135 _StampVersion(options)
136
137 # Compile JS.
138 print '=== Compiling Front End ==='
139 outfile = os.path.join(PROD_PATH, 'fe', PRODUCT_NAME + '.js')
140 fe_sources = map(lambda f: '-i' + os.path.join(FE_PATH, f), SOURCES_FE)
141 args = [ CLOSURE_CALCDEPS ]
142 args.extend(fe_sources)
143 output = "script"
144 if options.compile_fe:
145 output = "compiled"
146 args.extend([ '-o', output, '-c', CLOSURE_COMPILER, '--output_file', outfile ])
147 print ' ' + ' '.join(args)
148 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
149 handle.wait()
150
151
152 def Main():
153 parser = optparse.OptionParser()
154 parser.add_option('-c', '--closure_fe', action="store_true", dest="compile_fe",
155 help="Run the Front End inputs through the Closure Compiler")
156 parser.add_option('-b', '--back-end', action="store_true", dest="backend_only",
157 help="Compiles only the back-end")
158 parser.add_option('-f', '--front-end', action="store_true", dest="frontend_only",
159 help="Compiles only the front-end")
160 (options, args) = parser.parse_args()
161
162 print '=== Starting Build ==='
163 os.chdir(PROD_PATH)
164
165 if not options.frontend_only:
166 _CompileBackEnd()
167
168 if not options.backend_only:
169 _CompileFrontEnd(options)
170
171 if __name__ == '__main__':
172 Main()