本來是想寫一個東西可以直接調用TortoiseSVN保存當前代碼到一個分枝下的。
可惜調用SVN的部分還在研究。就先寫了目錄拷貝的部分。
如果有喜歡研究Python的童鞋愿意提供想法或者建議的話,
這里先謝謝了。 :)
就目錄拷貝的部分,思想很簡單。讀配置文件中的配置信息。
生成一個項目名稱加日期時間組成的文件夾名為分枝名稱。把當前項目下的全部內容
拷貝到這個目錄下。
然后要做的研究就是調用TortoiseSVN命令嵌入這部分代碼。
現在看代碼:
1. 讀取配置文件
配置文件很簡單。用的就是txt文件。 格式類似于:
# root:/Users/lichallenger/test_src/
# project:test
# destination:/Users/lichallenger/test_dst/
BTW: 我用的是Mac所以目錄格式是這樣的。如果你用的是Windows的話請適
當修改配置文件。
讀文件就是最簡單的了。直接用標準庫的文件操作模塊打開文件,讀出全部的配置。一共就三行,所以
也不用考慮效率什么的了。
# open config file and read config information
# author: bruce li
class ConfigHandler(object):
#
def __init__(self,config_path):
'''initializer'''
self.config_path = config_path
#read config infor
def read_config(self):
f = open(self.config_path)
try:
self.all_lines = f.readlines()
except:
raise
else:
f.close()
2. 拷貝目錄和目錄內容
拷貝目錄用了shutil模塊。里面有個方法可以直接把目錄和目錄下的全部內容拷貝到制定的其他目錄。
這樣就省得搞目錄遍歷之類的代碼了。?
# copy dir(s) & file(s) to configured path
# author: bruce li
import shutil
class CopyHandler(object):
#
def __init__(self,src_path,dest_path):
self.src_path = src_path
self.dest_path = dest_path
def move_content(self):
try:
shutil.copytree(self.src_path,self.dest_path)
except:
raise
@staticmethod
def move_src_content(src, dest):
try:
shutil.copytree(src_path,dest_path)
except:
raise
3. 綜合調用
這里用了time模塊獲取當前時間,然后生成目標文件夾名稱的一部分。?
外界給python傳的系統參數的第一個是文件名。這個文件就相當于C#項目里的Program文件一樣,
里面會包含一個main函數。雖然這個函數不一定要命名為main。?
還有注意下,Python代碼的換行符為\。?
? # ?main?of?dir?copy?function
import sys
import time
from code_bk_cpy import *
from code_bk_config import *
#print __name__
def main():
config_path = sys.argv[1]
# check if path of configuration path is empty
if (not config_path):
print 'configuration information is needed'
return -1
config_handler = ConfigHandler(config_path)
config_handler.read_config()
config_list = config_handler.all_lines
if len(config_list) != 3:
print 'configuration information is not correct'
return -1
# set source
sep = ':'
current_datetime = time.localtime(time.time())
root_path = config_list[0].split(sep)[1]
prj_name = config_list[1].split(sep)[1]
dst_path = config_list[2].split(sep)[1]
root_path = (root_path + prj_name).replace('\n','')
prj_folder = prj_name + str(current_datetime.tm_year) + str(current_datetime.tm_mon) + \
str(current_datetime.tm_mday) + str(current_datetime.tm_hour) + \
str(current_datetime.tm_min) + str(current_datetime.tm_sec)
dst_path = (dst_path + '/' + prj_folder + '/').replace('\n','')
copy_handler = CopyHandler(root_path,dst_path)
copy_handler.move_content()
print 'content moved'
# start main function
print __name__
if __name__ == "__main__":
main()
有時間我會研究下TortoiseSVN調用那塊的東西。估計不會難,就是調用exe傳參的問題。
本人初學Python,如有問題敬請指正!謝謝。
以上這篇Python備份目錄及目錄下的全部內容的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

