Simplify the build script now that |go build| exists
[armadillo.git] / build.py
1 #!/usr/bin/env python2.5
2 #
3 # Armadillo File Manager
4 # Copyright (c) 2010-2012, 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, 'frontend')
23 FE_PATH = os.path.join(ROOT, 'frontend')
24
25 CLOSURE_COMPILER = os.path.join(ROOT, 'closure-compiler.jar')
26
27 VERSION_FILE = os.path.join(FE_PATH, 'version.js.proto')
28
29 SOURCES_FE = [
30 'jquery-1.7.1.js',
31 'utils.js',
32 'version.js',
33 'tv_renamer.js',
34 'path_control.js',
35 'actor.js',
36 'file.js',
37 'main.js',
38 ]
39 RESOURCES_FE = [
40 'index.html',
41 'screen.css',
42 'reset.css'
43 ]
44 PRODUCT_NAME = 'armadillo'
45
46 # The Golang version (hg id).
47 BACK_END_COMPILER_VERSION = 'c1702f36df03 (release-branch.r60) release/release.r60.3'
48
49 def _CompileBackEnd():
50 args = [ 'go', 'build', '-v', '.' ]
51 print ' ' + ' ' .join(args)
52 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
53 handle.wait()
54
55 def _StampVersion(options):
56 print '=== Version Stamp ==='
57 if os.path.exists(VERSION_FILE):
58 gitcrement = subprocess.Popen([ 'gitcrement', 'next' ], stdout = subprocess.PIPE, cwd = ROOT)
59 gitcrement.wait()
60 build_stamp = gitcrement.stdout.read().strip()
61 time_stamp = str(int(time.time()))
62
63 fd = open(VERSION_FILE, 'a+')
64 fd.seek(0)
65 lines = fd.readlines()
66 fd.seek(0)
67 fd.truncate()
68 for line in lines:
69 line = re.sub(r'(BUILD =) ([0-9\.]+)', r'\1 ' + build_stamp, line)
70 line = re.sub(r'(STAMP =) ([0-9]+)', r'\1 ' + time_stamp, line)
71 fd.write(line)
72 fd.close()
73 print ' BUILD ' + build_stamp + ' @ ' + time_stamp
74 if options.compile_fe:
75 mfiles = subprocess.Popen([ 'git', 'ls-files', '-m' ], stdout = subprocess.PIPE, cwd = ROOT)
76 mfiles.wait()
77 versioned_stamp_file = string.replace(VERSION_FILE, '.proto', '')
78 shutil.copy(VERSION_FILE, versioned_stamp_file)
79 print ' COPY version.js.proto -> version.js'
80 if not len(mfiles.stdout.readlines()):
81 subprocess.Popen([ 'git', 'commit', '--author=Armadillo Build Script <armadillo@bluestatic.org>',
82 '-m', 'Stamp version.js @ ' + build_stamp + '.', versioned_stamp_file ], stdout = sys.stdout,
83 stderr = sys.stderr).wait()
84
85 def _CompileFrontEnd(options):
86 # Version
87 _StampVersion(options)
88
89 # Compile JS.
90 print '=== Compiling Front End ==='
91 outfile = os.path.join(PROD_PATH, PRODUCT_NAME + '.js')
92 if options.compile_fe:
93 fe_sources = map(lambda f: '--js=' + os.path.join(FE_PATH, f), SOURCES_FE)
94 args = [ 'java', '-jar', CLOSURE_COMPILER ]
95 args.extend(fe_sources)
96 args.extend(['--js_output_file', outfile, '--compilation_level', 'SIMPLE_OPTIMIZATIONS'])
97 print ' ' + ' '.join(args)
98 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
99 handle.wait()
100 else:
101 fd = open(outfile, 'w+')
102 for fe_source in SOURCES_FE:
103 fd2 = open(os.path.join(FE_PATH, fe_source), 'r')
104 fd.write('// === ' + fe_source + '\n')
105 fd.write(fd2.read())
106 fd2.close()
107 fd.close()
108 print ' DONE'
109
110
111 def Main():
112 parser = optparse.OptionParser()
113 parser.add_option('-c', '--closure_fe', action="store_true", dest="compile_fe",
114 help="Run the Front End inputs through the Closure Compiler")
115 parser.add_option('-b', '--back-end', action="store_true", dest="backend_only",
116 help="Compiles only the back-end")
117 parser.add_option('-f', '--front-end', action="store_true", dest="frontend_only",
118 help="Compiles only the front-end")
119 (options, args) = parser.parse_args()
120
121 print '=== Starting Build ==='
122 os.chdir(ROOT)
123 if not options.frontend_only:
124 _CompileBackEnd()
125
126 if not options.backend_only:
127 _CompileFrontEnd(options)
128
129 if __name__ == '__main__':
130 Main()