예제 #1
0
파일: hg.py 프로젝트: regmi/femhub_notebook
 def serve(self, port=8200, address='localhost',
           open_viewer=True, options=''):
     """
     Start a web server for this repository.
     
     This server is very nice - you can browse all files in the
     repository, see their changelogs, see who wrote any given line,
     etc. Very nice.
     
     INPUT:
     
     
     -  ``port`` - port that the server will listen on
     
     -  ``address`` - (default: 'localhost') address to
        listen on
     
     -  ``open_viewer`` - boolean (default: True); whether
        to pop up the web page
     
     -  ``options`` - a string passed directly to hg's serve
        command.
     """
     if open_viewer:
         cmd = 'sleep 1; %s http://%s:%s 1>&2 >/dev/null'%(browser(),
                                                           address, port)
         t = tmp_filename()
         open(t,'w').write(cmd)
         P = os.path.abspath(t)
         os.system('chmod +x %s; %s &'%(P, P))
         
     print_open_msg(address, port)
     self('serve --address %s --port %s  %s'%(address, port, options))
     print_open_msg(address, port)            
예제 #2
0
 def serve(self, port=8200, address='localhost',
           open_viewer=True, options=''):
     """
     Start a web server for this repository.
     
     This server is very nice - you can browse all files in the
     repository, see their changelogs, see who wrote any given line,
     etc. Very nice.
     
     INPUT:
     
     
     -  ``port`` - port that the server will listen on
     
     -  ``address`` - (default: 'localhost') address to
        listen on
     
     -  ``open_viewer`` - boolean (default: True); whether
        to pop up the web page
     
     -  ``options`` - a string passed directly to hg's serve
        command.
     """
     if open_viewer:
         cmd = 'sleep 1; %s http://%s:%s 1>&2 >/dev/null'%(browser(),
                                                           address, port)
         t = tmp_filename()
         open(t,'w').write(cmd)
         P = os.path.abspath(t)
         os.system('chmod +x %s; %s &'%(P, P))
         
     print_open_msg(address, port)
     self('serve --address %s --port %s  %s'%(address, port, options))
     print_open_msg(address, port)            
예제 #3
0
 def run(port):
     print_open_msg(address, port)
     daemonize_str = '-n'
     if fork:
         daemonize_str = ''
     e = os.system('twistd %s moin -p %d -a "%s"' % (
             daemonize_str, port, address))
     if e:
         raise socket.error
예제 #4
0
    def run(port):
        ## Create the config file
        if secure:
            if not os.path.exists(private_pem) or not os.path.exists(public_pem):
                print "In order to use an SECURE encrypted notebook, you must first run notebook.setup()."
                print "Now running notebook.setup()"
                notebook_setup()
            if not os.path.exists(private_pem) or not os.path.exists(public_pem):
                print "Failed to setup notebook.  Please try notebook.setup() again manually."
            strport = "tls:%s:interface=%s:privateKey=%s:certKey=%s" % (port, address, private_pem, public_pem)
        else:
            strport = "tcp:%s:interface=%s" % (port, address)

        notebook_opts = '"%s",address="%s",port=%s,secure=%s' % (os.path.abspath(directory), address, port, secure)

        if open_viewer:
            if require_login:
                start_path = "'/?startup_token=%s' % startup_token"
            else:
                start_path = "'/'"
            open_page = "from sage.server.misc import open_page; open_page('%s', %s, %s, %s)" % (
                address,
                port,
                secure,
                start_path,
            )
        else:
            open_page = ""

        config = open(conf, "w")

        config.write(
            """
####################################################################
# WARNING -- Do not edit this file!   It is autogenerated each time
# the notebook(...) command is executed.
####################################################################

# Now set things up and start the notebook
import sage.server.notebook.notebook
sage.server.notebook.notebook.JSMATH=True
import sage.server.notebook.notebook as notebook
twist.notebook = notebook.load_notebook(%s)
twist.SAGETEX_PATH = "%s"
twist.OPEN_MODE = %s
twist.SID_COOKIE = str(hash("%s"))
twist.init_updates()
worksheet.init_sage_prestart(twist.notebook.get_server(), twist.notebook.get_ulimit())

def save_notebook():
    from twisted.internet.error import ReactorNotRunning
    print "Saving notebook..."
    twist.notebook.save()
    try:
        reactor.stop()
    except ReactorNotRunning:
        pass
    print "Notebook cleanly saved."

def my_sigint(x, n):
    save_notebook()
    signal.signal(signal.SIGINT, signal.SIG_DFL)


signal.signal(signal.SIGINT, my_sigint)

## Disable client-side certificate request for gnutls
try:
    import gnutls.connection
    gnutls.connection.CERT_REQUEST = 0
except OSError:
    print "Note: GNUTLS not available."


## Authentication framework (ported from Knooboo)


realm = avatars.LoginSystem()
p = portal.Portal(realm)
startup_token = '%%x' %% random.randint(0, 2**128)
startup_checker = avatars.OneTimeTokenChecker()
startup_checker.token = startup_token
p.registerChecker(startup_checker)
password_checker = avatars.PasswordChecker()
p.registerChecker(password_checker)
p.registerChecker(checkers.AllowAnonymousAccess())
rsrc = guard.MySessionWrapper(p)
log.DefaultCommonAccessLoggingObserver().start()
site = server.Site(rsrc)
factory = channel.HTTPFactory(site)

application = service.Application("SAGE Notebook")
s = strports.service('%s', factory)
%s
s.setServiceParent(application)

reactor.addSystemEventTrigger('before', 'shutdown', save_notebook)

"""
            % (notebook_opts, sagetex_path, not require_login, os.path.abspath(directory), strport, open_page)
        )

        config.close()

        ## Start up twisted
        if not quiet:
            print_open_msg("localhost" if not address else address, port, secure=secure)
        if secure and not quiet:
            print "There is an admin account.  If you do not remember the password,"
            print "quit the notebook and type notebook(reset=True)."
        cmd = 'sage -twistd --pidfile="%s"/twistd.pid -ny "%s"/twistedconf.tac' % (directory, directory)
        if fork:
            return pexpect.spawn(cmd)
        else:
            e = os.system(cmd)
        if e == 256:
            raise socket.error
        return True
예제 #5
0
    def run(port):
        ## Create the config file
        if secure:
            if not os.path.exists(private_pem) or not os.path.exists(
                    public_pem):
                print "In order to use an SECURE encrypted notebook, you must first run notebook.setup()."
                print "Now running notebook.setup()"
                notebook_setup()
            if not os.path.exists(private_pem) or not os.path.exists(
                    public_pem):
                print "Failed to setup notebook.  Please try notebook.setup() again manually."
            strport = 'tls:%s:interface=%s:privateKey=%s:certKey=%s' % (
                port, address, private_pem, public_pem)
        else:
            strport = 'tcp:%s:interface=%s' % (port, address)

        notebook_opts = '"%s",address="%s",port=%s,secure=%s' % (
            os.path.abspath(directory), address, port, secure)

        if open_viewer:
            if require_login:
                start_path = "'/?startup_token=%s' % startup_token"
            else:
                start_path = "'/'"
            open_page = "from sage.server.misc import open_page; open_page('%s', %s, %s, %s)" % (
                address, port, secure, start_path)
        else:
            open_page = ''

        config = open(conf, 'w')

        config.write("""
####################################################################
# WARNING -- Do not edit this file!   It is autogenerated each time
# the notebook(...) command is executed.
####################################################################

# Now set things up and start the notebook
import sage.server.notebook.notebook
sage.server.notebook.notebook.JSMATH=True
import sage.server.notebook.notebook as notebook
twist.notebook = notebook.load_notebook(%s)
twist.SAGETEX_PATH = "%s"
twist.OPEN_MODE = %s
twist.SID_COOKIE = str(hash("%s"))
twist.init_updates()
worksheet.init_sage_prestart(twist.notebook.get_server(), twist.notebook.get_ulimit())

def save_notebook():
    from twisted.internet.error import ReactorNotRunning
    print "Saving notebook..."
    twist.notebook.save()
    try:
        reactor.stop()
    except ReactorNotRunning:
        pass
    print "Notebook cleanly saved."

def my_sigint(x, n):
    save_notebook()
    signal.signal(signal.SIGINT, signal.SIG_DFL)

signal.signal(signal.SIGINT, my_sigint)

## Disable client-side certificate request for gnutls
try:
    import gnutls.connection
    gnutls.connection.CERT_REQUEST = 0
except OSError:
    print "Note: GNUTLS not available."

## Authentication framework (ported from Knooboo)

realm = avatars.LoginSystem()
p = portal.Portal(realm)
startup_token = '%%x' %% random.randint(0, 2**128)
startup_checker = avatars.OneTimeTokenChecker()
startup_checker.token = startup_token
p.registerChecker(startup_checker)
password_checker = avatars.PasswordChecker()
p.registerChecker(password_checker)
p.registerChecker(checkers.AllowAnonymousAccess())
rsrc = guard.MySessionWrapper(p)
log.DefaultCommonAccessLoggingObserver().start()
site = server.Site(rsrc)
factory = channel.HTTPFactory(site)

application = service.Application("SAGE Notebook")
s = strports.service('%s', factory)
%s
s.setServiceParent(application)

reactor.addSystemEventTrigger('before', 'shutdown', save_notebook)

""" % (notebook_opts, sagetex_path, not require_login,
        os.path.abspath(directory), strport, open_page))

        config.close()

        ## Start up twisted
        if not quiet:
            print_open_msg('localhost' if not address else address,
                           port,
                           secure=secure)
        if secure and not quiet:
            print "There is an admin account.  If you do not remember the password,"
            print "quit the notebook and type notebook(reset=True)."
        cmd = 'sage -twistd --pidfile="%s"/twistd.pid -ny "%s"/twistedconf.tac' % (
            directory, directory)
        if fork:
            return pexpect.spawn(cmd)
        else:
            e = os.system(cmd)
        if e == 256:
            raise socket.error
        return True