2007年10月16日 星期二

[程式設計] make clean (Python 版本)



專案開發的同時, 會產生一些中間檔.
當要放到 svn 作版本控制時, 這些中間檔可以被刪除並且節省硬碟空間.

makefile 真的很好用, 尤其是在跨語言編譯時, 更顯的 makefile 的優越.
只要下一個 make clear command, 那麼所有的中間產物都會被清掉.
那麼 script 版本的 make clear, 要怎麼作呢?

一般 Vistual Studio 2005 與 Java 協同工作的中間檔類型如下,

'*.exe','*.obj','*.pch','*.pdb','*.idb','*.ncb','BuildLog.*','*.ilk','*.suo','*.bak','*.class'

所以只要寫一個批次檔, 將專案中的所有中間檔找出來一次刪除即可.

下面是 makeclear 的 Python 範例.


---------------------------- Python code ---------------------
# Make clear Python version
# by Jing
import os
import glob
import win32ui
import win32con

# Specified the file types which you want to clear
strDeletePattern=['*.exe','*.obj','*.pch','*.pdb','*.idb','*.ncb','BuildLog.*','*.ilk','*.suo','*.bak','*.class']

oldDir=os.getcwd()
top=oldDir
#win32ui.MessageBox(top,'Message',win32con.MB_OK)
ret=win32ui.MessageBox('Project Directory:\n\n\t'+top+' '+'\n\n Are you sure ? \n','Make Clear Operator',win32con.MB_OKCANCEL)
if(ret==win32con.IDCANCEL):
print 'Cancel'
else:
# txtFile=open('./remove.txt','w')
count=0
for root, dirs, files in os.walk(top, topdown=False):
for name in dirs:
FullDirname=os.path.join(root, name)
#print FullDirname
os.chdir(FullDirname) # into the directory
for i in strDeletePattern :
f=os.path.join(FullDirname, i)
RemoveList=glob.glob(f)
for x in RemoveList:
os.remove(x)
count=count+1
#txtFile.write(x)
#txtFile.write('\n')
#txtFile.close()
win32ui.MessageBox('%d files are removed! '%count,'Job Done!',win32con.MB_OK)
os.chdir(oldDir)
---------------------------- end of code ---------------------

Note:
1. Python 是一個跨平台的 Script language. 許多大型專案管理系統如 ViewVC 就是
用 Python 搭配其他系統所建構出來的.

ActiveState Python 是Windows 平台的 Python 實作. (Download)


2. 如果你使用 Windows, 那麼 make clean (Dos 版) 會比較有效率 [1].

by Jing

相關文章
[1] [程式設計] makeclean (Dos 批次檔版本) , 2009/3/12.

沒有留言:

張貼留言