oxjs/tools/build/build.py

118 lines
3.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#vim: et:ts=4:sw=4:sts=4
import json
2010-09-05 14:24:22 +00:00
import os
2011-04-22 22:03:10 +00:00
import re
2011-04-25 09:12:02 +00:00
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):
2011-04-25 09:12:02 +00:00
print 'reading', file
f = open(file)
data = f.read()
f.close()
return data
def write_file(file, data):
2011-04-25 09:12:02 +00:00
print 'writing', file
write_path(file)
f = open(file, 'w')
f.write(data)
f.close()
return len(data)
2011-04-22 22:03:10 +00:00
def write_link(source, target):
2011-04-25 09:12:02 +00:00
print 'linking', source, 'to', target
write_path(target)
2011-04-22 22:03:10 +00:00
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)
2011-04-25 09:12:02 +00:00
# doesn't work here
2011-04-25 11:19:01 +00:00
base_path = os.path.dirname(__file__)
if base_path:
os.chdir(base_path)
2011-04-22 22:03:10 +00:00
source_path = '../../source/'
build_path = '../../build/'
2011-04-25 09:12:02 +00:00
files = {}
2011-04-25 09:12:02 +00:00
# css
for path, dirnames, filenames in os.walk(source_path + 'css/'):
for filename in filenames:
2011-04-25 09:12:02 +00:00
source = os.path.join(path, filename)
target = source.replace(source_path, build_path)
copy_file(source, target)
append_file(target.replace(build_path, ''))
2011-04-25 09:12:02 +00:00
# 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)
2011-04-22 22:03:10 +00:00
for filename in filenames:
2011-04-25 09:12:02 +00:00
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, ''))
2011-04-23 11:04:37 +00:00
2011-04-25 09:12:02 +00:00
# png
for path, dirnames, filenames in os.walk(source_path + 'png/'):
for filename in filenames:
if filename[:1] != '.':
2011-04-25 09:12:02 +00:00
source = os.path.join(path, filename)
target = source.replace(source_path, build_path)
copy_file(source, target)
append_file(target.replace(build_path, ''))
2011-04-25 09:12:02 +00:00
# svg
for path, dirnames, filenames in os.walk(source_path + 'svg/'):
2010-09-05 14:24:22 +00:00
for filename in filenames:
2011-04-25 09:12:02 +00:00
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, ''))
2010-09-05 14:24:22 +00:00
2011-04-25 09:12:02 +00:00
for module in files:
file = build_path + 'json/' + module + '.json'
write_file(file, json.dumps(sorted(files[module]), indent=4, sort_keys=True))