python利用pysvn發布lib的小程序


背景:

    本人在公司的平台部門工作,我們部門寫出的代碼都是編譯成.a文件,定期發布版本到各個產品,現在老大要求我負責每周向公司的某個產品發布lib。發布lib的步驟大概就是自動化的兄弟給我提供一個歸檔的版本號、lib的標簽號(對應我們平台的代碼)和產品適配的標簽號(對應產品代碼,我們的.a文件會定期提交到這個svn下),然后我根據這個信息,操作svn,定期把適配中指定標簽下的指定的兩個文件夾導出到歸檔目錄下,然后在歸檔路徑下創建記錄這次發布信息(lib、適配各包含哪些標簽、版本信息)的文檔,還有就是創建一個release notes,要大家填寫依賴。為了方便記錄我還在另一個目錄下記錄着每次發布的信息。大概就是這樣比較機械的操作,作為程序員當然不能甘於每周重復一次這樣無趣的操作,於是乎利用今天調休的時間,用python寫了個腳本代替這個工作。

    主要用到的是python和pysvn(python)的一個第三方庫。這里注意下pysvn要和對應版本的python使用才能生效,否則import會失敗,所以建議先選pysvn,然后根據版本選python。

軟件下載和使用文檔:

    軟件下載:https://www.python.org/getit/

                      http://pysvn.tigris.org/project_downloads.html

    pysvn非常好的官方使用說明文檔:http://pysvn.tigris.org/docs/pysvn_prog_ref.html

代碼:

import pysvn
import os
import time
import shutil

#發布lib時對應的適配代碼路徑
code_path = "F:/xyp_work";
#lib的歸檔路徑
dest_path = "F:/AR_lib發布";
#自己的備份信息
myself_save_path = "F:/save/備份.txt";
#填寫依賴的路徑
dependence_path = "F:/依賴";
#維護一個專門記錄lib和br標簽號的文檔用於讀取上一次發lib時的標簽號
old_num_path = "F:/save/old_num.txt";

#svn 路徑
lib_svn = "lib的svn路徑"
br_svn = "br的svn路徑"

#版本號和標簽號
#verson = "esap v2R2C00B100";
#lib_num = 2;
#br_num = 7;

###讀取上一次的標簽號和用戶輸入的本次lib的標簽號和版本號###
print("輸入版本號");
verson = input();
print("輸入lib的標簽號");
lib_num = int(input());
print("輸入適配的標簽號");
br_num = int(input());

fp = open(old_num_path,"r");
last_lib_num = fp.readline();
last_br_num = fp.readline();  
fp.close();

print("last lib num is  "+str(last_lib_num));
print("last br num is  "+str(last_br_num));
print("current verson is "+str(verson)+"\n");
print("current lib num is  "+str(lib_num)+"\n");
print("current br num is  "+str(br_num)+"\n");
print("please confirm : y/n");

res=input();
if "y"== res:
    print("now start");
else:
    exit();


###################創建文件夾和文件######################
vasp = dest_path+'/'+"vasp";
vaspadp = dest_path + '/' + "vaspadp";
svn_txt = dest_path + '/'+"svn.txt";

isExists=os.path.exists(dest_path);
if not isExists:
    os.makedirs(dest_path);
else:
    print("AR_lib發布文件夾已存在,看看是否搞錯了");
    exit();
    
isExists=os.path.exists(vasp)
if not isExists:
    os.makedirs(vasp);

isExists=os.path.exists(vaspadp)
if not isExists:
    os.makedirs(vaspadp);

#svn.txt 
fp = open(svn_txt,"a");
fp.write(verson+"\n");
fp.write((lib_svn+" "+ str(lib_num) +"\n"));
fp.write((br_svn+" "+ str(br_num) +"\n"));
fp.close();

#自己的備份記錄
fp = open(myself_save_path,"a");
fp.write("\n");
fp.write("\n");
fp.write("\n");
fp.write(time.strftime("%d/%m/%Y") + "\n");
fp.write(verson+"\n");
fp.write((lib_svn+" "+ str(lib_num) +"\n"));
fp.write((br_svn+" "+ str(br_num) +"\n"));
fp.close();

#依賴文件 復制release notes.xlsx,然后重命名,否則直接創建出來不是共享的
shutil.copyfile(dependence_path+"/release notes.xlsx",dependence_path + "/" + verson+" release notes.xlsx");

#維護一個專門記錄lib和br標簽號的文檔用於讀取上一次發lib時的標簽號
fp = open(old_num_path,"w");
fp.truncate(); #清除文件內容
fp.write(str(lib_num)+"\n");
fp.write(str(br_num)+"\n");
fp.close();
##############################################

pysvn_current_br_num = pysvn.Revision( pysvn.opt_revision_kind.number, br_num);
pysvn_log_start = pysvn.Revision( pysvn.opt_revision_kind.number, last_lib_num);
pysvn_log_end = pysvn.Revision( pysvn.opt_revision_kind.number, lib_num);
"""
retcode - boolean, False if no username and password are available. True if subversion is to use the username and password.
username - string, the username to uses
password - string, the password to use
save - boolean, return True if you want subversion to remember the username and password in the configuration directory. return False to prevent saving the username and password.
"""
#def get_login( realm, username, may_save ):
#    return retcode, username, password, save

client = pysvn.Client()
#client.callback_get_login = get_login

client.revert(code_path,True);

client.update(code_path,True,pysvn_current_br_num,False);

client.export(code_path+"/vasp",vasp,True,pysvn_current_br_num,None,False);
client.export(code_path+"/product/ar/vaspadp",vaspadp,True,pysvn_current_br_num,None,False);

"""
log returns a list of log entries; each log entry is a dictionary. The dictionary contains: 
author - string - the name of the author who committed the revision
date - float time - the date of the commit
message - string - the text of the log message for the commit
revision - pysvn.Revision - the revision of the commit
changed_paths - list of dictionaries. Each dictionary contains:
path - string - the path in the repository
action - string
copyfrom_path - string - if copied, the original path, else None
copyfrom_revision - pysvn.Revision - if copied, the revision of the original, else None
"""
logmessege=client.log(code_path,pysvn_log_start,pysvn_log_end,False,False);
log_file = dest_path + '/'+"log.txt";
fp = open(log_file,"a");
for log_one in logmessege:
    fp.write(log_one.author +" "+ log_one.message + "\n");
fp.close();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM