oxjs/tools/build/build.py
2011-04-25 13:19:01 +02:00

117 lines
3.9 KiB
Python
Executable file

#!/usr/bin/env python
#vim: et:ts=4:sw=4:sts=4
import json
import os
import re
import shutil
def append_file(file):
split = file.split('/')[1].split('.')
module = '.'.join([split[0], split[1]])
if not module in files:
files[module] = []
files[module].append(file)
def copy_file(source, target):
print 'copying', source, 'to', target
write_file(target, read_file(source))
def read_file(file):
print 'reading', file
f = open(file)
data = f.read()
f.close()
return data
def write_file(file, data):
print 'writing', file
write_path(file)
f = open(file, 'w')
f.write(data)
f.close()
return len(data)
def write_link(source, target):
print 'linking', source, 'to', target
write_path(target)
if os.path.exists(target):
os.unlink(target)
os.symlink(source, target)
def write_path(file):
path = os.path.split(file)[0]
if path and not os.path.exists(path):
os.makedirs(path)
# doesn't work here
base_path = os.path.dirname(__file__)
if base_path:
os.chdir(base_path)
source_path = '../../source/'
build_path = '../../build/'
files = {}
# css
for path, dirnames, filenames in os.walk(source_path + 'css/'):
for filename in filenames:
source = os.path.join(path, filename)
target = source.replace(source_path, build_path)
copy_file(source, target)
append_file(target.replace(build_path, ''))
# js
filename = 'js/Ox.js'
write_link(source_path + filename, build_path + filename)
root = source_path + 'js/'
for path, dirnames, filenames in os.walk(root):
for dirname in dirnames:
if dirname[0] != '_':
if path == root and dirname != 'jquery':
source = os.path.join(path, dirname)
target = source.replace(source_path, build_path)
write_link(source, target)
for filename in filenames:
if filename[0] != '.' and filename[0] != '_':
if 'jquery' in path:
is_jquery = re.compile('jquery-[\d\.]+\.js').findall(filename)
is_jquery_min = re.compile('jquery-[\d\.]+\.min\.js').findall(filename)
source = os.path.join(path, filename)
if is_jquery:
target = os.path.join(path.replace(source_path, build_path), 'jquery.js')
elif is_jquery_min:
target = os.path.join(path.replace(source_path, build_path), 'jquery.min.js')
else:
target = source.replace(source_path, build_path)
copy_file(source, target)
if not is_jquery and not is_jquery_min:
files['Ox.UI'].append(target.replace(build_path, ''))
elif path != root and not '_' in path:
append_file(os.path.join(path, filename).replace(source_path, ''))
# png
for path, dirnames, filenames in os.walk(source_path + 'png/'):
for filename in filenames:
if filename[:1] != '.':
source = os.path.join(path, filename)
target = source.replace(source_path, build_path)
copy_file(source, target)
append_file(target.replace(build_path, ''))
# svg
for path, dirnames, filenames in os.walk(source_path + 'svg/'):
for filename in filenames:
if filename[0] != '.' and filename[0] != '_':
source = os.path.join(path, filename)
target = source.replace(source_path, build_path)
copy_file(source, target)
append_file(target.replace(build_path, ''))
if 'Ox.UI.classic' in source:
svg = read_file(source).replace('#404040', '#FFFFFF').replace('#000000', '#FFFFFF')
target = target.replace('Ox.UI.classic', 'Ox.UI.modern')
write_file(target, svg)
append_file(target.replace(build_path, ''))
for module in files:
file = build_path + 'json/' + module + '.json'
write_file(file, json.dumps(sorted(files[module]), indent=4, sort_keys=True))