diff --git a/.gitignore b/.gitignore index 6c106ead..04eb24ae 100755 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ MANIFEST *.DS_Store deb_dist .fuse_* +*.swp diff --git a/bloom/github.py b/bloom/github.py index b987c84c..b397b622 100644 --- a/bloom/github.py +++ b/bloom/github.py @@ -41,6 +41,7 @@ import datetime import json import socket +import os from urlparse import urlunsplit from urllib import urlencode @@ -59,6 +60,10 @@ import bloom +GITHUB_USER = os.getenv('GITHUB_USER', None) +GITHUB_PASSWORD = os.getenv('GITHUB_PASSWORD', None) + + def auth_header_from_basic_auth(user, password): return "Basic {0}".format(base64.b64encode('{0}:{1}'.format(user, password))) @@ -88,6 +93,10 @@ def do_github_post_req(path, data=None, auth=None, site='api.github.com'): else: request = Request(url, data=json.dumps(data), headers=headers) # POST + if GITHUB_USER and GITHUB_PASSWORD: + authheader = 'Basic %s' % base64.b64encode('%s:%s' % (GITHUB_USER, GITHUB_PASSWORD)) + request.add_header('Authorization', authheader) + try: response = urlopen(request, timeout=120) except HTTPError as e: diff --git a/bloom/util.py b/bloom/util.py index 07fdd534..cf258922 100755 --- a/bloom/util.py +++ b/bloom/util.py @@ -38,6 +38,7 @@ import argparse import os +import base64 import shutil import socket import sys @@ -48,12 +49,12 @@ # Python2 from urllib2 import HTTPError from urllib2 import URLError - from urllib2 import urlopen + from urllib2 import urlopen, Request except ImportError: # Python3 from urllib.error import HTTPError from urllib.error import URLError - from urllib.request import urlopen + from urllib.request import urlopen, Request from email.utils import formatdate @@ -78,6 +79,11 @@ from bloom.logging import sanitize from bloom.logging import warning + +GITHUB_USER = os.getenv('GITHUB_USER', None) +GITHUB_PASSWORD = os.getenv('GITHUB_PASSWORD', None) + + try: to_unicode = unicode except NameError: @@ -195,8 +201,13 @@ def load_url_to_file_handle(url, retry=2, retry_period=1, timeout=10): :param timeout: timeout for opening the URL in seconds :type timeout: float """ + req = Request(url) + if GITHUB_USER and GITHUB_PASSWORD: + authheader = 'Basic %s' % base64.b64encode('%s:%s' % (GITHUB_USER, GITHUB_PASSWORD)) + req.add_header('Authorization', authheader) + try: - fh = urlopen(url, timeout=timeout) + fh = urlopen(req, timeout=timeout) except HTTPError as e: if e.code == 503 and retry: time.sleep(retry_period)