2010-02-21 05:08:12 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import glob
|
2010-02-23 03:11:44 +00:00
|
|
|
from fnmatch import fnmatch
|
2010-02-24 01:58:18 +00:00
|
|
|
from itertools import ifilter
|
2010-02-23 02:49:18 +00:00
|
|
|
from os.path import join as pathjoin, relpath, dirname, \
|
2010-02-24 01:58:18 +00:00
|
|
|
abspath, basename, splitext, sep as pathsep
|
2010-02-21 05:08:12 +00:00
|
|
|
|
|
|
|
|
2010-02-23 02:49:18 +00:00
|
|
|
HERE = dirname(abspath(__file__))
|
2010-02-25 02:03:28 +00:00
|
|
|
FLEXUNIT = pathjoin('$(HOME)', '.local', 'share', 'flexunit')
|
2010-02-21 05:08:12 +00:00
|
|
|
AUTOGEN_WARNING = ('# ******** AUTOGENERATED by {0} ' +
|
2010-02-23 02:49:18 +00:00
|
|
|
('*' * 20)).format(basename(sys.argv[0]))
|
2010-02-24 01:58:18 +00:00
|
|
|
DEP_SEP = ' \\\n '
|
|
|
|
RULE_SEP = '\n\n'
|
|
|
|
TARGET_FMT = '{target}:' + DEP_SEP + '{deps}'
|
|
|
|
RULE_FMT = '\\\n\t'.join([
|
|
|
|
'\t$(MXMLC) {primary_dep} ',
|
2010-02-25 01:20:19 +00:00
|
|
|
'$(MXMLC_FLAGS) ',
|
|
|
|
'-l+={parent_dir} ',
|
|
|
|
'-l+={package_base} ',
|
2010-02-25 02:03:28 +00:00
|
|
|
'-l+={flexunit} ',
|
2010-02-25 01:20:19 +00:00
|
|
|
'-sp+={package_base} ',
|
2010-02-25 02:03:28 +00:00
|
|
|
'-sp+={flexunit} ',
|
2010-02-25 01:20:19 +00:00
|
|
|
'-o {target}',
|
2010-02-24 01:58:18 +00:00
|
|
|
])
|
2010-02-21 05:08:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(sysargs=sys.argv[:]):
|
|
|
|
targets_mk = pathjoin(HERE, 'targets.mk')
|
|
|
|
os.chmod(targets_mk, 0600)
|
|
|
|
targetmaker = TargetMaker(outstream=open(targets_mk, 'wb'))
|
|
|
|
targetmaker.make()
|
|
|
|
os.chmod(targets_mk, 0444)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
class TargetMaker(object):
|
|
|
|
_to_glob = (
|
|
|
|
pathjoin(HERE, '[0-9]*-*', '*.mxml'),
|
|
|
|
pathjoin(HERE, 'custom-*', '*.mxml'),
|
|
|
|
)
|
2010-02-23 02:49:18 +00:00
|
|
|
_source_fileexts = ('.mxml', '.as', '.css')
|
2010-02-21 05:08:12 +00:00
|
|
|
|
|
|
|
def __init__(self, outstream=sys.stdout):
|
|
|
|
self.outstream = outstream
|
|
|
|
self.targets = {}
|
|
|
|
|
|
|
|
def make(self):
|
2010-02-24 01:58:18 +00:00
|
|
|
self._glob_for_top_level_mxml_targets()
|
2010-02-25 02:03:28 +00:00
|
|
|
self._find_test_suite_targets()
|
2010-02-24 01:58:18 +00:00
|
|
|
self._write_out_targets()
|
|
|
|
|
|
|
|
def _glob_for_top_level_mxml_targets(self):
|
2010-02-21 05:08:12 +00:00
|
|
|
for pattern in self._to_glob:
|
|
|
|
for mxml in glob.glob(pattern):
|
|
|
|
self._add_target_from_mxml(mxml)
|
2010-02-24 01:58:18 +00:00
|
|
|
|
2010-02-25 02:03:28 +00:00
|
|
|
def _find_test_suite_targets(self):
|
2010-02-24 01:58:18 +00:00
|
|
|
for dirpath, dirnames, filenames in os.walk(HERE):
|
|
|
|
filter_generated_dirs(dirnames)
|
|
|
|
for filename in filenames:
|
2010-02-25 02:03:28 +00:00
|
|
|
if is_test_suite(filename):
|
|
|
|
test_suite = pathjoin(dirpath, filename)
|
|
|
|
self._add_target_from_test_suite(test_suite)
|
2010-02-24 01:58:18 +00:00
|
|
|
|
2010-02-25 02:03:28 +00:00
|
|
|
def _add_target_from_test_suite(self, test_suite):
|
|
|
|
rel_class = relpath(test_suite, HERE)
|
2010-02-24 01:58:18 +00:00
|
|
|
as_swf = re.sub('(.*)\.as$', '\\1.swf', rel_class)
|
|
|
|
self.targets[as_swf] = set([rel_class])
|
2010-02-21 05:08:12 +00:00
|
|
|
|
|
|
|
def _add_target_from_mxml(self, mxml):
|
2010-02-23 02:49:18 +00:00
|
|
|
rel_mxml = relpath(mxml, HERE)
|
2010-02-21 05:08:12 +00:00
|
|
|
as_swf = re.sub('(.*)\.mxml$', '\\1.swf', rel_mxml)
|
|
|
|
self.targets[as_swf] = set([rel_mxml])
|
2010-02-23 02:49:18 +00:00
|
|
|
self._add_deps_from_alt_source_file_extensions(as_swf, rel_mxml)
|
2010-02-21 21:10:56 +00:00
|
|
|
|
2010-02-23 02:49:18 +00:00
|
|
|
def _add_deps_from_alt_source_file_extensions(self, as_swf, rel_mxml):
|
|
|
|
basedir = dirname(rel_mxml)
|
|
|
|
for dirpath, dirnames, filenames in os.walk(basedir):
|
2010-02-24 01:58:18 +00:00
|
|
|
filter_generated_dirs(dirnames)
|
2010-02-25 02:03:28 +00:00
|
|
|
filter_test_suitees(filenames)
|
2010-02-23 02:49:18 +00:00
|
|
|
for filename in filenames:
|
2010-02-23 03:11:44 +00:00
|
|
|
if ext(filename) in self._source_fileexts:
|
2010-02-23 02:49:18 +00:00
|
|
|
rel_source = relpath(pathjoin(dirpath, filename), HERE)
|
|
|
|
self.targets[as_swf].add(rel_source)
|
2010-02-21 05:08:12 +00:00
|
|
|
|
|
|
|
def _write_out_targets(self):
|
|
|
|
print(AUTOGEN_WARNING, file=self.outstream)
|
|
|
|
for target in sorted(self.targets.iterkeys()):
|
2010-02-23 03:11:44 +00:00
|
|
|
deps = self._get_deps_for_target(target)
|
|
|
|
primary_dep = self._get_primary_dep(target, deps)
|
|
|
|
pattern_rule_kwargs = \
|
|
|
|
self._get_pattern_rule_kwargs(primary_dep, target)
|
|
|
|
|
2010-02-25 01:20:19 +00:00
|
|
|
rel_target = './' + target.lstrip('./')
|
|
|
|
print(TARGET_FMT.format(target=rel_target,
|
2010-02-25 02:03:28 +00:00
|
|
|
deps=DEP_SEP.join(deps),
|
|
|
|
), file=self.outstream
|
|
|
|
)
|
2010-02-24 01:58:18 +00:00
|
|
|
print(RULE_FMT.format(**pattern_rule_kwargs) +
|
2010-02-25 02:03:28 +00:00
|
|
|
RULE_SEP, file=self.outstream
|
|
|
|
)
|
2010-02-23 03:11:44 +00:00
|
|
|
|
|
|
|
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),
|
2010-02-24 01:58:18 +00:00
|
|
|
package_base=primary_dep.strip('./').split(pathsep)[0],
|
2010-02-25 02:03:28 +00:00
|
|
|
target=target,
|
|
|
|
flexunit=FLEXUNIT
|
2010-02-23 03:11:44 +00:00
|
|
|
)
|
|
|
|
return ret
|
|
|
|
|
2010-02-24 01:58:18 +00:00
|
|
|
|
2010-02-23 03:11:44 +00:00
|
|
|
def ext(filename):
|
|
|
|
return splitext(filename)[-1]
|
|
|
|
|
|
|
|
|
|
|
|
def namebase(filename):
|
|
|
|
return splitext(filename)[0]
|
2010-02-21 05:08:12 +00:00
|
|
|
|
|
|
|
|
2010-02-24 01:58:18 +00:00
|
|
|
def filter_generated_dirs(dirnames):
|
|
|
|
for i, directory in enumerate(dirnames[:]):
|
|
|
|
if directory == 'generated':
|
|
|
|
dirnames[i] = None
|
|
|
|
dirnames[:] = list(ifilter(None, dirnames))
|
|
|
|
|
|
|
|
|
2010-02-25 02:03:28 +00:00
|
|
|
def filter_test_suitees(filenames):
|
2010-02-24 01:58:18 +00:00
|
|
|
for i, filename in enumerate(filenames[:]):
|
2010-02-25 02:03:28 +00:00
|
|
|
if is_test_suite(filename):
|
2010-02-24 01:58:18 +00:00
|
|
|
filenames[i] = None
|
|
|
|
filenames[:] = list(ifilter(None, filenames))
|
|
|
|
|
|
|
|
|
2010-02-25 02:03:28 +00:00
|
|
|
def is_test_suite(filename):
|
2010-02-24 01:58:18 +00:00
|
|
|
base_filename = basename(filename)
|
2010-02-25 02:03:28 +00:00
|
|
|
return base_filename.startswith('TestSuite') and \
|
2010-02-24 01:58:18 +00:00
|
|
|
ext(filename) == '.as'
|
|
|
|
|
|
|
|
|
2010-02-21 05:08:12 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|
|
|
|
|
|
|
|
# vim:filetype=python
|