python-gitlab

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"...文件下载完毕..."

posted on 2022-03-11 16:32  帅胡  阅读(1050)  评论(0编辑  收藏  举报

导航