Add jQuery JS and a util file to define namespaces
[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 RESOURCES_CLOSURE = [
56 'common.css',
57 'dialog.css',
58 'menu.css',
59 'menuitem.css',
60 'menubutton.css',
61 ]
62 PRODUCT_NAME = 'armadillo'
63
64 # The Golang version (hg id).
65 BACK_END_COMPILER_VERSION = '95d2ce135523 (release-branch.r57) release/release.r57.1'
66
67 COMPILER = '6g'
68 LINKER = '6l'
69 O_EXTENSION = '6'
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.strip().endswith(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 args = [ LINKER, '-o', os.path.join(PROD_PATH, PRODUCT_NAME), 'main.' + O_EXTENSION ]
95 print ' ' + ' ' .join(args)
96 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
97 handle.wait()
98
99 def _StampVersion(options):
100 print '=== Version Stamp ==='
101 if os.path.exists(VERSION_FILE):
102 gitcrement = subprocess.Popen([ 'gitcrement', 'next' ], stdout = subprocess.PIPE, cwd = ROOT)
103 gitcrement.wait()
104 build_stamp = gitcrement.stdout.read().strip()
105 time_stamp = str(int(time.time()))
106
107 fd = open(VERSION_FILE, 'a+')
108 fd.seek(0)
109 lines = fd.readlines()
110 fd.seek(0)
111 fd.truncate()
112 for line in lines:
113 line = re.sub(r'(BUILD =) ([0-9\.]+)', r'\1 ' + build_stamp, line)
114 line = re.sub(r'(STAMP =) ([0-9]+)', r'\1 ' + time_stamp, line)
115 fd.write(line)
116 fd.close()
117 print ' BUILD ' + build_stamp + ' @ ' + time_stamp
118 if options.compile_fe:
119 mfiles = subprocess.Popen([ 'git', 'ls-files', '-m' ], stdout = subprocess.PIPE, cwd = ROOT)
120 mfiles.wait()
121 versioned_stamp_file = string.replace(VERSION_FILE, '.proto', '')
122 shutil.copy(VERSION_FILE, versioned_stamp_file)
123 print ' COPY version.js.proto -> version.js'
124 if not len(mfiles.stdout.readlines()):
125 subprocess.Popen([ 'git', 'commit', '--author=Armadillo Build Script <armadillo@bluestatic.org>',
126 '-m', 'Stamp version.js @ ' + build_stamp + '.', versioned_stamp_file ], stdout = sys.stdout,
127 stderr = sys.stderr).wait()
128
129 def _CompileFrontEnd(options):
130 _PullDeps()
131
132 # Copy
133 print '=== Copying Resources ==='
134 fe_resources = os.path.join(PROD_PATH, 'fe')
135 subprocess.Popen([ 'rm', '-rf', fe_resources ]).wait()
136 os.mkdir(fe_resources)
137 for resource in RESOURCES_FE:
138 print ' COPY ' + resource
139 shutil.copy(os.path.join(FE_PATH, resource), fe_resources)
140 fd = open(os.path.join(fe_resources, 'closure.css'), 'w+')
141 fd.write('/*=== Generated Resources for Closure Library ===*/')
142 for resource in RESOURCES_CLOSURE:
143 print ' COPY closure/' + resource
144 respath = os.path.join(CLOSURE_DEST, 'closure', 'goog', 'css', resource)
145 ofd = open(respath, 'r')
146 fd.write('\n\n/*=== File: ' + respath.replace(ROOT, '/') + ' ===*/\n')
147 fd.writelines(ofd.readlines())
148 ofd.close()
149 fd.close()
150
151 # Version
152 _StampVersion(options)
153
154 # Compile JS.
155 print '=== Compiling Front End ==='
156 outfile = os.path.join(PROD_PATH, 'fe', PRODUCT_NAME + '.js')
157 fe_sources = map(lambda f: '-i' + os.path.join(FE_PATH, f), SOURCES_FE)
158 closure_sources = os.path.join(CLOSURE_DEST, 'closure', 'goog')
159 args = [ CLOSURE_CALCDEPS ]
160 args.extend(fe_sources)
161 output = "script"
162 if options.compile_fe:
163 output = "compiled"
164 args.extend([ '-p', closure_sources, '-o', output, '-c', CLOSURE_COMPILER,
165 '--output_file', outfile ])
166 print ' ' + ' '.join(args)
167 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
168 handle.wait()
169
170
171 def Main():
172 parser = optparse.OptionParser()
173 parser.add_option('-c', '--closure_fe', action="store_true", dest="compile_fe",
174 help="Run the Front End inputs through the Closure Compiler")
175 parser.add_option('-b', '--back-end', action="store_true", dest="backend_only",
176 help="Compiles only the back-end")
177 parser.add_option('-f', '--front-end', action="store_true", dest="frontend_only",
178 help="Compiles only the front-end")
179 (options, args) = parser.parse_args()
180
181 print '=== Starting Build ==='
182 os.chdir(PROD_PATH)
183
184 if not options.frontend_only:
185 _CompileBackEnd()
186
187 if not options.backend_only:
188 _CompileFrontEnd(options)
189
190 if __name__ == '__main__':
191 Main()