build.py python 2/3 compatible

This commit is contained in:
j 2014-09-30 23:19:50 +02:00
parent 2ba2903dbc
commit 1ef2f4e0d3

View file

@ -17,14 +17,14 @@ def get_version():
if os.path.exists('../../.git'):
revision = subprocess.Popen(
['git', 'rev-list', 'HEAD', '--count'], stdout=subprocess.PIPE
).communicate()[0].strip()
).communicate()[0].strip().decode('utf-8')
print(revision)
revision = int(revision) - 94
print(revision)
else:
revision = subprocess.Popen(
['bzr', 'revno'], stdout=subprocess.PIPE
).communicate()[0].strip()
).communicate()[0].strip().decode('utf-8')
return '0.1.%s' % revision
def build_oxjs(downloads=False, geo=False):
@ -62,7 +62,7 @@ def build_oxjs(downloads=False, geo=False):
theme_data[theme]['themeClass'] = 'OxTheme' + theme[0].upper() + theme[1:]
# Ox.UI CSS
css = read_file(source_path + 'UI/css/UI.css')
css = read_text(source_path + 'UI/css/UI.css')
css = css.replace('$import', '\n'.join([
'@import url("../themes/%s/css/theme.css?%s");' % (theme, version) for theme in themes
]))
@ -70,7 +70,7 @@ def build_oxjs(downloads=False, geo=False):
write_file('%sUI/css/UI.css' % min_path, css)
# Ox.UI Theme CSS
css = read_file(source_path + 'UI/css/theme.css')
css = read_text(source_path + 'UI/css/theme.css')
for theme in themes:
theme_css = parse_css(css, theme_data[theme])
theme_css = theme_css.replace('.png)', '.png?%s)' % version)
@ -81,7 +81,7 @@ def build_oxjs(downloads=False, geo=False):
ui_images = {}
path = source_path + 'UI/svg/'
for filename in [filename for filename in os.listdir(path) if not filename[0] in '._']:
svg = read_file(path + filename)
svg = read_text(path + filename)
svg = re.sub('\n\s*', '', svg)
svg = re.sub('<!--.+?-->', '', svg)
# temporary fix for Chrome SVG bug, remove later!
@ -118,7 +118,8 @@ def build_oxjs(downloads=False, geo=False):
ui_files['min'].append(target.replace(min_path, ''))
if not '/Ox/js/' in source and not '/UI/js/' in source and not is_jquery:
if re.match('^Ox\..+\.js$', filename) or is_jsonc:
js = read_file(source)
js = read_text(source)
print('minifiy and write', filename, target)
write_file(target, ox.js.minify(js, '' if is_jsonc else comment))
else:
copy_file(source, target)
@ -181,7 +182,7 @@ def build_oxjs(downloads=False, geo=False):
and not filename.endswith('~'):
filenames.append(filename)
for filename in filenames:
js += read_file(source_path + js_dir + filename) + '\n'
js += read_text(source_path + js_dir + filename) + '\n'
if not js_dir + filename in sum(ox_files, []):
ox_files[-1].append(js_dir + filename)
js = re.sub(
@ -222,9 +223,10 @@ def build_oxjs(downloads=False, geo=False):
if not '/js/' in path:
ui_files['min'].append(os.path.join(path.replace(source_path, ''), filename))
if filename.endswith('.js'):
js += read_file(os.path.join(path, filename)) + '\n'
js += read_text(os.path.join(path, filename)) + '\n'
filename = min_path + 'UI/js/UI.js'
write_file(filename, ox.js.minify(js, comment))
ui_files['min'].append(filename.replace(min_path, ''))
write_file(min_path + 'UI/json/UI.json', json.dumps({
'files': sorted(ui_files['min']),
@ -239,27 +241,24 @@ def build_oxjs(downloads=False, geo=False):
# index
data = {
# sum(list, []) is flatten
'documentation': sorted(sum(ox_files, [])) + sorted(filter(
'documentation': sorted(sum(ox_files, [])) + sorted(list(filter(
lambda x: re.search('\.js$', x),
ui_files['dev']
) + map(
lambda x: '%s/%s.js' % (x, x),
['Geo', 'Image', 'Unicode']
)),
)) + ['%s/%s.js' % (x, x) for x in ['Geo', 'Image', 'Unicode']]),
'examples': sorted(sum(map(
lambda x: filter(
lambda x: list(filter(
lambda x: not re.search('/[._]', x),
map(
lambda y: x + '/' + y,
os.listdir(root_path + 'examples/' + x)
)
),
filter(
)),
list(filter(
lambda x: not re.search('^[._]', x),
os.listdir(root_path + 'examples/')
)
))
), [])),
'readme': map(
'readme': list(map(
lambda x: {
'date': time.strftime(
'%Y-%m-%dT%H:%M:%SZ',
@ -272,7 +271,7 @@ def build_oxjs(downloads=False, geo=False):
lambda x: not re.search('^[._]', x) and re.search('\.html$', x),
os.listdir(root_path + 'readme/')
)
)
))
}
write_file(root_path + 'index.json', json.dumps(data, indent=4, sort_keys=True))
@ -332,7 +331,7 @@ def format_hex(rgb):
return '#%s' % ''.join([hex(c)[-2:].replace('x', '0').upper() for c in rgb])
def get_title(file):
match = re.search('<h1>(.+)</h1>', read_file(file))
match = re.search('<h1>(.+)</h1>', read_text(file))
return match.groups()[0] if match else 'Untitled'
def parse_css(css, values):
@ -356,18 +355,23 @@ def parse_css(css, values):
def read_file(file):
print('reading', file)
f = open(file)
f = open(file, 'rb')
data = f.read()
f.close()
return data
def read_text(file):
return read_file(file).decode('utf-8')
def read_jsonc(file):
return ox.jsonc.loads(read_file(file))
return ox.jsonc.loads(read_text(file))
def write_file(file, data):
print('writing', file)
write_path(file)
f = open(file, 'w')
if not isinstance(data, bytes):
data = data.encode('utf-8')
f = open(file, 'wb')
f.write(data)
f.close()
return len(data)