Code
import json
import os
import shlex
import subprocess
import time
from urllib.request import urlopen
gitlab_address = ''
gitlab_token = ''
project_dir_root = ''
def pull_git_project():
for index in range(10):
url = "%s/api/v4/projects?private_token=%s&per_page=100&page=%d&order_by=name" % (
gitlab_address, gitlab_token, index)
print(url)
allProjects = urlopen(url)
allProjectsDict = json.loads(allProjects.read().decode(encoding='UTF-8'))
if len(allProjectsDict) == 0:
break
for thisProject in allProjectsDict:
try:
thisProjectURL = thisProject['http_url_to_repo']
thisProjectPath = project_dir_root + thisProject['path_with_namespace']
print(thisProjectURL + ' ' + thisProjectPath)
if os.path.exists(thisProjectPath):
print('git -C "%s" pull' % thisProjectPath)
command = shlex.split('git -C "%s" pull' % thisProjectPath)
else:
print('git clone %s %s' % (thisProjectURL, thisProjectPath))
command = shlex.split('git clone %s %s' % (thisProjectURL, thisProjectPath))
resultCode = subprocess.Popen(command)
print(resultCode)
time.sleep(2)
except Exception as e:
print("Error on %s: %s" % (thisProjectURL, e.strerror))
if __name__ == '__main__':
pull_git_project()