PluginDownloader: Use GitHub's APIv3.

This commit is contained in:
Valentin Lorentz 2012-06-13 18:07:23 +02:00
parent 6286558fa5
commit 0a442dfd15

View File

@ -74,62 +74,31 @@ class GithubRepository(GitRepository):
) )
_apiUrl = 'http://github.com/api/v2/json' _apiUrl = 'https://api.github.com'
def _query(self, type_, uri_end, args={}): def _query(self, type_, uri_end, args={}):
args = dict([(x,y) for x,y in args.items() if y is not None]) args = dict([(x,y) for x,y in args.items() if y is not None])
url = '%s/%s/%s?%s' % (self._apiUrl, type_, uri_end, url = '%s/%s/%s?%s' % (self._apiUrl, type_, uri_end,
urllib.urlencode(args)) urllib.urlencode(args))
print repr(url)
return json.load(utils.web.getUrlFd(url)) return json.load(utils.web.getUrlFd(url))
def getPluginList(self): def getPluginList(self):
latestCommit = self._query( plugins = self._query(
'repos', 'repos',
'show/%s/%s/branches' % ( '%s/%s/contents' % (
self._username, self._username,
self._reponame, self._reponame,
) )
)['branches']['master'] )
path = [x for x in self._path.split('/') if x != ''] if plugins is None:
treeHash = self._navigate(latestCommit, path)
if treeHash is None:
log.error(( log.error((
'Cannot get plugins list from repository %s/%s ' 'Cannot get plugins list from repository %s/%s '
'at Github' 'at Github'
) % (self._username, self._reponame)) ) % (self._username, self._reponame))
return [] return []
nodes = self._query( plugins = [x['name'] for x in plugins if x['type'] == 'dir']
'tree',
'show/%s/%s/%s' % (
self._username,
self._reponame,
treeHash,
)
)['tree']
plugins = [x['name'] for x in nodes if x['type'] == 'tree']
return plugins return plugins
def _navigate(self, treeHash, path):
if path == []:
return treeHash
tree = self._query(
'tree',
'show/%s/%s/%s' % (
self._username,
self._reponame,
treeHash,
)
)['tree']
nodeName = path.pop(0)
for node in tree:
if node['name'] != nodeName:
continue
if node['type'] != 'tree':
return None
else:
return self._navigate(node['sha'], path)
# Remember we pop(0)ed the path
return None
def _download(self, plugin): def _download(self, plugin):
try: try:
fileObject = urllib2.urlopen(self._downloadUrl) fileObject = urllib2.urlopen(self._downloadUrl)