代码拉取完成,页面将自动刷新
# -*- 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))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。