box-o-sand/mkbuildxml.py

183 lines
5.6 KiB
Python
Raw Normal View History

2010-02-25 03:23:19 +00:00
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import sys
import glob
from io import BytesIO
from fnmatch import fnmatch
from itertools import ifilter
from os.path import join as pathjoin, relpath, dirname, \
abspath, basename, splitext, sep as pathsep
import lxml.etree as ET
HERE = dirname(abspath(__file__))
AUTOGEN_WARNING = ('<!-- ******** AUTOGENERATED by {0} ' +
('*' * 20) + '-->').format(basename(sys.argv[0]))
ROOT = BytesIO(''.join([_line.strip() for _line in """\
<project name="LearningFlex" basedir="." default="build">
<property environment="env" />
<property name="FLEX_HOME" value="${env.FLEX_HOME}" />
<property name="APP_ROOT" value="${basedir}" />
<taskdef resource="flexTasks.tasks" """
"""classpath="${env.FLEX_HOME}/ant/lib/flexTasks.jar" />
<target name="build" />
</project>
""".splitlines() if _line.strip()]))
# TARGET_FMT = '{target}:' + DEP_SEP + '{deps}'
# RULE_FMT = '\\\n\t'.join([
# '\t$(MXMLC) {primary_dep} ',
# '$(MXMLC_FLAGS) ',
# '-l+={parent_dir} ',
# '-l+={package_base} ',
# '-l+={flexunit} ',
# '-sp+={package_base} ',
# '-sp+={flexunit} ',
# '-o {target}',
# ])
def main(sysargs=sys.argv[:]):
build_xml = pathjoin(HERE, 'build.xml')
os.chmod(build_xml, 0600)
buildxml_maker = BuildXMLMaker(outstream=open(build_xml, 'wb'))
buildxml_maker.make()
os.chmod(build_xml, 0444)
return 0
class BuildXMLMaker(object):
_to_glob = (
pathjoin(HERE, '[0-9]*-*', '*.mxml'),
pathjoin(HERE, 'custom-*', '*.mxml'),
)
_source_fileexts = ('.mxml', '.as', '.css')
def __init__(self, outstream=sys.stdout):
self.outstream = outstream
self.targets = {'compc': {}, 'mxmlc': {}}
self.tree = ET.parse(ROOT)
self.root = self.tree.getroot()
self.build = self.tree.xpath('//target[@name="build"]')[0]
def make(self):
self._glob_for_top_level_mxml_targets()
self._find_test_suite_targets()
self._write_out_targets()
def _glob_for_top_level_mxml_targets(self):
for pattern in self._to_glob:
for mxml in glob.glob(pattern):
self._add_target_from_mxml(mxml)
def _find_test_suite_targets(self):
for dirpath, dirnames, filenames in os.walk(HERE):
filter_generated_dirs(dirnames)
for filename in filenames:
if is_test_suite(filename):
test_suite = pathjoin(dirpath, filename)
self._add_target_from_test_suite(test_suite)
def _add_target_from_test_suite(self, test_suite):
rel_class = relpath(test_suite, HERE)
self.targets['compc'][rel_class] = dict()
def _add_target_from_mxml(self, mxml):
rel_mxml = relpath(mxml, HERE)
self.targets['mxmlc'][rel_mxml] = {} #dict(sources='')
self._add_deps_from_alt_source_file_extensions(rel_mxml)
def _add_deps_from_alt_source_file_extensions(self, rel_mxml):
basedir = dirname(rel_mxml)
# sources = self.targets['mxmlc'][rel_mxml]['sources']
for dirpath, dirnames, filenames in os.walk(basedir):
filter_generated_dirs(dirnames)
filter_test_suitees(filenames)
for filename in filenames:
if ext(filename) in self._source_fileexts:
rel_source = relpath(pathjoin(dirpath, filename), HERE)
# sources += (' ' + rel_source)
def _write_out_targets(self):
for mxml in sorted(self.targets['mxmlc'].iterkeys()):
rel_target = './' + mxml.lstrip('./')
attrs = dict(file=rel_target, as3='true')
attrs.update(self.targets['mxmlc'][mxml])
ET.SubElement(self.build, 'mxmlc', **attrs)
for as3 in sorted(self.targets['compc'].iterkeys()):
rel_target = './' + as3.lstrip('./')
as_swf = re.sub('(.*).as$', '\\1.swf', rel_target)
2010-02-25 03:27:20 +00:00
attrs = dict(output=as_swf)
2010-02-25 03:23:19 +00:00
attrs.update(self.targets['compc'][as3])
ET.SubElement(self.build, 'compc', **attrs)
print(ET.tostring(self.root, pretty_print=True),
file=self.outstream)
def _get_deps_for_target(self, target):
deps = []
for dep in reversed(sorted(list(self.targets[target]), key=ext)):
deps.append(dep)
return deps
def _get_primary_dep(self, target, deps):
primary_dep = deps[0]
for dependency in deps:
if fnmatch(target, namebase(dependency) + '.*'):
primary_dep = dependency
return primary_dep
def _get_pattern_rule_kwargs(self, primary_dep, target):
ret = dict(
primary_dep=primary_dep,
parent_dir=dirname(primary_dep),
package_base=primary_dep.strip('./').split(pathsep)[0],
target=target,
flexunit=FLEXUNIT
)
return ret
def ext(filename):
return splitext(filename)[-1]
def namebase(filename):
return splitext(filename)[0]
def filter_generated_dirs(dirnames):
for i, directory in enumerate(dirnames[:]):
if directory == 'generated':
dirnames[i] = None
dirnames[:] = list(ifilter(None, dirnames))
def filter_test_suitees(filenames):
for i, filename in enumerate(filenames[:]):
if is_test_suite(filename):
filenames[i] = None
filenames[:] = list(ifilter(None, filenames))
def is_test_suite(filename):
base_filename = basename(filename)
return base_filename.startswith('TestSuite') and \
ext(filename) == '.as'
if __name__ == '__main__':
sys.exit(main())
# vim:filetype=python