def authenticate(openid_uri, password): try: filepath = utils.user_url_to_filepath(openid_uri) host = urlparse(openid_uri.strip()).netloc; user = openid_uri.rsplit('/', 1)[1] except IndexError: raise MyProxyClientError('Invalid OpenID identifier') if not host or not user: raise MyProxyClientError('Invalid OpenID identifier') try: myproxy = MyProxyClient(hostname=host) credentials = myproxy.logon(user, password, bootstrap=True) cert_filepath = utils.user_cert_file(openid_uri) dir = os.path.dirname(cert_filepath); if not os.path.exists(dir): os.makedirs(dir) with open(cert_filepath, 'w') as fd: fd.write(credentials[0]) fd.write(credentials[1]) except socket.gaierror: raise MyProxyClientError('Invalid OpenID identifier')
def download(url, size, checksum, user_url): url = url.strip('"') request = None filepath = None try: cert_filepath = utils.user_cert_file(user_url) request = requests.get(url, cert=(cert_filepath, cert_filepath), verify=False, stream=True) # Registration is required if request.status_code == 403: # Update state response in meta data return request.text elif request.status_code != 200: raise Exception("HTTP status code: %s" % request.status_code) filepath = utils.url_to_download_filepath(user_url, url) dir = os.path.dirname(filepath) if not os.path.exists(dir): os.makedirs(dir) # Now we know the user is authorized, first check if they have already # downloaded this file. if os.path.exists(filepath): md5 = hashlib.md5() with open(filepath) as fp: for chunk in iter(partial(fp.read, 128), ""): md5.update(chunk) # if the checksums match we can skip the download if checksum == md5.hexdigest(): current_task.update_state(state="PROGRESS", meta={"percentage": 100}) return downloaded = 0 with open(filepath, "w") as fp: for block in request.iter_content(1024): if not block: break fp.write(block) downloaded += 1024 # update the task state percentage = int((downloaded / float(size)) * 100) current_task.update_state(state="PROGRESS", meta={"percentage": percentage}) except SoftTimeLimitExceeded: current_task.update_state(state="CANCELED") # Clean up the request and files if request: request.close() if os.path.exists(filepath): os.remove(filepath)