python-gitlab:下載文件或者指定文件夾
1.下載文件
//需要先創建本地目錄,先刪除再創建
def emptyFolder(pathName):
if 1 == os.path.exists(pathName):
shutil.rmtree(pathName)
allPath = 'D:/WebContent/'
emptyFolder(allPath)
os.makedirs(allPath)
//下載gitlab文件到本地,先打開本地文件,file_path是遠程Git路徑,ref是分支
with open('D:/WebContent/AppConfig.h', 'wb') as f:
project.files.raw(file_path='/src/main/AppConfig.h', ref='ReplaceWeb', streamed=True, action=f.write)
print u"下載AppConfig.h成功"
2.下載文件夾
//需要先創建本地目錄,先刪除再創建
def emptyFolder(pathName):
if 1 == os.path.exists(pathName):
shutil.rmtree(pathName)
allPath = 'D:/WebContent/'
emptyFolder(allPath)
os.makedirs(allPath)
//獲取遠程文件樹(path是遠程Git文件夾目錄,ref是分支名,遞歸獲取)
fileList = project.repository_tree(path='bin/tools/', ref='master', recursive=True, all=True)
//本地創建文件樹中的目錄
for i in fileList:
if (i['type'] == "tree"):
dirs = 'D:/WebContent/%s' % i['path']
if not os.path.exists(dirs):
os.makedirs(dirs)
print u"...下載文件中..."
//下載tools文件夾
for i in fileList:
if (i['type'] == "blob"):
with open('D:/WebContent/%s' % i['path'], 'wb') as f:
project.files.raw(file_path=i['path'], ref='master', streamed=True, action=f.write)
print u"%s" %i['path']
print u"...文件下載完畢..."