def handle_pong(self, msg):
     "a heart just beat"
     current = str_to_bytes(str(self.lifetime))
     last = str_to_bytes(str(self.last_ping))
     if msg[1] == current:
         delta = time.time()-self.tic
         # self.log.debug("heartbeat::heart %r took %.2f ms to respond"%(msg[0], 1000*delta))
         self.responses.add(msg[0])
     elif msg[1] == last:
         delta = time.time()-self.tic + (self.lifetime-self.last_ping)
         self.log.warn("heartbeat::heart %r missed a beat, and took %.2f ms to respond", msg[0], 1000*delta)
         self.responses.add(msg[0])
     else:
         self.log.warn("heartbeat::got bad heartbeat (possibly old?): %s (current=%.3f)", msg[1], self.lifetime)
Ejemplo n.º 2
0
def restore_bytes(nb):
    """Restore bytes of image data from unicode-only formats.
    
    Base64 encoding is handled elsewhere.  Bytes objects in the notebook are
    always b64-encoded. We DO NOT encode/decode around file formats.
    """
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type == 'code':
                for output in cell.outputs:
                    if 'png' in output:
                        output.png = str_to_bytes(output.png, 'ascii')
                    if 'jpeg' in output:
                        output.jpeg = str_to_bytes(output.jpeg, 'ascii')
    return nb
Ejemplo n.º 3
0
def restore_bytes(nb):
    """Restore bytes of image data from unicode-only formats.

    Base64 encoding is handled elsewhere.  Bytes objects in the notebook are
    always b64-encoded. We DO NOT encode/decode around file formats.
    """
    for ws in nb.worksheets:
        for cell in ws.cells:
            if cell.cell_type == 'code':
                for output in cell.outputs:
                    if 'png' in output:
                        output.png = str_to_bytes(output.png, 'ascii')
                    if 'jpeg' in output:
                        output.jpeg = str_to_bytes(output.jpeg, 'ascii')
    return nb
Ejemplo n.º 4
0
    def beat(self):
        self.pongstream.flush()
        self.last_ping = self.lifetime

        toc = time.time()
        self.lifetime += toc-self.tic
        self.tic = toc
        if self.debug:
            self.log.debug("heartbeat::sending %s", self.lifetime)
        goodhearts = self.hearts.intersection(self.responses)
        missed_beats = self.hearts.difference(goodhearts)
        newhearts = self.responses.difference(goodhearts)
        for heart in newhearts:
            self.handle_new_heart(heart)
        heartfailures, on_probation = self._check_missed(missed_beats, self.on_probation,
                                                         self.hearts)
        for failure in heartfailures:
            self.handle_heart_failure(failure)
        self.on_probation = on_probation
        self.responses = set()
        #print self.on_probation, self.hearts
        # self.log.debug("heartbeat::beat %.3f, %i beating hearts", self.lifetime, len(self.hearts))
        self.pingstream.send(str_to_bytes(str(self.lifetime)))
        # flush stream to force immediate socket send
        self.pingstream.flush()
Ejemplo n.º 5
0
    def load_connection_file(self):
        """load ip/port/hmac config from JSON connection file"""
        # this is identical to IPKernelApp.load_connection_file
        # perhaps it can be centralized somewhere?
        try:
            fname = filefind(
                self.connection_file, ['.', self.profile_dir.security_dir])
        except IOError:
            self.log.debug(
                "Connection File not found: %s", self.connection_file)
            return
        self.log.debug(u"Loading connection file %s", fname)
        with open(fname) as f:
            cfg = json.load(f)
        self.transport = cfg.get('transport', 'tcp')
        self.ip = cfg.get('ip', localhost())

        for channel in ('hb', 'shell', 'iopub', 'stdin', 'control'):
            name = channel + '_port'
            if getattr(self, name) == 0 and name in cfg:
                # not overridden by config or cl_args
                setattr(self, name, cfg[name])
        if 'key' in cfg:
            self.config.Session.key = str_to_bytes(cfg['key'])
        if 'signature_scheme' in cfg:
            self.config.Session.signature_scheme = cfg['signature_scheme']
 def sign(self, msg):
     if self.auth is None:
         return b''
     h = self.auth.copy()
     for m in msg:
         h.update(m)
     return str_to_bytes(h.hexdigest())
Ejemplo n.º 7
0
    def load_connection_file(self):
        """load ip/port/hmac config from JSON connection file"""
        # this is identical to IPKernelApp.load_connection_file
        # perhaps it can be centralized somewhere?
        try:
            fname = filefind(self.connection_file,
                             ['.', self.profile_dir.security_dir])
        except IOError:
            self.log.debug("Connection File not found: %s",
                           self.connection_file)
            return
        self.log.debug(u"Loading connection file %s", fname)
        with open(fname) as f:
            cfg = json.load(f)

        self.config.KernelManager.transport = cfg.get('transport', 'tcp')
        self.config.KernelManager.ip = cfg.get('ip', LOCALHOST)

        for channel in ('hb', 'shell', 'iopub', 'stdin'):
            name = channel + '_port'
            if getattr(self, name) == 0 and name in cfg:
                # not overridden by config or cl_args
                setattr(self, name, cfg[name])
        if 'key' in cfg:
            self.config.Session.key = str_to_bytes(cfg['key'])
        if 'signature_scheme' in cfg:
            self.config.Session.signature_scheme = cfg['signature_scheme']
 def sign(self, msg):
     if self.auth is None:
         return b''
     h = self.auth.copy()
     for m in msg:
         h.update(m)
     return str_to_bytes(h.hexdigest())
Ejemplo n.º 9
0
def get_connection_info(connection_file=None, unpack=False, profile=None):
    """Return the connection information for the current Kernel.

    Parameters
    ----------
    connection_file : str [optional]
        The connection file to be used. Can be given by absolute path, or
        IPython will search in the security directory of a given profile.
        If run from IPython,

        If unspecified, the connection file for the currently running
        IPython Kernel will be used, which is only allowed from inside a kernel.
    unpack : bool [default: False]
        if True, return the unpacked dict, otherwise just the string contents
        of the file.
    profile : DEPRECATED

    Returns
    -------
    The connection dictionary of the current kernel, as string or dict,
    depending on `unpack`.
    """
    cf = _find_connection_file(connection_file, profile)

    with open(cf) as f:
        info = f.read()

    if unpack:
        info = json.loads(info)
        # ensure key is bytes:
        info['key'] = str_to_bytes(info.get('key', ''))
    return info
Ejemplo n.º 10
0
    def beat(self):
        self.pongstream.flush()
        self.last_ping = self.lifetime

        toc = time.time()
        self.lifetime += toc - self.tic
        self.tic = toc
        if self.debug:
            self.log.debug("heartbeat::sending %s", self.lifetime)
        goodhearts = self.hearts.intersection(self.responses)
        missed_beats = self.hearts.difference(goodhearts)
        newhearts = self.responses.difference(goodhearts)
        for heart in newhearts:
            self.handle_new_heart(heart)
        heartfailures, on_probation = self._check_missed(
            missed_beats, self.on_probation, self.hearts)
        for failure in heartfailures:
            self.handle_heart_failure(failure)
        self.on_probation = on_probation
        self.responses = set()
        #print self.on_probation, self.hearts
        # self.log.debug("heartbeat::beat %.3f, %i beating hearts", self.lifetime, len(self.hearts))
        self.pingstream.send(str_to_bytes(str(self.lifetime)))
        # flush stream to force immediate socket send
        self.pingstream.flush()
Ejemplo n.º 11
0
 def __getitem__(self,attr):
      try:
           return pickle.loads(
                bytes_to_str(base64.b64decode(str_to_bytes(
                     PermanentStorage._data[attr]))))
      except TypeError as e:
           return "TypeError: "+str(e)
Ejemplo n.º 12
0
def compute_checksum(cell):
    m = hashlib.md5()

    # fix minor whitespace issues that might have been added and then
    # add cell contents
    m.update(str_to_bytes(autopep8.fix_code(cell.source).rstrip()))

    # include number of points that the cell is worth
    if 'points' in cell.metadata.nbgrader:
        m.update(str_to_bytes(str(float(cell.metadata.nbgrader['points']))))

    # include the grade_id
    if 'grade_id' in cell.metadata.nbgrader:
        m.update(str_to_bytes(cell.metadata.nbgrader['grade_id']))

    return m.hexdigest()
Ejemplo n.º 13
0
 def load_connection_file(self):
     """load ip/port/hmac config from JSON connection file"""
     try:
         fname = filefind(self.connection_file,
                          ['.', self.profile_dir.security_dir])
     except IOError:
         self.log.debug("Connection file not found: %s",
                        self.connection_file)
         # This means I own it, so I will clean it up:
         atexit.register(self.cleanup_connection_file)
         return
     self.log.debug(u"Loading connection file %s", fname)
     with open(fname) as f:
         s = f.read()
     cfg = json.loads(s)
     self.transport = cfg.get('transport', self.transport)
     if self.ip == self._ip_default() and 'ip' in cfg:
         # not overridden by config or cl_args
         self.ip = cfg['ip']
     for channel in ('hb', 'shell', 'iopub', 'stdin'):
         name = channel + '_port'
         if getattr(self, name) == 0 and name in cfg:
             # not overridden by config or cl_args
             setattr(self, name, cfg[name])
     if 'key' in cfg:
         self.config.Session.key = str_to_bytes(cfg['key'])
Ejemplo n.º 14
0
def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'

    fmt = {
        'image/png': pngdata,
        'image/jpeg': jpegdata,
        'application/pdf': pdfdata
    }
    encoded = encode_images(fmt)
    for key, value in iteritems(fmt):
        # encoded has unicode, want bytes
        decoded = decodestring(encoded[key].encode('ascii'))
        nt.assert_equal(decoded, value)
    encoded2 = encode_images(encoded)
    nt.assert_equal(encoded, encoded2)

    b64_str = {}
    for key, encoded in iteritems(encoded):
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = encode_images(b64_str)
    nt.assert_equal(encoded3, b64_str)
    for key, value in iteritems(fmt):
        # encoded3 has str, want bytes
        decoded = decodestring(str_to_bytes(encoded3[key]))
        nt.assert_equal(decoded, value)
Ejemplo n.º 15
0
def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'
    
    fmt = {
        'image/png'  : pngdata,
        'image/jpeg' : jpegdata,
        'application/pdf' : pdfdata
    }
    encoded = encode_images(fmt)
    for key, value in iteritems(fmt):
        # encoded has unicode, want bytes
        decoded = decodestring(encoded[key].encode('ascii'))
        nt.assert_equal(decoded, value)
    encoded2 = encode_images(encoded)
    nt.assert_equal(encoded, encoded2)
    
    b64_str = {}
    for key, encoded in iteritems(encoded):
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = encode_images(b64_str)
    nt.assert_equal(encoded3, b64_str)
    for key, value in iteritems(fmt):
        # encoded3 has str, want bytes
        decoded = decodestring(str_to_bytes(encoded3[key]))
        nt.assert_equal(decoded, value)
Ejemplo n.º 16
0
 def load_connection_file(self):
     """load ip/port/hmac config from JSON connection file"""
     try:
         fname = filefind(
             self.connection_file, ['.', self.profile_dir.security_dir])
     except IOError:
         self.log.debug(
             "Connection file not found: %s", self.connection_file)
         # This means I own it, so I will clean it up:
         atexit.register(self.cleanup_connection_file)
         return
     self.log.debug(u"Loading connection file %s", fname)
     with open(fname) as f:
         s = f.read()
     cfg = json.loads(s)
     self.transport = cfg.get('transport', self.transport)
     if self.ip == self._ip_default() and 'ip' in cfg:
         # not overridden by config or cl_args
         self.ip = cfg['ip']
     for channel in ('hb', 'shell', 'iopub', 'stdin', 'control'):
         name = channel + '_port'
         if getattr(self, name) == 0 and name in cfg:
             # not overridden by config or cl_args
             setattr(self, name, cfg[name])
     if 'key' in cfg:
         self.config.Session.key = str_to_bytes(cfg['key'])
 def load_connection_file(self):
     """load ip/port/hmac config from JSON connection file"""
     # this is identical to KernelApp.load_connection_file
     # perhaps it can be centralized somewhere?
     try:
         fname = filefind(self.connection_file,
                          ['.', self.profile_dir.security_dir])
     except IOError:
         self.log.debug("Connection File not found: %s",
                        self.connection_file)
         return
     self.log.debug("Loading connection file %s", fname)
     with open(fname) as f:
         s = f.read()
     cfg = json.loads(s)
     if self.ip == LOCALHOST and 'ip' in cfg:
         # not overridden by config or cl_args
         self.ip = cfg['ip']
     for channel in ('hb', 'shell', 'iopub', 'stdin'):
         name = channel + '_port'
         if getattr(self, name) == 0 and name in cfg:
             # not overridden by config or cl_args
             setattr(self, name, cfg[name])
     if 'key' in cfg:
         self.config.Session.key = str_to_bytes(cfg['key'])
Ejemplo n.º 18
0
def ipexec(fname, options=None, commands=()):
    """Utility to call 'ipython filename'.

    Starts IPython with a minimal and safe configuration to make startup as fast
    as possible.

    Note that this starts IPython in a subprocess!

    Parameters
    ----------
    fname : str
      Name of file to be executed (should have .py or .ipy extension).

    options : optional, list
      Extra command-line flags to be passed to IPython.

    commands : optional, list
      Commands to send in on stdin

    Returns
    -------
    (stdout, stderr) of ipython subprocess.
    """
    if options is None:
        options = []

    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [
        '--PromptManager.in_template=""',
        '--PromptManager.in2_template=""',
        '--PromptManager.out_template=""',
    ]
    cmdargs = default_argv() + prompt_opts + options

    test_dir = os.path.dirname(__file__)

    ipython_cmd = get_ipython_cmd()
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = ipython_cmd + cmdargs + [full_fname]
    env = os.environ.copy()
    # FIXME: ignore all warnings in ipexec while we have shims
    # should we keep suppressing warnings here, even after removing shims?
    env["PYTHONWARNINGS"] = "ignore"
    # env.pop('PYTHONWARNINGS', None)  # Avoid extraneous warnings appearing on stderr
    for k, v in env.items():
        # Debug a bizarre failure we've seen on Windows:
        # TypeError: environment can only contain strings
        if not isinstance(v, str):
            print(k, v)
    p = Popen(full_cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, env=env)
    out, err = p.communicate(input=py3compat.str_to_bytes("\n".join(commands)) or None)
    out, err = py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
    # `import readline` causes 'ESC[?1034h' to be output sometimes,
    # so strip that out before doing comparisons
    if out:
        out = re.sub(r"\x1b\[[^h]+h", "", out)
    return out, err
Ejemplo n.º 19
0
def filehash(path):
    """Make an MD5 hash of a file, ignoring any differences in line
    ending characters."""
    warn("filehash() is deprecated since IPython 4.0",
         DeprecationWarning,
         stacklevel=2)
    with open(path, "rU") as f:
        return md5(py3compat.str_to_bytes(f.read())).hexdigest()
Ejemplo n.º 20
0
def create_new_code_cell(code):
    """Javascript to create and populate a new code cell in the notebook"""
    encoded_code = bytes_to_str(b64encode(str_to_bytes(code)))
    display(
        Javascript("""
        var code_cell = IPython.notebook.insert_cell_below('code');
        code_cell.set_text(atob("{0}"));
    """.format(encoded_code)))
Ejemplo n.º 21
0
 def __getitem__(self, attr):
     try:
         return pickle.loads(
             bytes_to_str(
                 base64.b64decode(
                     str_to_bytes(PermanentStorage._data[attr]))))
     except TypeError as e:
         return "TypeError: " + str(e)
Ejemplo n.º 22
0
def ipexec(fname, options=None, commands=()):
    """Utility to call 'ipython filename'.

    Starts IPython with a minimal and safe configuration to make startup as fast
    as possible.

    Note that this starts IPython in a subprocess!

    Parameters
    ----------
    fname : str
      Name of file to be executed (should have .py or .ipy extension).

    options : optional, list
      Extra command-line flags to be passed to IPython.

    commands : optional, list
      Commands to send in on stdin

    Returns
    -------
    (stdout, stderr) of ipython subprocess.
    """
    if options is None: options = []

    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [
        '--PromptManager.in_template=""', '--PromptManager.in2_template=""',
        '--PromptManager.out_template=""'
    ]
    cmdargs = default_argv() + prompt_opts + options

    test_dir = os.path.dirname(__file__)

    ipython_cmd = get_ipython_cmd()
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = ipython_cmd + cmdargs + [full_fname]
    env = os.environ.copy()
    # FIXME: ignore all warnings in ipexec while we have shims
    # should we keep suppressing warnings here, even after removing shims?
    env['PYTHONWARNINGS'] = 'ignore'
    # env.pop('PYTHONWARNINGS', None)  # Avoid extraneous warnings appearing on stderr
    for k, v in env.items():
        # Debug a bizarre failure we've seen on Windows:
        # TypeError: environment can only contain strings
        if not isinstance(v, str):
            print(k, v)
    p = Popen(full_cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, env=env)
    out, err = p.communicate(
        input=py3compat.str_to_bytes('\n'.join(commands)) or None)
    out, err = py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
    # `import readline` causes 'ESC[?1034h' to be output sometimes,
    # so strip that out before doing comparisons
    if out:
        out = re.sub(r'\x1b\[[^h]+h', '', out)
    return out, err
Ejemplo n.º 23
0
def test_write_connection_file():
    with TemporaryDirectory() as d:
        cf = os.path.join(d, 'kernel.json')
        connect.write_connection_file(cf, **sample_info)
        nt.assert_true(os.path.exists(cf))
        with open(cf, 'r') as f:
            info = json.load(f)
    info['key'] = str_to_bytes(info['key'])
    nt.assert_equal(info, sample_info)
Ejemplo n.º 24
0
def test_write_connection_file():
    with TemporaryDirectory() as d:
        cf = os.path.join(d, 'kernel.json')
        connect.write_connection_file(cf, **sample_info)
        nt.assert_true(os.path.exists(cf))
        with open(cf, 'r') as f:
            info = json.load(f)
    info['key'] = str_to_bytes(info['key'])
    nt.assert_equal(info, sample_info)
Ejemplo n.º 25
0
 def __setitem__(self, attr, value):
     """Set property in the metadata"""
     pick = bytes_to_str(
         base64.b64encode(str_to_bytes(pickle.dumps(value))))
     name = attr
     PermanentStorage._data[attr] = pick
     display(
         Javascript('%s["%s"]="%s";console.log("Setting %s");' %
                    (self.__storePath, name, pick, name)))
Ejemplo n.º 26
0
 def __setitem__(self,attr,value):
     """Set property in the metadata"""
     pick=bytes_to_str(base64.b64encode(str_to_bytes(pickle.dumps(value))))
     name=attr
     PermanentStorage._data[attr]=pick
     display(Javascript('%s["%s"]="%s";console.log("Setting %s");' % (self.__storePath,
                                          name,
                                          pick,
                                          name)));
Ejemplo n.º 27
0
def filehash(path):
    """Make an MD5 hash of a file, ignoring any differences in line
    ending characters."""
    warn(
        "filehash() is deprecated since IPython 4.0",
        DeprecationWarning,
        stacklevel=2)
    with open(path, "rU") as f:
        return md5(py3compat.str_to_bytes(f.read())).hexdigest()
Ejemplo n.º 28
0
    def load_connection_file(self):
        """Load connection info from JSON dict in self.connection_file."""
        with open(self.connection_file) as f:
            cfg = json.loads(f.read())

        self.transport = cfg.get('transport', 'tcp')
        self.ip = cfg['ip']
        for name in port_names:
            setattr(self, name, cfg[name])
        self.session.key = str_to_bytes(cfg['key'])
Ejemplo n.º 29
0
 def load_connection_file(self):
     """load connection info from JSON dict in self.connection_file"""
     with open(self.connection_file) as f:
         cfg = json.loads(f.read())
     
     self.ip = cfg['ip']
     self.shell_port = cfg['shell_port']
     self.stdin_port = cfg['stdin_port']
     self.iopub_port = cfg['iopub_port']
     self.hb_port = cfg['hb_port']
     self.session.key = str_to_bytes(cfg['key'])
Ejemplo n.º 30
0
    def load_connection_file(self):
        """load connection info from JSON dict in self.connection_file"""
        with open(self.connection_file) as f:
            cfg = json.loads(f.read())

        self.ip = cfg['ip']
        self.shell_port = cfg['shell_port']
        self.stdin_port = cfg['stdin_port']
        self.iopub_port = cfg['iopub_port']
        self.hb_port = cfg['hb_port']
        self.session.key = str_to_bytes(cfg['key'])
Ejemplo n.º 31
0
def default_secure(cfg):
    """Set the default behavior for a config environment to be secure.
    
    If Session.key/keyfile have not been set, set Session.key to
    a new random UUID.
    """

    if 'Session' in cfg:
        if 'key' in cfg.Session or 'keyfile' in cfg.Session:
            return
    # key/keyfile not specified, generate new UUID:
    cfg.Session.key = str_to_bytes(str(uuid.uuid4()))
Ejemplo n.º 32
0
def default_secure(cfg):
    """Set the default behavior for a config environment to be secure.

    If Session.key/keyfile have not been set, set Session.key to
    a new random UUID.
    """

    if 'Session' in cfg:
        if 'key' in cfg.Session or 'keyfile' in cfg.Session:
            return
    # key/keyfile not specified, generate new UUID:
    cfg.Session.key = str_to_bytes(str(uuid.uuid4()))
Ejemplo n.º 33
0
def create_code_cell(code: str = '', where: CellLocation = 'below'):
    """
    Create a code cell in the IPython Notebook

    :param code: code to fill the new code cell with.
    :param where: where to add the new code cell
    """
    encoded_code = bytes_to_str(base64.b64encode(str_to_bytes(code)))
    display(Javascript('''
        var code = IPython.notebook.insert_cell_{0}('code');
        code.set_text(atob("{1}"));
    '''.format(where, encoded_code)))
Ejemplo n.º 34
0
def test_get_connection_info():
    with TemporaryDirectory() as d:
        cf = os.path.join(d, 'kernel.json')
        connect.write_connection_file(cf, **sample_info)
        json_info = connect.get_connection_info(cf)
        info = connect.get_connection_info(cf, unpack=True)
    
    nt.assert_equal(type(json_info), type(""))
    nt.assert_equal(info, sample_info)
    
    info2 = json.loads(json_info)
    info2['key'] = str_to_bytes(info2['key'])
    nt.assert_equal(info2, sample_info)
Ejemplo n.º 35
0
def test_get_connection_info():
    with TemporaryDirectory() as d:
        cf = os.path.join(d, 'kernel.json')
        connect.write_connection_file(cf, **sample_info)
        json_info = connect.get_connection_info(cf)
        info = connect.get_connection_info(cf, unpack=True)
    
    nt.assert_equal(type(json_info), type(""))
    nt.assert_equal(info, sample_info)
    
    info2 = json.loads(json_info)
    info2['key'] = str_to_bytes(info2['key'])
    nt.assert_equal(info2, sample_info)
Ejemplo n.º 36
0
    def load_connection_file(self):
        """Load connection info from JSON dict in self.connection_file."""
        with open(self.connection_file) as f:
            cfg = json.loads(f.read())

        self.transport = cfg.get('transport', 'tcp')
        self.ip = cfg['ip']
        for name in port_names:
            setattr(self, name, cfg[name])
        if 'key' in cfg:
            self.session.key = str_to_bytes(cfg['key'])
        if cfg.get('signature_scheme'):
            self.session.signature_scheme = cfg['signature_scheme']
Ejemplo n.º 37
0
def signup():
    form = SignUpForm(request.form, csrf_enabled=False)
    if request.method == 'POST':
        if form.validate():

            username = str(form.username.data)
            email = str(form.email.data)
            password = str(form.password.data)

            #curr user count
            curr_user_count = models.User.query.count()

            if curr_user_count >= 15:
                flash(
                    'Your company can only create 15 users, please contact us if you have any question'
                )
                return render_template('index.html')

            # get the next server port
            port = 9499 + curr_user_count + 1
            #create passwd
            h = hashlib.new(algorithm)
            salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(
                4 * salt_len)
            h.update(
                cast_bytes(password, 'utf-8') + str_to_bytes(salt, 'ascii'))
            hashed_password = '******'.join((algorithm, salt, h.hexdigest()))

            # create user
            user = models.User()
            user.username = username
            user.email = email
            user.password = hashed_password
            user.nbserver_port = port
            user = db.session.merge(user)
            db.session.commit()

            # create the user data directory hierarchy
            if not os.path.exists('{0}/{1}'.format(DATA_DIR, username)):
                create_user_dir(username, hashed_password)

            return redirect('/login')
        else:
            errorMsg = ''
            for key in form.errors:
                errorMsg = errorMsg + key + ':' + form.errors[key][0] + '      '

            flash(errorMsg)
            return render_template('index.html')

    return render_template('index.html')
Ejemplo n.º 38
0
    def sign(self, msg_list):
        """Sign a message with HMAC digest. If no auth, return b''.

        Parameters
        ----------
        msg_list : list
            The [p_header,p_parent,p_content] part of the message list.
        """
        if self.auth is None:
            return b''
        h = self.auth.copy()
        for m in msg_list:
            h.update(m)
        return str_to_bytes(h.hexdigest())
Ejemplo n.º 39
0
    def load_connection_file(self):
        """Load connection info from JSON dict in self.connection_file."""
        with open(self.connection_file) as f:
            cfg = json.loads(f.read())

        from pprint import pprint
        pprint(cfg)
        self.transport = cfg.get('transport', 'tcp')
        self.ip = cfg['ip']
        self.shell_port = cfg['shell_port']
        self.stdin_port = cfg['stdin_port']
        self.iopub_port = cfg['iopub_port']
        self.hb_port = cfg['hb_port']
        self.session.key = str_to_bytes(cfg['key'])
Ejemplo n.º 40
0
    def sign(self, msg_list):
        """Sign a message with HMAC digest. If no auth, return b''.

        Parameters
        ----------
        msg_list : list
            The [p_header,p_parent,p_content] part of the message list.
        """
        if self.auth is None:
            return b''
        h = self.auth.copy()
        for m in msg_list:
            h.update(m)
        return str_to_bytes(h.hexdigest())
Ejemplo n.º 41
0
def main(connection_file):
    """watch iopub channel, and print messages"""

    ctx = zmq.Context.instance()

    with open(connection_file) as f:
        cfg = json.loads(f.read())

    location = cfg['location']
    reg_url = cfg['url']
    session = Session(key=str_to_bytes(cfg['exec_key']))

    query = ctx.socket(zmq.DEALER)
    query.connect(disambiguate_url(cfg['url'], location))
    session.send(query, "connection_request")
    idents, msg = session.recv(query, mode=0)
    c = msg['content']
    iopub_url = disambiguate_url(c['iopub'], location)
    sub = ctx.socket(zmq.SUB)
    # This will subscribe to all messages:
    sub.setsockopt(zmq.SUBSCRIBE, b'')
    # replace with b'' with b'engine.1.stdout' to subscribe only to engine 1's stdout
    # 0MQ subscriptions are simple 'foo*' matches, so 'engine.1.' subscribes
    # to everything from engine 1, but there is no way to subscribe to
    # just stdout from everyone.
    # multiple calls to subscribe will add subscriptions, e.g. to subscribe to
    # engine 1's stderr and engine 2's stdout:
    # sub.setsockopt(zmq.SUBSCRIBE, b'engine.1.stderr')
    # sub.setsockopt(zmq.SUBSCRIBE, b'engine.2.stdout')
    sub.connect(iopub_url)
    while True:
        try:
            idents, msg = session.recv(sub, mode=0)
        except KeyboardInterrupt:
            return
        # ident always length 1 here
        topic = idents[0]
        if msg['msg_type'] == 'stream':
            # stdout/stderr
            # stream names are in msg['content']['name'], if you want to handle
            # them differently
            print("%s: %s" % (topic, msg['content']['data']))
        elif msg['msg_type'] == 'pyerr':
            # Python traceback
            c = msg['content']
            print(topic + ':')
            for line in c['traceback']:
                # indent lines
                print('    ' + line)
Ejemplo n.º 42
0
def ipexec(fname, options=None, commands=()):
    """Utility to call 'ipython filename'.

    Starts IPython with a minimal and safe configuration to make startup as fast
    as possible.

    Note that this starts IPython in a subprocess!

    Parameters
    ----------
    fname : str
      Name of file to be executed (should have .py or .ipy extension).

    options : optional, list
      Extra command-line flags to be passed to IPython.

    commands : optional, list
      Commands to send in on stdin

    Returns
    -------
    (stdout, stderr) of ipython subprocess.
    """
    if options is None: options = []

    # For these subprocess calls, eliminate all prompt printing so we only see
    # output from script execution
    prompt_opts = [ '--PromptManager.in_template=""',
                    '--PromptManager.in2_template=""',
                    '--PromptManager.out_template=""'
    ]
    cmdargs = default_argv() + prompt_opts + options

    test_dir = os.path.dirname(__file__)

    ipython_cmd = get_ipython_cmd()
    # Absolute path for filename
    full_fname = os.path.join(test_dir, fname)
    full_cmd = ipython_cmd + cmdargs + [full_fname]
    env = os.environ.copy()
    env.pop('PYTHONWARNINGS', None)  # Avoid extraneous warnings appearing on stderr
    p = Popen(full_cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, env=env)
    out, err = p.communicate(input=py3compat.str_to_bytes('\n'.join(commands)) or None)
    out, err = py3compat.bytes_to_str(out), py3compat.bytes_to_str(err)
    # `import readline` causes 'ESC[?1034h' to be output sometimes,
    # so strip that out before doing comparisons
    if out:
        out = re.sub(r'\x1b\[[^h]+h', '', out)
    return out, err
Ejemplo n.º 43
0
def main(connection_file):
    """watch iopub channel, and print messages"""

    ctx = zmq.Context.instance()

    with open(connection_file) as f:
        cfg = json.loads(f.read())

    location = cfg["location"]
    reg_url = cfg["url"]
    session = Session(key=str_to_bytes(cfg["exec_key"]))

    query = ctx.socket(zmq.DEALER)
    query.connect(disambiguate_url(cfg["url"], location))
    session.send(query, "connection_request")
    idents, msg = session.recv(query, mode=0)
    c = msg["content"]
    iopub_url = disambiguate_url(c["iopub"], location)
    sub = ctx.socket(zmq.SUB)
    # This will subscribe to all messages:
    sub.setsockopt(zmq.SUBSCRIBE, b"")
    # replace with b'' with b'engine.1.stdout' to subscribe only to engine 1's stdout
    # 0MQ subscriptions are simple 'foo*' matches, so 'engine.1.' subscribes
    # to everything from engine 1, but there is no way to subscribe to
    # just stdout from everyone.
    # multiple calls to subscribe will add subscriptions, e.g. to subscribe to
    # engine 1's stderr and engine 2's stdout:
    # sub.setsockopt(zmq.SUBSCRIBE, b'engine.1.stderr')
    # sub.setsockopt(zmq.SUBSCRIBE, b'engine.2.stdout')
    sub.connect(iopub_url)
    while True:
        try:
            idents, msg = session.recv(sub, mode=0)
        except KeyboardInterrupt:
            return
        # ident always length 1 here
        topic = idents[0]
        if msg["msg_type"] == "stream":
            # stdout/stderr
            # stream names are in msg['content']['name'], if you want to handle
            # them differently
            print "%s: %s" % (topic, msg["content"]["data"])
        elif msg["msg_type"] == "pyerr":
            # Python traceback
            c = msg["content"]
            print topic + ":"
            for line in c["traceback"]:
                # indent lines
                print "    " + line
Ejemplo n.º 44
0
def display_cell(text):
    """Remove cells that start with "# Temp" and add a new one

    Arguments:

    * `text` -- new cell content

    """
    encoded_code = bytes_to_str(base64.b64encode(str_to_bytes(text)))
    display(
        Javascript("""
        $('span:contains("# Temp")').closest('.cell').remove();
        var code = IPython.notebook.insert_cell_{0}('code');
        code.set_text(atob("{1}"));
    """.format('below', encoded_code)))
Ejemplo n.º 45
0
    def load_connection_file(self):
        """Load connection info from JSON dict in self.connection_file."""
        self.log.debug(u"Loading connection file %s", self.connection_file)
        with open(self.connection_file) as f:
            cfg = json.load(f)
        self.transport = cfg.get('transport', self.transport)
        self.ip = cfg.get('ip', self._ip_default())

        for name in port_names:
            if getattr(self, name) == 0 and name in cfg:
                # not overridden by config or cl_args
                setattr(self, name, cfg[name])
        if 'key' in cfg:
            self.config.Session.key = str_to_bytes(cfg['key'])
        if 'signature_scheme' in cfg:
            self.config.Session.signature_scheme = cfg['signature_scheme']
Ejemplo n.º 46
0
 def load_connection_file(self):
     """Load connection info from JSON dict in self.connection_file."""
     self.log.debug(u"Loading connection file %s", self.connection_file)
     with open(self.connection_file) as f:
         cfg = json.load(f)
     self.transport = cfg.get('transport', self.transport)
     self.ip = cfg.get('ip', self._ip_default())
     
     for name in port_names:
         if getattr(self, name) == 0 and name in cfg:
             # not overridden by config or cl_args
             setattr(self, name, cfg[name])
     if 'key' in cfg:
         self.config.Session.key = str_to_bytes(cfg['key'])
     if 'signature_scheme' in cfg:
         self.config.Session.signature_scheme = cfg['signature_scheme']
Ejemplo n.º 47
0
def signup():
    form = SignUpForm(request.form,csrf_enabled=False)
    if request.method == 'POST':
        if form.validate():
            
            username = str(form.username.data)
            email = str(form.email.data)
            password = str(form.password.data)

            #curr user count
            curr_user_count = models.User.query.count()

            if curr_user_count >= 15:
                flash('Your company can only create 15 users, please contact us if you have any question')
                return render_template('index.html')

            # get the next server port
            port = 9499 + curr_user_count + 1
            #create passwd
            h = hashlib.new(algorithm)
            salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
            h.update(cast_bytes(password, 'utf-8') + str_to_bytes(salt, 'ascii'))
            hashed_password = '******'.join((algorithm, salt, h.hexdigest()))

            # create user
            user = models.User()
            user.username = username
            user.email = email
            user.password = hashed_password
            user.nbserver_port = port
            user = db.session.merge(user)
            db.session.commit()

            # create the user data directory hierarchy
            if not os.path.exists('{0}/{1}'.format(DATA_DIR, username)):
                create_user_dir(username, hashed_password)

            return redirect('/login')
        else:
            errorMsg = ''
            for key in form.errors:
                errorMsg=errorMsg+key+':'+form.errors[key][0]+'      '

            flash(errorMsg)
            return render_template('index.html')
        
    return render_template('index.html')
Ejemplo n.º 48
0
def main(connection_file):
    """watch iopub channel, and print messages"""

    ctx = zmq.Context.instance()

    with open(connection_file) as f:
        cfg = json.loads(f.read())

    reg_url = cfg['interface']
    iopub_port = cfg['iopub']
    iopub_url = "%s:%s"%(reg_url, iopub_port)

    session = Session(key=str_to_bytes(cfg['key']))
    sub = ctx.socket(zmq.SUB)

    # This will subscribe to all messages:
    sub.setsockopt(zmq.SUBSCRIBE, b'')
    # replace with b'' with b'engine.1.stdout' to subscribe only to engine 1's stdout
    # 0MQ subscriptions are simple 'foo*' matches, so 'engine.1.' subscribes
    # to everything from engine 1, but there is no way to subscribe to
    # just stdout from everyone.
    # multiple calls to subscribe will add subscriptions, e.g. to subscribe to
    # engine 1's stderr and engine 2's stdout:
    # sub.setsockopt(zmq.SUBSCRIBE, b'engine.1.stderr')
    # sub.setsockopt(zmq.SUBSCRIBE, b'engine.2.stdout')
    sub.connect(iopub_url)
    while True:
        try:
            idents,msg = session.recv(sub, mode=0)
        except KeyboardInterrupt:
            return
        # ident always length 1 here
        topic = idents[0]
        if msg['msg_type'] == 'stream':
            # stdout/stderr
            # stream names are in msg['content']['name'], if you want to handle
            # them differently
            print("%s: %s" % (topic, msg['content']['text']))
        elif msg['msg_type'] == 'pyerr':
            # Python traceback
            c = msg['content']
            print(topic + ':')
            for line in c['traceback']:
                # indent lines
                print('    ' + line)
Ejemplo n.º 49
0
def new_cell(texto, tipo_celda):

    if tipo_celda == "markdown":

        display(
            Javascript("""
      var mark = IPython.notebook.insert_cell_above('markdown')
      mark.set_text("{0}")
      mark.execute()
    """.format(texto.encode('utf-8'))))

    if tipo_celda == "code":
        texto = bytes_to_str(base64.b64encode(str_to_bytes(texto)))
        display(
            Javascript("""
    var code = IPython.notebook.insert_cell_above('code')
    code.set_text(atob("{0}"))
    """.format(texto)))
Ejemplo n.º 50
0
def passwd_check(hashed_passphrase, passphrase):
    """Verify that a given passphrase matches its hashed version.

    Parameters
    ----------
    hashed_passphrase : str
        Hashed password, in the format returned by `passwd`.
    passphrase : str
        Passphrase to validate.

    Returns
    -------
    valid : bool
        True if the passphrase matches the hash.

    Examples
    --------
    In [1]: from IPython.lib.security import passwd_check

    In [2]: passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
       ...:              'mypassword')
    Out[2]: True

    In [3]: passwd_check('sha1:0e112c3ddfce:a68df677475c2b47b6e86d0467eec97ac5f4b85a',
       ...:              'anotherpassword')
    Out[3]: False

    """
    try:
        algorithm, salt, pw_digest = hashed_passphrase.split(':', 2)
    except (ValueError, TypeError):
        return False

    try:
        h = hashlib.new(algorithm)
    except ValueError:
        return False

    if len(pw_digest) == 0:
        return False

    h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))

    return h.hexdigest() == pw_digest
Ejemplo n.º 51
0
def passwd(passphrase=None, algorithm='sha1'):
    """Generate hashed password and salt for use in notebook configuration.

    In the notebook configuration, set `c.NotebookApp.password` to
    the generated string.

    Parameters
    ----------
    passphrase : str
        Password to hash.  If unspecified, the user is asked to input
        and verify a password.
    algorithm : str
        Hashing algorithm to use (e.g, 'sha1' or any argument supported
        by :func:`hashlib.new`).

    Returns
    -------
    hashed_passphrase : str
        Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.

    Examples
    --------
    >>> passwd('mypassword')
    'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'

    """
    if passphrase is None:
        for i in range(3):
            p0 = getpass.getpass('Enter password: '******'Verify password: '******'Passwords do not match.')
        else:
            raise UsageError('No matching passwords found. Giving up.')

    h = hashlib.new(algorithm)
    salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
    h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))

    return ':'.join((algorithm, salt, h.hexdigest()))
Ejemplo n.º 52
0
def passwd(passphrase=None, algorithm='sha1'):
    """Generate hashed password and salt for use in notebook configuration.

    In the notebook configuration, set `c.NotebookApp.password` to
    the generated string.

    Parameters
    ----------
    passphrase : str
        Password to hash.  If unspecified, the user is asked to input
        and verify a password.
    algorithm : str
        Hashing algorithm to use (e.g, 'sha1' or any argument supported
        by :func:`hashlib.new`).

    Returns
    -------
    hashed_passphrase : str
        Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'.

    Examples
    --------
    >>> passwd('mypassword')
    'sha1:7cf3:b7d6da294ea9592a9480c8f52e63cd42cfb9dd12'

    """
    if passphrase is None:
        for i in range(3):
            p0 = getpass.getpass('Enter password: '******'Verify password: '******'Passwords do not match.')
        else:
            raise UsageError('No matching passwords found. Giving up.')

    h = hashlib.new(algorithm)
    salt = ('%0' + str(salt_len) + 'x') % random.getrandbits(4 * salt_len)
    h.update(cast_bytes(passphrase, 'utf-8') + str_to_bytes(salt, 'ascii'))

    return ':'.join((algorithm, salt, h.hexdigest()))
Ejemplo n.º 53
0
def create_code_cell(code='', where='below'):
    """Create a code cell in the IPython Notebook.

    Found at https://github.com/ipython/ipython/issues/4983

    Parameters
    code: unicode
        Code to fill the new code cell with.
    where: unicode
        Where to add the new code cell.
        Possible values include:
            at_bottom
            above
            below"""
    encoded_code = bytes_to_str(base64.b64encode(str_to_bytes(code)))
    display(Javascript("""
        var code = IPython.notebook.insert_cell_{0}('code');
        code.set_text(atob("{1}"));
    """.format(where, encoded_code)))
Ejemplo n.º 54
0
def compute_checksum(cell):
    m = hashlib.md5()
    # add the cell source and type
    m.update(str_to_bytes(cell.source))
    m.update(str_to_bytes(cell.cell_type))

    # add whether it's a grade cell and/or solution cell
    m.update(str_to_bytes(str(is_grade(cell))))
    m.update(str_to_bytes(str(is_solution(cell))))
    m.update(str_to_bytes(str(is_locked(cell))))

    # include the cell id
    m.update(str_to_bytes(cell.metadata.nbgrader['grade_id']))

    # include the number of points that the cell is worth, if it is a grade cell
    if is_grade(cell):
        m.update(str_to_bytes(str(float(cell.metadata.nbgrader['points']))))

    return m.hexdigest()
Ejemplo n.º 55
0
 def load_connection_file(self):
     """load ip/port/hmac config from JSON connection file"""
     try:
         fname = filefind(self.connection_file, ['.', self.profile_dir.security_dir])
     except IOError:
         self.log.debug("Connection file not found: %s", self.connection_file)
         return
     self.log.debug(u"Loading connection file %s", fname)
     with open(fname) as f:
         s = f.read()
     cfg = json.loads(s)
     if self.ip == LOCALHOST and 'ip' in cfg:
         # not overridden by config or cl_args
         self.ip = cfg['ip']
     for channel in ('hb', 'shell', 'iopub', 'stdin'):
         name = channel + '_port'
         if getattr(self, name) == 0 and name in cfg:
             # not overridden by config or cl_args
             setattr(self, name, cfg[name])
     if 'key' in cfg:
         self.config.Session.key = str_to_bytes(cfg['key'])
Ejemplo n.º 56
0
 def load_connection_file(self):
     """load ip/port/hmac config from JSON connection file"""
     try:
         fname = filefind(self.connection_file, [".", self.profile_dir.security_dir])
     except IOError:
         self.log.debug("Connection file not found: %s", self.connection_file)
         return
     self.log.debug(u"Loading connection file %s", fname)
     with open(fname) as f:
         s = f.read()
     cfg = json.loads(s)
     if self.ip == LOCALHOST and "ip" in cfg:
         # not overridden by config or cl_args
         self.ip = cfg["ip"]
     for channel in ("hb", "shell", "iopub", "stdin"):
         name = channel + "_port"
         if getattr(self, name) == 0 and name in cfg:
             # not overridden by config or cl_args
             setattr(self, name, cfg[name])
     if "key" in cfg:
         self.config.Session.key = str_to_bytes(cfg["key"])
    def beat(self):
        self.pongstream.flush()
        self.last_ping = self.lifetime

        toc = time.time()
        self.lifetime += toc-self.tic
        self.tic = toc
        self.log.debug("heartbeat::sending %s", self.lifetime)
        goodhearts = self.hearts.intersection(self.responses)
        missed_beats = self.hearts.difference(goodhearts)
        heartfailures = self.on_probation.intersection(missed_beats)
        newhearts = self.responses.difference(goodhearts)
        list(map(self.handle_new_heart, newhearts))
        list(map(self.handle_heart_failure, heartfailures))
        self.on_probation = missed_beats.intersection(self.hearts)
        self.responses = set()
        # print self.on_probation, self.hearts
        # self.log.debug("heartbeat::beat %.3f, %i beating hearts", self.lifetime, len(self.hearts))
        self.pingstream.send(str_to_bytes(str(self.lifetime)))
        # flush stream to force immediate socket send
        self.pingstream.flush()