<s> import os <EOL> from django . http import HttpResponseRedirect <EOL> from django . conf import settings <EOL> import urllib2 <EOL> import sh <EOL> import time <EOL> import shutil <EOL> def run_notebook ( ipython_arguments ) : <EOL> ipython_arguments . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> sh . ipython ( '<STR_LIT>' , * ipython_arguments , _bg = True , _env = { <EOL> '<STR_LIT>' : settings . PROJECT_ROOT , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : os . path . join ( os . environ [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> } ) <EOL> def shared_notebook ( request , * args , ** kwargs ) : <EOL> notebook_settings = list ( settings . IPYTHON_SETTINGS ) <EOL> user_directory = os . path . join ( settings . IPYTHON_BASE , '<STR_LIT>' ) <EOL> port = <NUM_LIT> <EOL> hostname = settings . IPYTHON_HOST <EOL> target_url = '<STR_LIT>' . format ( hostname = hostname , port = port ) <EOL> notebook_settings . append ( '<STR_LIT>' . format ( user_path = user_directory ) ) <EOL> notebook_settings . append ( '<STR_LIT>' . format ( user_path = user_directory ) ) <EOL> notebook_settings . append ( '<STR_LIT>' . format ( port = port ) ) <EOL> notebook_settings . append ( '<STR_LIT>' ) <EOL> notebook_settings . append ( '<STR_LIT>' ) <EOL> if not os . path . exists ( settings . IPYTHON_BASE ) : <EOL> os . mkdir ( settings . IPYTHON_BASE ) <EOL> if not os . path . exists ( user_directory ) : <EOL> os . mkdir ( user_directory ) <EOL> os . mkdir ( os . path . join ( user_directory , '<STR_LIT>' ) ) <EOL> try : <EOL> urllib2 . urlopen ( target_url ) . read ( ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> run_notebook ( notebook_settings ) <EOL> starting = <NUM_LIT:5> <EOL> while starting : <EOL> try : <EOL> urllib2 . urlopen ( target_url ) . read ( ) <EOL> return HttpResponseRedirect ( target_url ) <EOL> except : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> starting -= <NUM_LIT:1> <EOL> def notebook_for_user ( request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user = request . user <EOL> notebook_settings = list ( settings . IPYTHON_SETTINGS ) <EOL> user_directory = os . path . join ( settings . IPYTHON_BASE , user . email ) <EOL> port = <NUM_LIT> + user . pk <EOL> hostname = '<STR_LIT:127.0.0.1>' <EOL> target_url = '<STR_LIT>' . format ( hostname = hostname , port = port ) <EOL> if user . is_authenticated ( ) : <EOL> notebook_settings . append ( '<STR_LIT>' . format ( user_path = user_directory ) ) <EOL> notebook_settings . append ( '<STR_LIT>' . format ( user_path = user_directory ) ) <EOL> notebook_settings . append ( '<STR_LIT>' . format ( port = port ) ) <EOL> notebook_settings . append ( '<STR_LIT>' ) <EOL> notebook_settings . append ( '<STR_LIT>' ) <EOL> if not os . path . exists ( user_directory ) : <EOL> os . mkdir ( user_directory ) <EOL> os . mkdir ( os . path . join ( user_directory , user . email ) ) <EOL> try : <EOL> urllib2 . urlopen ( target_url ) . read ( ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> run_notebook ( notebook_settings ) <EOL> starting = <NUM_LIT:5> <EOL> while starting : <EOL> try : <EOL> urllib2 . urlopen ( target_url ) . read ( ) <EOL> return HttpResponseRedirect ( target_url ) <EOL> except : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> starting -= <NUM_LIT:1> </s>
