Commit 6bee497e authored by Michael Völske's avatar Michael Völske

move api functions to separate module; add six.py; set PYTHONPATH

parent 9e1052d4
__pycache__
.idea
*~
\ No newline at end of file
*~
.#*
*.pyc
This diff is collapsed.
import os
import json
try:
## Using Python3
import configparser
from urllib.request import urlopen
from urllib.parse import urlencode
def get_headers_and_json_response(url):
with urlopen(url) as response:
data = json.loads(response.read().decode('utf-8'))
headers = response.getheaders()
headers = dict((k.lower(), v) for k, v in headers)
return (headers, data)
except ImportError:
## Using Python2
import ConfigParser as configparser
from urllib import urlopen
from urllib import urlencode
def get_headers_and_json_response(url):
response = urlopen(url)
return (response.headers.dict, json.loads(response.read()))
gitlab_config = configparser.ConfigParser()
gitlab_config.read(os.path.join(os.environ["HOME"], ".python-gitlab.cfg"))
def gitlab_get_request(path, **params):
try:
url = gitlab_config.get('webis', 'url')
params['private_token'] = gitlab_config.get('webis', 'private_token')
except KeyError:
sys.stderr.write("Error reading config file. Have you run 'webis git init'?\n")
sys.exit(1)
url = "{}/api/v3/{}?{}".format(url, path, urlencode(params))
headers, data = get_headers_and_json_response(url)
return (headers, data)
def gitlab_get_all_pages(path, **params):
params['per_page'] = 100
params['page'] = 1
headers, data = gitlab_get_request(path, **params)
while ('x-page' in headers and
int(headers['x-page']) != int(headers['x-total-pages'])):
params['page'] = int(headers['x-page']) + 1
headers, data1 = gitlab_get_request(path, **params)
data += data1
return data
......@@ -3,61 +3,17 @@
#
from __future__ import print_function
import json
import os
import re
import subprocess
import sys
try:
## Using Python3
import configparser
from urllib.request import urlopen
from urllib.parse import urlencode
def get_headers_and_json_response(url):
with urlopen(url) as response:
data = json.loads(response.read().decode('utf-8'))
headers = response.getheaders()
headers = dict((k.lower(), v) for k, v in headers)
return (headers, data)
except ImportError:
## Using Python2
import ConfigParser as configparser
from urllib import urlopen
from urllib import urlencode
def get_headers_and_json_response(url):
response = urlopen(url)
return (response.headers.dict, json.loads(response.read()))
input = raw_input
config = configparser.ConfigParser()
config.read(os.path.join(os.environ["HOME"], ".python-gitlab.cfg"))
def custom_get_request(path, **params):
try:
url = config.get('webis', 'url')
params['private_token'] = config.get('webis', 'private_token')
except KeyError:
sys.stderr.write("Error reading config file. Have you run 'webis git init'?\n")
sys.exit(1)
url = "{}/api/v3/{}?{}".format(url, path, urlencode(params))
headers, data = get_headers_and_json_response(url)
return (headers, data)
print(os.getcwd())
import loader
from six.moves import input
def custom_get_all_pages(path, **params):
params['per_page'] = 100
params['page'] = 1
headers, data = custom_get_request(path, **params)
while ('x-page' in headers and
int(headers['x-page']) != int(headers['x-total-pages'])):
params['page'] = int(headers['x-page']) + 1
headers, data1 = custom_get_request(path, **params)
data += data1
return data
from webis_gitlab import gitlab_config, gitlab_get_request, gitlab_get_all_pages
class GroupWithProjects(object):
......@@ -70,8 +26,8 @@ class GroupWithProjects(object):
def gather_groups_and_projects():
groups = custom_get_all_pages('/groups', all_available='true')
projects = custom_get_all_pages('/projects/visible')
groups = gitlab_get_all_pages('/groups', all_available='true')
projects = gitlab_get_all_pages('/projects/visible')
groups = {g['path']: GroupWithProjects(g, []) for g in groups}
for p in projects:
......@@ -84,7 +40,7 @@ def gather_groups_and_projects():
def get_user_info():
return custom_get_request('/user')[1]
return gitlab_get_request('/user')[1]
def get_group_selections(groups):
......
......@@ -102,6 +102,9 @@ def check_subcommand_shebang(subcommandpath, allowed_scripts):
def run_subcommand(subcommand, subcommandpath, params=[]):
"""Runs subcommand passing params and returns its exit code."""
lDbg("Running " + subcommand + " at " + subcommandpath)
pp = os.environ.get('PYTHONPATH')
webis_dir = os.path.dirname(os.path.realpath(__file__))
os.environ['PYTHONPATH'] = webis_dir if pp is None else pp + ':' + webis_dir
cmd = " ".join([subcommandpath] + params)
return_value = os.system(cmd)
if return_value == 0:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment