Add OAuth2 support to wescamp.py

This commit is contained in:
Alexander van Gessel 2012-10-15 16:19:22 +01:00
parent 19658537c1
commit 5523fa577b
2 changed files with 23 additions and 17 deletions

View file

@ -295,16 +295,16 @@ class GitHub(object):
Every GitHub object is specific to a directory and wesnoth version.
"""
def __init__(self, directory, version, userpass=None):
def __init__(self, directory, version, authorization=None):
"""Initializes a GitHub object.
directory: Directory in which the git repos for this wesnoth branch live.
version: The version of this wesnoth branch.
"""
logging.debug("GitHub created with directory {0} and version {1}, {2} authentication data".format(directory, version, "with" if userpass else "without"))
logging.debug("GitHub created with directory {0} and version {1}, {2} authentication data".format(directory, version, "with" if authorization else "without"))
self.directory = directory
self.version = version
self.userpass = userpass
self.authorization = authorization
def update(self):
"""Update all add-ons.
@ -482,8 +482,14 @@ class GitHub(object):
# probably because github's API doesn't send a www-authenticate header
if authenticate:
from base64 import encodestring
base64string = encodestring(self._github_userpass()).replace('\n','')
request.add_header("Authorization", "Basic {0}".format(base64string))
auth = self._github_authorization()
if ":" in auth:
# username:password
base64string = encodestring(auth).replace('\n','')
request.add_header("Authorization", "Basic {0}".format(base64string))
else:
# token
request.add_header("Authorization", "Bearer {0}".format(auth))
try:
response = urllib2.urlopen(request)
@ -510,9 +516,9 @@ class GitHub(object):
return json_parsed
def _github_userpass(self):
if self.userpass:
return self.userpass
def _github_authorization(self):
if self.authorization:
return self.authorization
else:
raise Error("Authentication required")

View file

@ -50,7 +50,7 @@ class tempdir:
if __name__ == "__main__":
git_version = None
git_userpass = None
git_auth = None
quiet_libwml = True
def update_addon(addon_obj, addon_name, addon_server, temp_dir):
@ -299,7 +299,7 @@ if __name__ == "__main__":
+ "upload aborted.", addon)
return
github = libgithub.GitHub(wescamp_dir, git_version, userpass=git_userpass)
github = libgithub.GitHub(wescamp_dir, git_version, authorization=git_auth)
has_updated = False
@ -324,17 +324,17 @@ if __name__ == "__main__":
pot_update(addon_obj, addon)
def checkout(wescamp, userpass=None, readonly=False):
def checkout(wescamp, auth=None, readonly=False):
"""Checkout all add-ons of one wesnoth version from wescamp.
wescamp The directory where all checkouts should be stored.
userpass Authentication data. Shouldn't be needed.
auth Authentication data. Shouldn't be needed.
readonly Makes a read-only checkout that doesn't require authentication.
"""
logging.debug("checking out add-ons from wesnoth version = '%s' to directory '%s'", git_version, wescamp)
github = libgithub.GitHub(wescamp, git_version, userpass=git_userpass)
github = libgithub.GitHub(wescamp, git_version, authorization=git_auth)
for addon in github.list_addons():
addon_obj = github.addon(addon, readonly=readonly)
@ -385,8 +385,8 @@ if __name__ == "__main__":
help = "Use git instead of svn to interface with wescamp. "
+ "This is a temporary option for the conversion from berlios to github.")
optionparser.add_option("-G", "--github-login",
help = "Username and password for github in the user:pass format")
optionparser.add_option("-G", "--github-auth",
help = "Username and password for github in the user:pass format, or an OAuth2 token.")
optionparser.add_option("-c", "--checkout", action = "store_true",
help = "Create a new branch checkout directory. "
@ -443,7 +443,7 @@ if __name__ == "__main__":
if(options.git):
logging.warn("--git is no longer required, as svn is no longer supported")
#TODO: remove entirely
git_userpass = options.github_login
git_auth = options.github_auth
if not wescamp:
logging.error("No wescamp checkout specified. Needed for git usage.")
sys.exit(2)
@ -542,7 +542,7 @@ if __name__ == "__main__":
sys.exit(2)
try:
checkout(wescamp, userpass=git_userpass, readonly=(options.checkout_readonly != None))
checkout(wescamp, auth=git_auth, readonly=(options.checkout_readonly != None))
except libgithub.AddonError, e:
print "[ERROR github in {0}] {1}".format(e.addon, str(e.message))
sys.exit(1)