早就听说python是一个十分不错的脚本语言,语法简洁而且开发效率很高。在从http://www.python.org下载了最新的windows版本的python解释器后,我又从eclipse的插件网下了pydev这个支持Python在Eclipse开发的插件。Python虽然是unix/linux下的东西,但是由于其执行和编写都很简单,所以在时常工作中能够快速解决一些小问题。同时,对于以后我需要研究算法,做论文的时候,也可以用python来快速开发一个算法的测试demo,尽量不必纠缠于程序具体实现时候的困难。
Python语言学起来比较容易,网上据说,python开发的效率比perl,php,ruby都高。在研究了IBM developworks的一篇文章:应用Python解决一些实际问题(http://www-128.ibm.com/developerworks/cn/linux/tips/l-python/)后,我也为现在正在做的J2ME项目做了个代码的预编译处理脚本。通过扫描Java项目中的标签,#if_version,#end_version,从一个原始的J2ME项目自动产生成各个机型的版本,比如NOKIA。索爱,MOTO。下面就是我第一次写的这个Python小程序,总共才100行多点,如果用其它语言写,肯定不止这么少。
1
import sys, string, os2
3
g_sourcepath = ''4
g_destpath=''5
g_parsefileext=['java','jsp',6
'c','cpp','h',7
'py','pl','php']8
9
def getversionlist():10
file = open('version.txt','r')11
lines = file.readlines()12
file.close()13
for i in range(len(lines)) :14
lines[i] = lines[i][:-1]15
return lines16
17
def isparsefile(filename):18
splits = os.path.splitext(filename)19
if len(splits) < 2:20
return False21
ext = splits[1]22
ext = ext[1:]23
if ext in g_parsefileext :24
return True25
else:26
return False27
28
def getsubdir(dirname):29
basedirlen = len(g_sourcepath)30
if basedirlen >= len(dirname)-1:31
return ''32
subdir = dirname[basedirlen+1:]33
return subdir34
35
def scan(arg, dirname, names):36
for filename in names:37
# 如果不是文件(还可能是目录),则跳过38
if os.path.isfile(dirname+'\\'+filename) == False:39
continue40
# 如果是生成版本说明文件,则跳过41
if filename == 'version.txt':42
continue43
if isparsefile(filename) :44
fileparsecopy(dirname,filename,arg)45
else:46
fileonlycopy(dirname,filename,arg)47
48
def fileonlycopy(dirname,filename,verlist):49
srcfilename = dirname+'\\'+filename50
file = open(srcfilename,'r')51
text = file.read()52
file.close()53
subdir = getsubdir(dirname)54
for ver in verlist :55
destfilepath = g_destpath+'\\'+ver56
if len(subdir) > 0 :57
destfilepath = destfilepath +'\\'+subdir58
if(os.path.exists(destfilepath) == False):59
os.mkdir(destfilepath)60
destfilename = destfilepath+'\\'+filename61
file = open(destfilename,'w')62
file.write(text)63
file.close()64
print 'copy '+destfilename65
66
def fileparsecopy(dirname,filename,verlist):67
srcfilename = dirname+'\\'+filename68
file = open(srcfilename,'r')69
srclines = file.readlines()70
file.close()71
72
subdir = getsubdir(dirname)73
74
destfiletexts = []75
for ver in verlist :76
destfiletexts.append('')77
78
# 扫描源文件的每一行79
curverindex = -180
lineno = 181
for curline in srclines :82
wordindex = -183
verindex = 084
for flag in verlist :85
if '#if_version '+flag in curline :86
curverindex = verindex87
break
88
elif '#end_version' in curline :89
curverindex = -190
break
91
verindex = verindex +192
93
if verindex == len(verlist) : # 不是标记行94
if curverindex >= 0: # 某个版本标记内的行95
destfiletexts[curverindex] = destfiletexts[curverindex] + curline96
else : # 所有版本都有的内容97
for i in range(len(destfiletexts)) :98
destfiletexts[i] = destfiletexts[i]+ curline99
100
lineno = lineno +1101
# 产生各个版本的目标文件102
verindex = 0;103
for ver in verlist :104
destfilepath = g_destpath+'\\'+ver105
if len(subdir) > 0 :106
destfilepath = destfilepath +'\\'+subdir107
if(os.path.exists(destfilepath) == False):108
os.mkdir(destfilepath)109
destfilename = destfilepath+'\\'+filename110
file = open(destfilename,'w')111
file.write(destfiletexts[verindex])112
file.close()113
verindex = verindex + 1114
print 'parse '+ destfilename115
116
# main117
if len(sys.argv) < 3 :118
sys.stderr.write("No source directory or dest directory parameter!");119
else:120
g_sourcepath = sys.argv[1]121
g_destpath = sys.argv[2]122
os.chdir(g_sourcepath)123
verlist = getversionlist()124
os.path.walk(g_sourcepath, scan, verlist)125
sys.exit(1)126