Created
August 17, 2016 13:06
-
-
Save cwshu/91fdf28853882a1fd6d3a3467a6d5a93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
import sys | |
import subprocess as sp | |
import os | |
# Please modify EFLOW_BASE_DIR to eflow repository root dir if you move this script to other directory. | |
EFLOW_BASE_DIR = ".." | |
# path | |
RUNTIME_LIB = '{}/SourceCodeTransformation/TraceFramework/bin/'.format(EFLOW_BASE_DIR) | |
DEP_LIB = '{}/SourceCodeTransformation/lib/*'.format(EFLOW_BASE_DIR) | |
DEP_BASE_LIB = DEP_LIB + ":$CLASSPATH" | |
# program | |
JAVA_FETCH = 'java -cp "{base_dir}/SourceCodeTransformation/Java_Fetch/bin:{dep_base_lib}" MainModule'.format( | |
base_dir=EFLOW_BASE_DIR, dep_base_lib=DEP_BASE_LIB) | |
JAVA_TRANSLATE = 'java -cp "{base_dir}/SourceCodeTransformation/Java_Translate/bin:{dep_base_lib}" MainModule'.format( | |
base_dir=EFLOW_BASE_DIR, dep_base_lib=DEP_BASE_LIB) | |
# MSGPACK_PARSER = 'python2 (base_dir)/FlowGraphModel/ParseMsgLog/ParseMsgpackLog.py'.format(base_dir=EFLOW_BASE_DIR) | |
def concat_java_cp(*args): | |
return ':'.join(args) | |
def print_usage_and_exit(_help=False): | |
print 'usage:' | |
print ' {} translate <input_java_source> [<java_dest>]'.format(sys.argv[0]) | |
print ' {} compile <input_java_source> <dest_dir>'.format(sys.argv[0]) | |
print ' {} run <java_module_name> <module_class_path>'.format(sys.argv[0]) | |
# print ' {} translate_multi <dest_dir> <src1> <src2> ... '.format(sys.argv[0]) | |
print ' {} --help for more detail'.format(sys.argv[0]) | |
EXAMPLE_USAGE = '\nexample usage: \n' + \ | |
' ./eFlow.py translate ../SourceCodeTransformation/test/hello.java \n' + \ | |
' ./eFlow.py compile ../SourceCodeTransformation/test/hello_trans.java . \n' + \ | |
' ./eFlow.py run hello_trans . \n' | |
if _help: | |
print EXAMPLE_USAGE | |
sys.exit() | |
def fetch(java_src): | |
sp.call('cat {} | {}'.format(java_src, JAVA_FETCH), shell=True) | |
def translate(java_src, java_dst=None): | |
if not java_dst: | |
# {java_src_name}.java => {java_src_name}Trans.java | |
java_dst = os.path.splitext(java_src)[0] + 'Trans.java' | |
java_src_name = os.path.basename(java_src) | |
java_src_name = os.path.splitext(java_src_name)[0] | |
java_dst_name = java_src_name + 'Trans' | |
else: | |
# {java_src_name}.java => {java_dst_name}.java | |
java_src_name = os.path.basename(java_src) | |
java_src_name = os.path.splitext(java_src_name)[0] | |
java_dst_name = os.path.basename(java_dst) | |
java_dst_name = os.path.splitext(java_dst_name)[0] | |
sp.call('cat {} | {} > {}'.format(java_src, JAVA_TRANSLATE, java_dst), shell=True) | |
# \<word\> mean really march word, excludes dword, wordpress, ... etc . | |
sp.call("sed -i.bak 's/\<{}\>/{}/g' {}".format(java_src_name, java_dst_name, java_dst), shell=True) | |
def main(): | |
if len(sys.argv) not in (2, 3, 4): | |
print_usage_and_exit() | |
if sys.argv[1] == '--help': | |
print_usage_and_exit(_help=True) | |
if sys.argv[1] in ('fetch', 'translate') and len(sys.argv) not in (3, 4): | |
print_usage_and_exit() | |
if sys.argv[1] in ('compile', 'run') and len(sys.argv) != 4: | |
print_usage_and_exit() | |
java_src = sys.argv[2] | |
if sys.argv[1] == 'fetch': | |
fetch(java_src) | |
if sys.argv[1] == 'translate': | |
fetch(java_src) | |
if len(sys.argv) == 4: | |
java_dst = sys.argv[3] | |
translate(java_src, java_dst) | |
else: | |
translate(java_src) | |
if sys.argv[1] == 'compile': | |
byte_code_dest_dir = sys.argv[3] | |
sp.call('javac -d {} -cp {} {}'.format( | |
byte_code_dest_dir, concat_java_cp(RUNTIME_LIB, DEP_BASE_LIB), java_src), | |
shell=True) | |
if sys.argv[1] == 'run': | |
java_module_path = sys.argv[2] | |
module_class_path = sys.argv[3] | |
sp.call('java -cp {} {}'.format( | |
concat_java_cp(module_class_path, RUNTIME_LIB, DEP_BASE_LIB), java_module_path), | |
shell=True) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment