diff --git a/.gitignore b/.gitignore index 0786bfc..b8eb5d1 100644 --- a/.gitignore +++ b/.gitignore @@ -15,8 +15,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ -lib64/ parts/ sdist/ var/ diff --git a/lib/.gitlab.py.swp b/lib/.gitlab.py.swp new file mode 100644 index 0000000..7eb00af Binary files /dev/null and b/lib/.gitlab.py.swp differ diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/config.py b/lib/config.py new file mode 100644 index 0000000..97372d4 --- /dev/null +++ b/lib/config.py @@ -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()) + diff --git a/lib/gitlab.py b/lib/gitlab.py new file mode 100644 index 0000000..8fabd8e --- /dev/null +++ b/lib/gitlab.py @@ -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 +