Add version.js and the auto-versioner in build.js.
[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 'paths.go',
34 'server.go',
35 'main.go'
36 ]
37 SOURCES_FE = [
38 'actor.js',
39 'file.js',
40 'main.js',
41 ]
42 RESOURCES_FE = [
43 'index.html',
44 'screen.css',
45 'reset.css'
46 ]
47 PRODUCT_NAME = 'armadillo'
48
49 COMPILER = '8g'
50 LINKER = '8l'
51 O_EXTENSION = '8'
52
53 def _ObjFileName(gofile):
54 gofile = os.path.basename(gofile)
55 return os.path.join(PROD_PATH, os.path.splitext(gofile)[0] + '.' + O_EXTENSION)
56
57 def _PullDeps():
58 print '=== Pulling Dependencies ==='
59 if os.path.exists(CLOSURE_DEST):
60 handle = subprocess.Popen([ 'svn', 'info', CLOSURE_DEST ], stdout = subprocess.PIPE)
61 handle.wait()
62 for line in handle.stdout:
63 if line.startswith('Revision'):
64 if not line.startswith('Revision: ' + CLOSURE_REV):
65 subprocess.Popen([ 'svn', 'update', '-r', CLOSURE_REV, CLOSURE_DEST ]).wait()
66 else:
67 print ' Closure @ ' + CLOSURE_REV
68 else:
69 subprocess.Popen([ 'svn', 'checkout', '-r', CLOSURE_REV, CLOSURE_SVN, CLOSURE_DEST ]).wait()
70
71 def Main():
72 parser = optparse.OptionParser()
73 parser.add_option('-c', '--closure_fe', action="store_true", dest="compile_fe",
74 help="Run the Front End inputs through the Closure Compiler")
75 (options, args) = parser.parse_args()
76
77 print '=== Starting Build ==='
78 os.chdir(PROD_PATH)
79
80 # Compile.
81 for gofile in SOURCES:
82 gofile = os.path.join(SRC_PATH, gofile)
83 args = [ COMPILER, gofile ]
84 print ' ' + ' '.join(args)
85 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
86 handle.wait()
87
88 # Link
89 objects = map(_ObjFileName, SOURCES)
90 args = [ LINKER, '-o', os.path.join(PROD_PATH, PRODUCT_NAME), 'main.8' ]
91 print ' ' + ' ' .join(args)
92 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
93 handle.wait()
94
95 _PullDeps()
96
97 # Copy
98 print '=== Copying Resources ==='
99 fe_resources = os.path.join(PROD_PATH, 'fe')
100 subprocess.Popen([ 'rm', '-rf', fe_resources ]).wait()
101 os.mkdir(fe_resources)
102 for resource in RESOURCES_FE:
103 print ' COPY ' + resource
104 shutil.copy(os.path.join(FE_PATH, resource), fe_resources)
105
106 # Version
107 print '=== Marking Version ==='
108 if os.path.exists(VERSION_FILE):
109 gitcrement = subprocess.Popen([ 'gitcrement', 'next' ], stdout = subprocess.PIPE, cwd = ROOT)
110 gitcrement.wait()
111 build_stamp = gitcrement.stdout.read().strip()
112 time_stamp = str(int(time.time()))
113
114 fd = open(VERSION_FILE, 'a+')
115 fd.seek(0)
116 lines = fd.readlines()
117 fd.seek(0)
118 fd.truncate()
119 for line in lines:
120 line = re.sub(r'(BUILD =) ([0-9]+)', r'\1 ' + build_stamp, line)
121 line = re.sub(r'(STAMP =) ([0-9]+)', r'\1 ' + time_stamp, line)
122 fd.write(line)
123 fd.close()
124 print ' BUILD ' + build_stamp + ' @ ' + time_stamp
125 if options.compile_fe:
126 shutil.copy(VERSION_FILE, string.replace(VERSION_FILE, '.proto', ''))
127 print ' COPY version.js.proto -> version.js'
128
129 # Compile JS.
130 print '=== Compiling Front End ==='
131 outfile = os.path.join(PROD_PATH, 'fe', PRODUCT_NAME + '.js')
132 fe_sources = map(lambda f: '-i' + os.path.join(FE_PATH, f), SOURCES_FE)
133 closure_sources = os.path.join(CLOSURE_DEST, 'closure', 'goog')
134 args = [ CLOSURE_CALCDEPS ]
135 args.extend(fe_sources)
136 output = "script"
137 if options.compile_fe:
138 output = "compiled"
139 args.extend([ '-p', closure_sources, '-o', output, '-c', CLOSURE_COMPILER,
140 '--output_file', outfile ])
141 print ' ' + ' '.join(args)
142 handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr)
143 handle.wait()
144
145 if __name__ == '__main__':
146 Main()