java class to jar_java to class, class to jar

'''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'''

importos, shutilfrom common_util import_printlog, _exclude, _add_files_to_list, _execute_cmddefcopy_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 infile_list:

archive_file_path=os.path.join(src_path, f)try:if notos.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)ifos.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)exceptException, 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')defgenerate_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 inoperate_list:try:

full_path=os.path.join(src_path, dirname)

java_file_list= _add_files_to_list(full_path, '.*\.java$')if notjava_file_list:

_printlog("WARNING: no java file in %s\n"%(full_path))else:

dest_full_path=os.path.join(target_dir, dirname)ifos.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)exceptException, 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')defclass_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)ifos.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 notclass_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 inclass_file_list:

command= "%s %s" %(command, f[start:])

_execute_cmd(command)

shutil.move('%s.jar'%dirname, target_dir)

os.remove('%s.jar'%dirname)defgenerate_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 inoperate_list:

full_path=os.path.join(src_path, dirname)try:

class_to_jar(dirname, full_path, target_dir)

success_list.append(dirname)exceptException, 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')deftoJar(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'''

importsys

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:printusage

sys.exit()elif argv_len == 4:

toJar(sys.argv[1], sys.argv[2], sys.argv[3])else:printusage

sys.exit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值