Ticket #693: trac_693-spawn-nb.patch

File trac_693-spawn-nb.patch, 2.4 KB (added by timdumol, 12 years ago)

Changes run_notebook.py to launch a browser to the notebook page should an instance in the directory exist. Apply to sagenb repo. Apply this patch only.

  • sagenb/notebook/run_notebook.py

    #693 Script to spawn a browser/start notebook
    diff -r 75eea8feabcd -r c655b708d90b sagenb/notebook/run_notebook.py
    a b  
    2020    protocol = 'ssl'
    2121
    2222# System libraries
    23 import getpass, os, shutil, socket
     23import getpass, os, shutil, socket, subprocess, errno
    2424
    2525from sagenb.misc.misc import (DOT_SAGENB, print_open_msg, find_next_available_port)
     26from sagenb.misc.misc import browser
    2627
    2728import notebook
    2829
     
    7576    else:
    7677        if isinstance(directory, basestring) and len(directory) > 0 and directory[-1] == "/":
    7778            directory = directory[:-1]
     79
     80    twistd_pid_path = os.path.join(directory + '.sagenb', 'twistd.pid')
     81   
     82    def remove_pidfile(path):
     83        """Ad-hoc function to remove the stale pidfile"""
     84        print 'Removing stale pidfile %s' % path
     85        os.remove(path)
     86
     87    def launch_browser_to_nb():
     88        """Ad-hoc function to launch browser to the notebook page"""
     89        subprocess.Popen(browser().split(' ') + ['http%s://%s:%d' % ('s' if secure else '', address,port)])
     90       
     91    if os.path.exists(twistd_pid_path):
     92        try:
     93            twistd_pid = int(open(twistd_pid_path).read())
     94        except:
     95            print 'Pidfile %s contains non-numeric value' % twistd_pid_path
     96            return
     97       
     98        try:
     99            # First check using /proc directory
     100            if os.path.exists('/proc/%d'  % twistd_pid):
     101                launch_browser_to_nb()
     102                return
     103            else:
     104                remove_pidfile(twistd_pid_path)               
     105        except:
     106            # Either we have no permission, or /proc does not exist (Windows), so
     107            # check with signal 0
     108            try:
     109                os.kill(twistd_pid, 0)
     110                # Process exists, proceed launch
     111                launch_browser_to_nb()
     112                return
     113            except OSError, why:
     114                if why[0] == errno.ESRCH:
     115                    # Process with pid does not exist
     116                    remove_pidfile(twistd_pid_path)
     117                else:
     118                    print("Could not check status of PID %s from pidfile %s: %s" %
     119                          (twistd_pid, twistd_pid_path, why[1]))
    78120           
    79121    # First change to the directory that contains the notebook directory
    80122    wd = os.path.split(directory)