from lib2to3.pgen2 import token
from urllib.request import urlopen
import json
import subprocess
import shlex
import os
# 获取当前文件路径
strToken = "xxxxxxx" # 自己的token,去gitlab上面设置
parentpath = os.getcwd()
print("parentpath " + parentpath)
# 获取所有group,其中private_token就是access_token
allgroups = urlopen(
"https://gitlab.xxx.com/api/v4/groups?private_token=" + strToken)
allgroupsDict = json.loads(allgroups.read().decode())
def getProject(projects):
projectsDict = json.loads(projects.read().decode())
for thisProject in projectsDict:
# 已经下载的,直接跳过
modelName = parentpath+"/"+thisProject["path"]
isExists = os.path.exists(modelName)
print(modelName)
if isExists:
continue
try:
# 因为我本地git clone 配置的是http格式的,所以我选择了http_url_to_repo, 如果你是用的git@格式,你就选择ssh_url_to_repo
thisProjectURL = thisProject['http_url_to_repo']
command = shlex.split('git clone %s' % thisProjectURL)
subprocess.Popen(command)
# print("project url %s" % (thisProjectURL))
except Exception as e:
print("Error on %s:%s" % (thisProjectURL, e))
def getGroup(thisgroup):
try:
# 切换到父目录
os.chdir(parentpath)
thisGroupFullPath = thisgroup['full_path']
thisGroupId = thisgroup['id']
print(
"==================================================================================")
print("group full path " + thisGroupFullPath + ": " + str(thisGroupId))
# 这里我3rd这个group下的仓库排除
if "3rd" not in thisGroupFullPath:
# 获取当前group下的所有projects
projects = urlopen("https://gitlab.xxx.com/api/v4/groups/" +
str(thisGroupId)+"/projects?private_token=" + strToken + "&per_page=100") # per_page默认是20,如果超过20则会丢失,所以要显示设置
getProject(projects)
except Exception as e:
print("Error on %s:%s" % (thisGroupFullPath, e))
for thisgroup in allgroupsDict:
getGroup(thisgroup)