最近麻煩同事將要更新的程式碼更新正式機時發現,同事是一行一行的下指令,
例如 ︰
cp -a /tmp/1.php /var/www/html/1.php
cp -a /tmp/2.php /var/www/html/2.php
.....
.......
原因是該主機是新建立的自動更新的Shell還沒寫好,
於是就利用點業餘時間使用Python寫了一隻程式更新器給他使用,
當然這隻程式也可以在Windows底下使用,
當然在Linux下使用後,要確認一下檔案更新後的權限喔。
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
import os
import sys
import array
import errno
import shutil
import platform
def main():
operatFolder = "/tmp"
declare = '/'
if len(sys.argv) > 1:
operatFolder = sys.argv[1]
while True:
iv = raw_input("本次更新檔案的來源資料夾是否為︰[" + operatFolder + "] (y/n/e)︰")
if iv == "y" or iv == "Y":
break
elif iv == "n" or iv == "N":
while True:
operatFolder = raw_input("請輸入本次更新檔案的來源資料夾位置︰")
if os.path.exists(operatFolder) :
break
else:
print "[" + operatFolder + "]資料夾不存在請重新輸入"
else:
print "下次再為您服務 byebye!!"
sys.exit(1)
if platform.system().lower() == 'windows':
declare = '\\'
filesPath = operatFolder + declare + 'files.txt'
if not os.path.exists(filesPath):
print '找不到檔案更新清單︰' + filesPath ,
sys.exit(1)
fileopen = open(filesPath)
failCount = successCount = 0
failUpfateList = []
while True:
line = fileopen.readline().replace(' ','').replace('\n','').replace('\r','').replace('\r\n','')
if not len(line):
break
targetPath = line[:(line.rindex(declare)) + 1]
fileName = line[(line.rindex(declare)) + 1:]
sourcePath = operatFolder + declare + fileName
try:
if not os.path.isdir(targetPath):
print targetPath + '目標資料夾不存在,正在重新建立',
os.mkdir (targetPath)
print targetPath + '目標資料夾建立完成',
shutil.copy2(sourcePath,line)
if os.path.isfile (line):
print "[" + line + "] copy success"
successCount += 1
except:
failUpfateList.append("[" + str(line )+ "]" + str(sys.exc_info()[1]))
failCount += 1
fileopen.close()
print '檔案更新完成,總計︰' + str(successCount + failCount) + ' 成功︰' + str(successCount) + ' 失敗︰' + str(failCount)
if len(failUpfateList) > 0:
print "檔案更新失敗清單"
for element in failUpfateList:
print element
if __name__ == "__main__":
main()
使用的方式1.先將要更新的程式檔案集中放在同一層的資料夾中。
Ex
/tmp
2.將程式檔案於正式機的位徑含檔名寫了 files.txt之中
Ex
/var/www/html/1.php
/var/www/html/2.php
3.下執行命令
Ex
python update.py
或者給update.py執行權限
./update.py
沒有留言:
張貼留言