Skip to content

Commit

Permalink
added lib
Browse files Browse the repository at this point in the history
  • Loading branch information
wiro committed Dec 20, 2018
1 parent 851270c commit b13cae9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
Expand Down
Binary file added lib/.gitlab.py.swp
Binary file not shown.
Empty file added lib/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import print_function
import yaml
import os

class Config:
'''Load configuration from yaml file'''

def __init__(self,config_file):
'''Init config object'''

self.config_file = config_file
self.config_open()
self.config_load()
self.config_close()

def config_open(self):
try:
self.conf_fh = open(self.config_file,'r')
except IOError, e:
print("({})".format(e))
sys.exit(1)

def config_close(self):
self.conf_fh.close()

def config_load(self):
self.config = yaml.load(self.conf_fh.read())

74 changes: 74 additions & 0 deletions lib/gitlab.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from __future__ import print_function
import requests
import urllib
import sys
import time

class Api:
'''Api class for gitlab'''

def __init__(self,gitlab_url, token):
'''Init config object'''
self.headers = { "PRIVATE-TOKEN": token }
self.api_url = gitlab_url + "/api/v4"
self.download_url = None

def __api_export(self, project_url):
'''Send export request to API'''
self.download_url = None
return requests.post(self.api_url+"/projects/" + project_url + "/export", headers=self.headers)

def __api_status(self,project_url):
'''Check project status'''
return requests.get(self.api_url+"/projects/" + project_url + "/export", headers=self.headers)

def project_export(self, project_path):
url_project_path = urllib.quote(project_path, safe='')

# Let's export project
r = self.__api_export(url_project_path)
if ( str(r.status_code) == "202" ):
# Api good, check for status
max_tries = 20
s = ""
status_export = False
while max_tries != 0:
# Decrement tries
max_tries -= 1

r = self.__api_status(url_project_path)

# Check API reply status
if ( r.status_code == requests.codes.ok ):
json = r.json()

# Check export status
print(json)
if "export_status" in json.keys():
s = json["export_status"]
if s == "finished":
status_export = True
break
else:
s = "unknown"
print(s)

else:
print("API not respond well with %s" %(str(r.status_code)), file=sys.stderr)
break

# Wait litle bit
time.sleep(2)

if status_export:
self.download_url = json["_links"]
return True
else:
return False



else:
print("API not respond well with %s" %(str(r.status_code)), file=sys.stderr)
return False

0 comments on commit b13cae9

Please sign in to comment.