1 Star 1 Fork 0

云联/Python自动拉取Git多分组代码

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Git2Me.py 2.72 KB
一键复制 编辑 原始数据 按行查看 历史
yangwenzhuang 提交于 2021-04-22 10:26 +08:00 . add remark
# -*- coding: UTF-8 -*-
# 在Python3.0测试通过
# 需要在gitlab里面新建一个AccessToken填入gitlabToken
import sys
if sys.version_info < (3, 0):
import urllib
else:
from urllib.request import urlopen
import json
import subprocess, shlex
import time
import os
# gitlabAddr GitLab域名 git.xxx.com
gitlabAddr = 'xxxxxx'
# gitlabToken GitLab访问token 登录-设置-访问token设置
gitlabToken = 'xxxxxx'
# sync syn projects
for index in range(10):
url = "https://%s/api/v4/projects?private_token=%s&per_page=100&page=%d&order_by=name" % ( gitlabAddr, gitlabToken, index)
print(url)
if sys.version_info < (3, 0):
allProjects = urllib.urlopen(url)
else:
allProjects = urlopen(url)
allProjectsDict = json.loads(allProjects.read().decode(encoding='UTF-8'))
if len(allProjectsDict) == 0:
break
for thisProject in allProjectsDict:
try:
thisProjectId = thisProject['id']
thisProjectURL = thisProject['http_url_to_repo']
thisProjectPath = thisProject['path_with_namespace']
isContinue = True
if not thisProjectPath.startswith('jiaoguan/yingji/'):
continue
print(thisProjectURL + ' ' + thisProjectPath)
if not os.path.isdir(thisProjectPath):
print("create project dir %s" % (thisProjectPath))
os.makedirs(thisProjectPath)
time.sleep(0.5)
# syn branches
branchesUrl = "https://%s/api/v4/projects/%s/repository/branches?private_token=%s" % (gitlabAddr, thisProjectId , gitlabToken)
print(branchesUrl)
allBranches = urlopen(branchesUrl)
allBranchesDict = json.loads(allBranches.read().decode(encoding='UTF-8'))
for thisBranch in allBranchesDict:
thisBranchName = thisBranch['name']
thisBranchPath = thisProjectPath + '/' + thisBranchName
if not os.path.exists(thisBranchPath):
print("create branch dir %s" % (thisBranchPath))
os.makedirs(thisBranchPath)
time.sleep(0.5)
# sync code
gitDirPath = thisBranchPath + '/.git'
if os.path.exists(gitDirPath):
command = shlex.split('C:/Program Files/Git/bin/git -C "%s" pull' % (thisBranchPath))
else:
command = shlex.split(
'C:/Program Files/Git/bin/git clone -b %s %s %s' % (thisBranchName,thisProjectURL, thisBranchPath))
resultCode = subprocess.Popen(command)
time.sleep(1)
except Exception as e:
print("Error on %s: %s" % (thisProjectURL, e.strerror))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/coding2me/python-gitlab.git
git@gitee.com:coding2me/python-gitlab.git
coding2me
python-gitlab
Python自动拉取Git多分组代码
master

搜索帮助