Skip to content

Instantly share code, notes, and snippets.

@PeterJCLaw
Created December 12, 2011 21:19
Show Gist options
  • Save PeterJCLaw/1469157 to your computer and use it in GitHub Desktop.
Save PeterJCLaw/1469157 to your computer and use it in GitHub Desktop.
A file to fetch a zip from the SR IDE
#!/usr/bin/env python
import urllib2
import Cookie
import json
import config
IDE = config.URL
def extractToken(response):
info = response.info()
# print "info:", info
cookie = info['Set-Cookie']
cookie = Cookie.BaseCookie(cookie)
# print "cookie:", cookie
token = cookie['token']
# print "token:", token.value
return token.value
_token = None
def getCurrentToken():
global _token
if _token is None:
pass
# TODO: go get it from somewhere
return _token
def saveCurrentToken(token):
global _token
_token = token
if _token is None:
raise Exception('No token to save!')
# TODO: save it more than this.
def getURLForEnpoint(endPoint):
url = IDE + "control.php/" + endPoint
return url
def makeIDERequest(endPoint, data):
auth_token = getCurrentToken()
data_string = json.dumps(data)
url = getURLForEnpoint(endPoint)
req = urllib2.Request(url, data_string)
if auth_token is not None:
req.add_header('Cookie', 'token=%s' % auth_token)
response = urllib2.urlopen(req)
response_data = json.load(response)
#print 'response_data:', json.dumps(response_data, sort_keys = True, indent = 4)
if response_data.has_key('error'):
raise Exception('Server error: %s' % response_data['error'][1])
auth_token = extractToken(response)
saveCurrentToken(auth_token)
return response_data
def printDict(data):
print json.dumps(data, sort_keys = True, indent = 4)
if __name__ == '__main__':
# Authenticate before we can do anything
username = config.username
password = config.password
data = dict(username=username, password=password)
makeIDERequest('auth/authenticate', data)
# Get the zip created
info = dict(project="new-project", rev="HEAD", team="12")
data = makeIDERequest('proj/co', info)
printDict(data)
# Fetch the zip
url = data['url']
zip = urllib2.urlopen(IDE + url)
f = open('robot.zip', 'w')
f.write(zip.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment