<s> __author__ = '<STR_LIT>' <EOL> import json <EOL> from django . conf import settings <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . http import HttpResponseNotFound <EOL> from django . shortcuts import redirect , render <EOL> from django . views . decorators . http import require_POST <EOL> from ide . api import json_response , json_failure <EOL> import requests <EOL> import random <EOL> import urlparse <EOL> import urllib <EOL> import string <EOL> from utils . redis_helper import redis_client <EOL> @ login_required <EOL> @ require_POST <EOL> def launch_emulator ( request ) : <EOL> user_id = request . user . id <EOL> platform = request . POST [ '<STR_LIT>' ] <EOL> oauth = request . POST [ '<STR_LIT>' ] <EOL> tz_offset = request . POST [ '<STR_LIT>' ] <EOL> versions = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> version = versions [ platform ] <EOL> redis_key = '<STR_LIT>' % ( user_id , platform ) <EOL> qemu_instance = redis_client . get ( redis_key ) <EOL> if qemu_instance is not None : <EOL> qemu_instance = json . loads ( qemu_instance ) <EOL> try : <EOL> response = requests . post ( qemu_instance [ '<STR_LIT>' ] , timeout = <NUM_LIT:2> , verify = settings . COMPLETION_CERTS ) <EOL> response . raise_for_status ( ) <EOL> response = response . json ( ) <EOL> except ( requests . RequestException , ValueError ) as e : <EOL> print "<STR_LIT>" % e <EOL> pass <EOL> else : <EOL> if response . get ( '<STR_LIT>' , False ) : <EOL> return json_response ( qemu_instance ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> token = _generate_token ( ) <EOL> servers = set ( settings . QEMU_URLS ) <EOL> while len ( servers ) > <NUM_LIT:0> : <EOL> server = random . choice ( list ( servers ) ) <EOL> servers . remove ( server ) <EOL> try : <EOL> result = requests . post ( server + '<STR_LIT>' , <EOL> data = { '<STR_LIT>' : token , <EOL> '<STR_LIT>' : platform , <EOL> '<STR_LIT:version>' : version , <EOL> '<STR_LIT>' : oauth , <EOL> '<STR_LIT>' : tz_offset } , <EOL> headers = { '<STR_LIT>' : settings . QEMU_LAUNCH_AUTH_HEADER } , <EOL> timeout = settings . QEMU_LAUNCH_TIMEOUT , <EOL> verify = settings . COMPLETION_CERTS ) <EOL> result . raise_for_status ( ) <EOL> response = result . json ( ) <EOL> url = urlparse . urlsplit ( server ) <EOL> response [ '<STR_LIT:host>' ] = url . hostname <EOL> response [ '<STR_LIT>' ] = ( url . scheme == '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = url . port or ( <NUM_LIT> if url . scheme == '<STR_LIT>' else <NUM_LIT> ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % ( server , response [ '<STR_LIT>' ] ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % ( server , response [ '<STR_LIT>' ] ) <EOL> response [ '<STR_LIT>' ] = token <EOL> redis_client . set ( redis_key , json . dumps ( response ) ) <EOL> return json_response ( response ) <EOL> except requests . HTTPError as e : <EOL> print e . response . text <EOL> except ( requests . RequestException , ValueError ) as e : <EOL> print e <EOL> pass <EOL> return json_failure ( "<STR_LIT>" ) <EOL> @ login_required <EOL> @ require_POST <EOL> def generate_phone_token ( request , emulator_id ) : <EOL> phone_token = random . randint ( <NUM_LIT> , <NUM_LIT> ) <EOL> token = request . POST [ '<STR_LIT>' ] <EOL> url = request . POST [ '<STR_LIT:url>' ] <EOL> redis_key = '<STR_LIT>' % phone_token <EOL> redis_client . set ( redis_key , json . dumps ( { '<STR_LIT>' : emulator_id , '<STR_LIT>' : token , '<STR_LIT:url>' : url } ) , ex = <NUM_LIT> ) <EOL> return json_response ( { '<STR_LIT>' : phone_token } ) <EOL> def handle_phone_token ( request , token ) : <EOL> redis_key = '<STR_LIT>' % token <EOL> qemu = redis_client . get ( redis_key ) <EOL> if qemu is not None : <EOL> qemu = json . loads ( qemu ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT:url>' : qemu [ '<STR_LIT:url>' ] , <EOL> '<STR_LIT>' : qemu [ '<STR_LIT>' ] , <EOL> } ) <EOL> else : <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : True <EOL> } ) <EOL> def _generate_token ( ) : <EOL> rng = random . SystemRandom ( ) <EOL> valid = string . ascii_letters + string . digits + string . punctuation <EOL> return '<STR_LIT>' . join ( rng . choice ( valid ) for i in xrange ( <NUM_LIT:30> ) ) </s>
