Python常用基础语法知识点大全(python中基本语法)
710
2022-08-23
python使用API接口来下载gitlab代码库(Gitlab api)
之前一直用简单的def函数形式写脚本,前段时间学习django的时候刚好将类复习了下,果然还是面向对象的编程比较方便,自己以后尽量都用类写一些东西,以下是使用了父类和子类写的脚本。
(后续备注:我这里基于python-gitlab 2.10.0之前的版本写的脚本,在最新的python-gitlab3.8.1中有些API用法可能已经不支持了,使用时建议安装2.10.0的API库,pip install python-gitlab==2.10.0,如果要研究最新的API使用方法请参考D:\Program Files (x86)\Python36# -*- coding=utf-8 -*-import gitlab, osfrom pathlib import Pathclass Login : def __init__(self, url, private_token, project_name) : self.url = url self.private_token = private_token self.project_name = project_name def stategl(self) : gl = gitlab.Gitlab(self.url, private_token=self.private_token) return gl def search(self) : projects = self.stategl().projects.list(search=self.project_name) # url = line.for line in projects if line.name == self.project_name for line in projects : if line.name == self.project_name : url = line. pid = line.id return url, pid def __str__(self) : return list(self.search())class GitREPO(Login) : def __init__(self, url, private_token, project_name) : super().__init__(url, private_token, project_name) def DOWNRepo(self,workdir, branch, repodir) : if not os.path.exists(workdir) : os.makedirs(workdir) project = self.stategl().projects.get(list(self.search())[1]) branches = project.branches.list() fileList = project.repository_tree(path=repodir, ref=branch, recursive=True, all=True) for i in fileList : print(i) dirs = os.path.join(workdir, i['path']) if i['type'] == "tree" : print(dirs) if not os.path.exists(dirs) : os.mkdir(dirs) repodir = os.path.join(repodir, i['name']) self.DOWNRepo(workdir, branch, repodir) elif i['type'] == "blob" : print(dirs) with open(dirs, 'wb') as f : project.files.raw(file_path=i['path'], ref=branch, streamed=True, action=f.write) print("%s" % dirs) #以下为提交代码到gitlab仓库的功能,我没加入到主程序中 def COMMITCode(self, branch, workdir, filelist) : project = self.stategl().projects.get(list(self.search())[1]) '''filelist为代码仓内部的相对路径,比如D:\REPO\JAVA1\JAVA2\est.java,这里仓库名称为JAVA1,那么filelist 就是 JAVA1\JAVA2\est.java''' filepath = os.path.join(workdir, filelist) # 本机需要提交的文件的具体路径 commitpaths = filepath.split(workdir)[1] # 远程仓库的路径,以/开始作为远程仓库的路径 data = { 'branch' : branch, 'commit_message' : 'update content', 'actions' : [ { 'action' : 'create', 'file_path' : commitpaths, 'content' : open(filepath, 'r', encoding='UTF-8').read(), }, ] } commit = project.commits.create(data)if __name__ == '__main__' : CUR_DIR = Path(__file__).resolve().parent # Path(__file__).resolve())当前脚本的详细路径 filepath = os.path.join(CUR_DIR, 'add-list.txt') #将需要下载的服务写到里面 url = ' token = 'rwwrtttrteaR repodir = '/' #远程仓库的根目录,即下载整个代码仓,如果带/file,就是下载代码仓中file目录下的文件 branch = 'develop' #指定下载的分支 with open(filepath,'r',encoding='utf-8') as f: for line in f.readlines(): reponame = line.strip() workdir = os.path.join(r'D:\Program Files\GitlabReposity', reponame) GitREPO(url, token, reponame).DOWNRepo(workdir, branch, repodir)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~