oxjs/tools/build/build.py
2011-08-09 19:00:39 +02:00

109 lines
No EOL
4 KiB
Python
Executable file

#!/usr/bin/env python
#vim: et:ts=4:sw=4:sts=4
import base64
import json
import os
import re
import shutil
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)
base_path = os.path.dirname(__file__)
if base_path:
os.chdir(base_path)
source_path = '../../source/'
build_path = '../../build/'
files = ['Ox.UI/css/Ox.UI.css']
# SVGs
path = source_path + 'Ox.UI/themes/classic/svg/'
for filename in os.listdir(path):
svg = read_file(path + filename)
svg = svg.replace('#404040', '#FFFFFF').replace('#000000', '#FFFFFF')
write_file(path.replace('/classic/', '/modern/') + filename, svg)
imageURLs = {}
imageDataURLs = {}
for theme in ['classic', 'modern']:
path = source_path + 'Ox.UI/themes/' + theme + '/svg/'
for filename in os.listdir(path):
if not filename[0] in '._':
key = theme + '/' + filename[:-4]
imageURLs[key] = os.path.join(path, filename).replace(source_path, '')
data = re.sub('\n\s+', '', read_file(path + filename))
imageDataURLs[key] = 'data:image/svg+xml;base64,' + base64.b64encode(data)
write_file(build_path + 'Ox.UI/json/Ox.UI.imageURLs.json', json.dumps(imageURLs, indent=4, sort_keys=True))
write_file(build_path + 'Ox.UI/json/Ox.UI.imageDataURLs.json', json.dumps(imageDataURLs, indent=4, sort_keys=True))
# symlinks
for path, dirnames, filenames in os.walk(source_path):
for filename in filenames:
if not '_' in path and not filename[0] in '._':
parts = os.path.join(path.replace(source_path, ''), filename).split('/')
for i, part in enumerate(parts):
if i < len(parts) - 1:
parts[i] = '..'
source = '/'.join(parts).replace(filename, os.path.join(path, filename))[3:]
is_jquery = re.compile('^jquery-[\d\.]+\.js$').findall(filename)
is_jquery_min = re.compile('^jquery-[\d\.]+\.min\.js$').findall(filename)
is_jquery_plugin = re.compile('^jquery\..*?\.js$').findall(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 = os.path.join(path.replace(source_path, build_path), filename)
if is_jquery_plugin:
files.append(target.replace(build_path, ''))
write_link(source, target)
# Ox.UI
js = ''
section = ''
root = source_path + 'Ox.UI/'
for path, dirnames, filenames in os.walk(root):
for filename in filenames:
# theme css files get included by main css
if path != root and not '_' in path and not filename[0] in '._' and filename[-4:] != '.css' and filename[-4:] != '.svg':
files.append(os.path.join(path.replace(source_path, ''), filename))
if filename[-3:] == '.js':
folder = os.path.split(path)[-1]
if section != folder:
section = folder
js += '/*\n' + '=' * 80 + '\n' + section + '\n' + '=' * 80 + '\n*/\n\n'
js += '/*\n' + '-' * 80 + '\n' + filename[:-3] + '\n' + '-' * 80 + '\n*/\n\n'
js += read_file(os.path.join(path, filename)) + '\n\n'
write_file(build_path + 'Ox.UI/js/Ox.UI.js', js)
files = json.dumps(sorted(files), indent=4, sort_keys=True)
write_file(build_path + 'Ox.UI/json/Ox.UI.files.json', files)