'''
Created on Feb 22, 2013
@author: changxue
@summary: aim: generate jar file
path: 1. copy original jar to target dir
2. java to class, class to jar to target dir
'''
import os, shutil
from common_util import _printlog, _exclude, _add_files_to_list, _execute_cmd
def copy_original_jar(src_path, jar_dir):
_printlog('############## copy jar starting ##############\n')
jar_list = []
os.chdir(src_path)
file_list = os.listdir(src_path)
for f in file_list:
archive_file_path = os.path.join(src_path, f)
try:
if not os.path.isfile(archive_file_path):
raise Exception('%s is not a file.\n'%f)
else:
if f.endswith('.jar'):
jar_file = os.path.join(jar_dir, f)
if os.path.exists(jar_file):
raise Exception('WARNING: File[%s] already exists.\n' % jar_file)
else:
shutil.copy2(archive_file_path, jar_dir)
_printlog('Copy file[%s] to %s\n'% archive_file_path, jar_dir)
jar_list.append(f)
except Exception, e:
_printlog(e)
continue
_printlog('total_count=%s\n'%len(file_list))
_printlog('success_count=%s\n'%len(jar_list))
_printlog('success_list=\n%s'%'\n'.join(jar_list))
_printlog('############## jar_list end ##############\n')
def generate_class_all(src_path, target_dir):
''' make all java files in each sub-dir in src_path into .class files which is placed in target_dir.'''
_printlog('############## Generate class starting ##############\n')
success_list = []
fail_list = []
operate_list = _exclude(src_path)
for dirname in operate_list:
try:
full_path = os.path.join(src_path, dirname)
java_file_list = _add_files_to_list(full_path, '.*\.java$')
if not java_file_list:
_printlog("WARNING: no java file in %s\n"%(full_path))
else:
dest_full_path = os.path.join(target_dir, dirname)
if os.path.exists(dest_full_path):
_printlog('WARNING: directory already exist: %s\n'%dest_full_path)
else:
os.makedirs(dest_full_path)
cmd = 'javac -d %s %s' % (dest_full_path, ' '.join(java_file_list)) # would fail if the list is too large, could instead of *.java
_execute_cmd(cmd)
success_list.append(dirname)
except Exception, e:
_printlog("WARNING ON: make class file: %s\n%s\n"%(full_path, e))
fail_list.append(dirname)
continue
_printlog('total_count=%s\n'%len(operate_list))
_printlog('success_count=%s\n'%len(success_list))
_printlog('success_list=\n%s'%'\n'.join(success_list))
_printlog('fail_count=%s\n'%len(fail_list))
_printlog('fail_list=\n%s'%'\n'.join(fail_list))
_printlog('############## Generate class end ##############\n')
def class_to_jar(dirname, full_path, target_dir):
''' generate .jar file from .class files and place it to target_dir.
notice: full path include dirname and do nothing if .jar file is already existed.'''
f = os.path.join(target_dir, '%s.jar'%dirname)
if os.path.exists(f):
raise Exception('WARNING: file already exists: %s\n'%f)
# os.remove(f)
# _printlog('WARNING: replace file %s' % f)
class_file_list = _add_files_to_list(full_path, '.*\.class$')
if not class_file_list:
_printlog('ERROR: no .class file in %s' % (full_path))
else:
os.chdir(full_path)
command = 'jar cf %s.jar' % (dirname)
start = len(full_path)+1
for f in class_file_list:
command = "%s %s" % (command, f[start:])
_execute_cmd(command)
shutil.move('%s.jar'%dirname, target_dir)
os.remove('%s.jar'%dirname)
def generate_jar_all(src_path, target_dir):
''' make all .class files in each sub-dir in src_path into .jar file which is placed in target_dir.'''
_printlog('############## Make jar starting ##############\n')
operate_list = _exclude(src_path)
success_list = []
fail_list = []
for dirname in operate_list:
full_path = os.path.join(src_path, dirname)
try:
class_to_jar(dirname, full_path, target_dir)
success_list.append(dirname)
except Exception, e:
_printlog("WARNING ON generate_jar_all: %s\n%s\n"%(full_path, e))
fail_list.append(dirname)
continue
_printlog('total_count=%s\n'%len(operate_list))
_printlog('success_count=%s\n'%len(success_list))
_printlog('success_list=\n%s'%'\n'.join(success_list))
_printlog('fail_count=%s\n'%len(fail_list))
_printlog('fail_list=\n%s'%'\n'.join(fail_list))
_printlog('############## Make jar end ##############\n')
def toJar(archive_dir, extract_dir, jar_dir):
''' Main Entry '''
copy_original_jar(archive_dir, jar_dir)
bin_path = os.path.join(extract_dir, 'bin')
generate_class_all(os.path.join(extract_dir, 'src'), bin_path)
generate_jar_all(bin_path, jar_dir)
if __name__ == '__main__':
usage = '''Usage:
\tgenerateJar.py copy_jar
\tgenerateJar.py to_class
\tgenerateJar.py archive_path extract_dir jar_dir
'''
import sys
argv_len = len(sys.argv)
src_path = 'C:\\works\\workload_src'
target_dir = "C:\\works\\workload_out"
jar_dir = "C:\\ProGuard\\proguard4.8\\proguard4.8\\lib"
if argv_len == 1:
toJar(src_path, target_dir, jar_dir)
elif argv_len == 2:
option = sys.argv[1]
if option == 'copy_jar':
copy_original_jar(src_path, jar_dir)
elif option == 'to_class':
generate_class_all(os.path.join(target_dir, 'src'), os.path.join(target_dir, 'bin'))
else:
print usage
sys.exit()
elif argv_len == 4:
toJar(sys.argv[1], sys.argv[2], sys.argv[3])
else:
print usage
sys.exit()