| 1 |
|
| 2 |
import sys, os, shutil, re |
| 3 |
from optparse import OptionParser |
| 4 |
PROG_NAME = os.path.basename(os.path.abspath(sys.argv[0])) |
| 5 |
VERBOSE = False |
| 6 |
def debug(msg): |
| 7 |
if VERBOSE: |
| 8 |
sys.stderr.write('%s: %s\n' % (PROG_NAME, msg)) |
| 9 |
parser = OptionParser() |
| 10 |
parser.add_option('-p', '--package', dest='package', |
| 11 |
help='The package that contains the Plugin impl') |
| 12 |
parser.add_option('-c', '--class', dest='classname', |
| 13 |
help='The name of the of class that implements Plugin') |
| 14 |
parser.add_option('-x', '--xmlelement', dest='xml', default='', type='str', |
| 15 |
help='The name of the xml element associated with the parser') |
| 16 |
parser.add_option('-v', '--verbose', dest='verbose', default=False, |
| 17 |
action='store_true', |
| 18 |
help='verbose mode') |
| 19 |
|
| 20 |
|
| 21 |
(options, args) = parser.parse_args() |
| 22 |
if options.verbose: |
| 23 |
VERBOSE = True |
| 24 |
if not options.package: |
| 25 |
sys.exit('The package options must be specified') |
| 26 |
if not options.classname: |
| 27 |
sys.exit('The class options must be specified') |
| 28 |
|
| 29 |
dir = os.path.dirname(os.path.abspath(sys.argv[0])) |
| 30 |
template_par = os.path.join(dir, 'templates') |
| 31 |
if not os.path.exists(template_par): |
| 32 |
sys.exit('%s does not exist' % template_par) |
| 33 |
|
| 34 |
sp_package = options.package.split('.') |
| 35 |
|
| 36 |
def copy_with_subst(s, d, rep_dict): |
| 37 |
try: |
| 38 |
source = open(s, 'rU') |
| 39 |
except: |
| 40 |
sys.exit('Could not open %s ' % s) |
| 41 |
try: |
| 42 |
dest = open(d, 'w') |
| 43 |
except: |
| 44 |
sys.exit('Could not open %s ' % d) |
| 45 |
pat_sub_list = [(re.compile(r'@%s@' % k), v) for k, v in rep_dict.iteritems()] |
| 46 |
for line in source: |
| 47 |
for p_el in pat_sub_list: |
| 48 |
line = p_el[1].join(p_el[0].split(line)) |
| 49 |
dest.write(line) |
| 50 |
dest.close() |
| 51 |
source.close() |
| 52 |
replace_dict = {} |
| 53 |
replace_dict['PLUGIN_CLASS'] = options.classname |
| 54 |
replace_dict['PLUGIN_PACKAGE'] = sp_package[0] |
| 55 |
replace_dict['PLUGIN_SUBPACKAGES'] = '.'.join([''] + sp_package[1:] + ['']) |
| 56 |
replace_dict['PLUGIN_FULL_PACKAGE'] = options.package |
| 57 |
replace_dict['PLUGIN_SRC_PATH'] = ''' <include name="${plugin-package}/**/*.java"/>''' |
| 58 |
replace_dict['PLUGIN_CLASS_FILE_PATH'] = ''' <include name="${plugin-package}/**/*.class"/>''' |
| 59 |
replace_dict['EXAMPLE_XML_FILE'] = 'test%s.xml' % options.classname |
| 60 |
if options.xml: |
| 61 |
xml_el = options.xml |
| 62 |
elif options.classname.lower().endswith('plugin'): |
| 63 |
xml_el = options.classname[:len('plugin') - 2] |
| 64 |
else: |
| 65 |
xml_el = options.classname |
| 66 |
|
| 67 |
replace_dict['PLUGIN_XML_ELEMENT'] = xml_el |
| 68 |
|
| 69 |
replace_dict['PLUGIN_IMPORTS'] = '''import dr.evomodel.substmodel.NucModelType; |
| 70 |
import dr.evomodel.substmodel.FrequencyModel; |
| 71 |
import dr.evomodel.substmodel.HKY; |
| 72 |
import dr.inference.model.Parameter; |
| 73 |
import dr.inference.model.Variable;''' |
| 74 |
replace_dict['PLUGIN_XML_SYNTAX_RULES'] = '''new ElementRule(FrequencyModel.FREQUENCIES, |
| 75 |
new XMLSyntaxRule[]{new ElementRule(FrequencyModel.class)}), |
| 76 |
new ElementRule("kappa", |
| 77 |
new XMLSyntaxRule[]{new ElementRule(Variable.class)})''' |
| 78 |
replace_dict['PLUGIN_XML_PARSER_STUB'] = '''Variable kappaParam = (Variable) xo.getElementFirstChild("kappa"); |
| 79 |
FrequencyModel freqModel = (FrequencyModel) xo.getElementFirstChild(FrequencyModel.FREQUENCIES); |
| 80 |
|
| 81 |
Logger.getLogger("dr.evomodel").info("Creating THMM substitution model. Initial kappa = " + |
| 82 |
kappaParam.getValue(0)); |
| 83 |
|
| 84 |
return new HKY(kappaParam, freqModel);''' |
| 85 |
replace_dict['PLUGIN_PARSER_RETURN_TYPE'] = 'HKY.class' |
| 86 |
parser_name = 'DummyModelParser' |
| 87 |
|
| 88 |
debug('\n '.join(['%s = %s' % (k, v) for k, v in replace_dict.iteritems()])) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
dest_dir = options.classname |
| 93 |
if os.path.exists(dest_dir): |
| 94 |
sys.exit('%s already exsists' % dest_dir) |
| 95 |
dir_list = [dest_dir, 'src'] + sp_package |
| 96 |
dest_src_dir = os.path.join(*dir_list) |
| 97 |
try: |
| 98 |
os.makedirs(dest_src_dir) |
| 99 |
except: |
| 100 |
sys.exit('Could not create %s' % dest_src_dir) |
| 101 |
dest_example_dir = os.path.join(dest_dir, 'example') |
| 102 |
try: |
| 103 |
os.makedirs(dest_example_dir) |
| 104 |
except: |
| 105 |
sys.exit('Could not create %s' % dest_example_dir) |
| 106 |
|
| 107 |
plugin_src = os.path.join(template_par, 'DummyPlugin.java') |
| 108 |
dest_src = os.path.join(dest_src_dir, '%s.java' % options.classname) |
| 109 |
copy_with_subst(plugin_src, dest_src, replace_dict) |
| 110 |
|
| 111 |
plugin_src = os.path.join(template_par, '%s.java' % parser_name) |
| 112 |
dest_src = os.path.join(dest_src_dir, '%sParser.java' % options.classname) |
| 113 |
copy_with_subst(plugin_src, dest_src, replace_dict) |
| 114 |
|
| 115 |
plugin_src = os.path.join(template_par, 'build.xml') |
| 116 |
dest_src = os.path.join(dest_dir, 'build.xml') |
| 117 |
copy_with_subst(plugin_src, dest_src, replace_dict) |
| 118 |
|
| 119 |
plugin_src = os.path.join(template_par, 'beast_sdk.properties.in') |
| 120 |
dest_src = os.path.join(dest_dir, 'beast_sdk.properties.in') |
| 121 |
copy_with_subst(plugin_src, dest_src, replace_dict) |
| 122 |
|
| 123 |
plugin_src = os.path.join(template_par, 'Dummy.xml') |
| 124 |
dest_src = os.path.join(dest_example_dir, 'test%s.xml' % options.classname) |
| 125 |
copy_with_subst(plugin_src, dest_src, replace_dict) |
| 126 |
|