扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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行多点,如果用其它语言写,肯定不止这么少。
1import sys, string, os
2
3g_sourcepath = ''
4g_destpath=''
5g_parsefileext=['java','jsp',
6 'c','cpp','h',
7 'py','pl','php']
8
9def 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 lines
16
17def isparsefile(filename):
18 splits = os.path.splitext(filename)
19 if len(splits) < 2:
20 return False
21 ext = splits[1]
22 ext = ext[1:]
23 if ext in g_parsefileext :
24 return True
25 else:
26 return False
27
28def getsubdir(dirname):
29 basedirlen = len(g_sourcepath)
30 if basedirlen >= len(dirname)-1:
31 return ''
32 subdir = dirname[basedirlen+1:]
33 return subdir
34
35def scan(arg, dirname, names):
36 for filename in names:
37 # 如果不是文件(还可能是目录),则跳过
38 if os.path.isfile(dirname+'\\'+filename) == False:
39 continue
40 # 如果是生成版本说明文件,则跳过
41 if filename == 'version.txt':
42 continue
43 if isparsefile(filename) :
44 fileparsecopy(dirname,filename,arg)
45 else:
46 fileonlycopy(dirname,filename,arg)
47
48def fileonlycopy(dirname,filename,verlist):
49 srcfilename = dirname+'\\'+filename
50 file = open(srcfilename,'r')
51 text = file.read()
52 file.close()
53 subdir = getsubdir(dirname)
54 for ver in verlist :
55 destfilepath = g_destpath+'\\'+ver
56 if len(subdir) > 0 :
57 destfilepath = destfilepath +'\\'+subdir
58 if(os.path.exists(destfilepath) == False):
59 os.mkdir(destfilepath)
60 destfilename = destfilepath+'\\'+filename
61 file = open(destfilename,'w')
62 file.write(text)
63 file.close()
64 print 'copy '+destfilename
65
66def fileparsecopy(dirname,filename,verlist):
67 srcfilename = dirname+'\\'+filename
68 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 = -1
80 lineno = 1
81 for curline in srclines :
82 wordindex = -1
83 verindex = 0
84 for flag in verlist :
85 if '#if_version '+flag in curline :
86 curverindex = verindex
87 break
88 elif '#end_version' in curline :
89 curverindex = -1
90 break
91 verindex = verindex +1
92
93 if verindex == len(verlist) : # 不是标记行
94 if curverindex >= 0: # 某个版本标记内的行
95 destfiletexts[curverindex] = destfiletexts[curverindex] + curline
96 else : # 所有版本都有的内容
97 for i in range(len(destfiletexts)) :
98 destfiletexts[i] = destfiletexts[i]+ curline
99
100 lineno = lineno +1
101 # 产生各个版本的目标文件
102 verindex = 0;
103 for ver in verlist :
104 destfilepath = g_destpath+'\\'+ver
105 if len(subdir) > 0 :
106 destfilepath = destfilepath +'\\'+subdir
107 if(os.path.exists(destfilepath) == False):
108 os.mkdir(destfilepath)
109 destfilename = destfilepath+'\\'+filename
110 file = open(destfilename,'w')
111 file.write(destfiletexts[verindex])
112 file.close()
113 verindex = verindex + 1
114 print 'parse '+ destfilename
115
116# main
117if len(sys.argv) < 3 :
118 sys.stderr.write("No source directory or dest directory parameter!");
119else:
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)
125sys.exit(1)
126
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者