Exemplo n.º 1
0
 def _exec_file(self, fname):
     try:
         full_filename = filefind(fname, [u'.', self.ipython_dir])
     except IOError as e:
         self.log.warn("File not found: %r"%fname)
         return
     # Make sure that the running script gets a proper sys.argv as if it
     # were run from a system shell.
     save_argv = sys.argv
     sys.argv = [full_filename] + self.extra_args[1:]
     # protect sys.argv from potential unicode strings on Python 2:
     if not py3compat.PY3:
         sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
     try:
         if os.path.isfile(full_filename):
             if full_filename.endswith('.ipy'):
                 self.log.info("Running file in user namespace: %s" %
                               full_filename)
                 self.shell.safe_execfile_ipy(full_filename)
             else:
                 # default to python, even without extension
                 self.log.info("Running file in user namespace: %s" %
                               full_filename)
                 # Ensure that __file__ is always defined to match Python behavior
                 self.shell.user_ns['__file__'] = fname
                 try:
                     self.shell.safe_execfile(full_filename, self.shell.user_ns)
                 finally:
                     del self.shell.user_ns['__file__']
     finally:
         sys.argv = save_argv
Exemplo n.º 2
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']
Exemplo n.º 3
0
 def _exec_file(self, fname, shell_futures=False):
     try:
         full_filename = filefind(fname, [u'.', self.ipython_dir])
     except IOError:
         self.log.warning("File not found: %r" % fname)
         return
     # Make sure that the running script gets a proper sys.argv as if it
     # were run from a system shell.
     save_argv = sys.argv
     sys.argv = [full_filename] + self.extra_args[1:]
     try:
         if os.path.isfile(full_filename):
             self.log.info("Running file in user namespace: %s" %
                           full_filename)
             # Ensure that __file__ is always defined to match Python
             # behavior.
             with preserve_keys(self.shell.user_ns, '__file__'):
                 self.shell.user_ns['__file__'] = fname
                 if full_filename.endswith('.ipy'):
                     self.shell.safe_execfile_ipy(
                         full_filename, shell_futures=shell_futures)
                 else:
                     # default to python, even without extension
                     self.shell.safe_execfile(full_filename,
                                              self.shell.user_ns,
                                              shell_futures=shell_futures,
                                              raise_exceptions=True)
     finally:
         sys.argv = save_argv
Exemplo n.º 4
0
 def _mathjax_url_default(self):
     if not self.enable_mathjax:
         return u''
     static_url_prefix = self.webapp_settings.get("static_url_prefix",
                      url_path_join(self.base_url, "static")
     )
     
     # try local mathjax, either in nbextensions/mathjax or static/mathjax
     for (url_prefix, search_path) in [
         (url_path_join(self.base_url, "nbextensions"), self.nbextensions_path),
         (static_url_prefix, self.static_file_path),
     ]:
         self.log.debug("searching for local mathjax in %s", search_path)
         try:
             mathjax = filefind(os.path.join('mathjax', 'MathJax.js'), search_path)
         except IOError:
             continue
         else:
             url = url_path_join(url_prefix, u"mathjax/MathJax.js")
             self.log.info("Serving local MathJax from %s at %s", mathjax, url)
             return url
     
     # no local mathjax, serve from CDN
     if self.certfile:
         # HTTPS: load from Rackspace CDN, because SSL certificate requires it
         host = u"https://c328740.ssl.cf1.rackcdn.com"
     else:
         host = u"http://cdn.mathjax.org"
     
     url = host + u"/mathjax/latest/MathJax.js"
     self.log.info("Using MathJax from CDN: %s", url)
     return url
Exemplo 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.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']
Exemplo n.º 6
0
 def _exec_file(self, fname):
     try:
         full_filename = filefind(fname, [u'.', self.ipython_dir])
     except IOError as e:
         self.log.warn("File not found: %r" % fname)
         return
     # Make sure that the running script gets a proper sys.argv as if it
     # were run from a system shell.
     save_argv = sys.argv
     sys.argv = [full_filename] + self.extra_args[1:]
     # protect sys.argv from potential unicode strings on Python 2:
     if not py3compat.PY3:
         sys.argv = [py3compat.cast_bytes(a) for a in sys.argv]
     try:
         if os.path.isfile(full_filename):
             if full_filename.endswith('.ipy'):
                 self.log.info("Running file in user namespace: %s" %
                               full_filename)
                 self.shell.safe_execfile_ipy(full_filename)
             else:
                 # default to python, even without extension
                 self.log.info("Running file in user namespace: %s" %
                               full_filename)
                 # Ensure that __file__ is always defined to match Python behavior
                 self.shell.user_ns['__file__'] = fname
                 try:
                     self.shell.safe_execfile(full_filename,
                                              self.shell.user_ns)
                 finally:
                     del self.shell.user_ns['__file__']
     finally:
         sys.argv = save_argv
Exemplo n.º 7
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'])
Exemplo n.º 8
0
    def init_kernel_manager(self):
        # Don't let Qt or ZMQ swallow KeyboardInterupts.
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        sec = self.profile_dir.security_dir
        try:
            cf = filefind(self.connection_file, ['.', sec])
        except IOError:
            # file might not exist
            if self.connection_file == os.path.basename(self.connection_file):
                # just shortname, put it in security dir
                cf = os.path.join(sec, self.connection_file)
            else:
                cf = self.connection_file

        # Create a KernelManager and start a kernel.
        self.kernel_manager = QtKernelManager(
                                ip=self.ip,
                                shell_port=self.shell_port,
                                iopub_port=self.iopub_port,
                                stdin_port=self.stdin_port,
                                hb_port=self.hb_port,
                                connection_file=cf,
                                config=self.config,
        )
        # start the kernel
        if not self.existing:
            kwargs = dict(ipython=not self.pure)
            kwargs['extra_arguments'] = self.kernel_argv
            self.kernel_manager.start_kernel(**kwargs)
        elif self.sshserver:
            # ssh, write new connection file
            self.kernel_manager.write_connection_file()
        self.kernel_manager.start_channels()
Exemplo n.º 9
0
 def _exec_file(self, fname, shell_futures=False):
     try:
         full_filename = filefind(fname, [u'.', self.ipython_dir])
     except IOError:
         self.log.warning("File not found: %r"%fname)
         return
     # Make sure that the running script gets a proper sys.argv as if it
     # were run from a system shell.
     save_argv = sys.argv
     sys.argv = [full_filename] + self.extra_args[1:]
     try:
         if os.path.isfile(full_filename):
             self.log.info("Running file in user namespace: %s" %
                           full_filename)
             # Ensure that __file__ is always defined to match Python
             # behavior.
             with preserve_keys(self.shell.user_ns, '__file__'):
                 self.shell.user_ns['__file__'] = fname
                 if full_filename.endswith('.ipy'):
                     self.shell.safe_execfile_ipy(full_filename,
                                                  shell_futures=shell_futures)
                 else:
                     # default to python, even without extension
                     self.shell.safe_execfile(full_filename,
                                              self.shell.user_ns,
                                              shell_futures=shell_futures,
                                              raise_exceptions=True)
     finally:
         sys.argv = save_argv
Exemplo n.º 10
0
    def init_kernel_manager(self):
        # Don't let Qt or ZMQ swallow KeyboardInterupts.
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        sec = self.profile_dir.security_dir
        try:
            cf = filefind(self.connection_file, ['.', sec])
        except IOError:
            # file might not exist
            if self.connection_file == os.path.basename(self.connection_file):
                # just shortname, put it in security dir
                cf = os.path.join(sec, self.connection_file)
            else:
                cf = self.connection_file

        # Create a KernelManager and start a kernel.
        self.kernel_manager = QtKernelManager(
            ip=self.ip,
            shell_port=self.shell_port,
            iopub_port=self.iopub_port,
            stdin_port=self.stdin_port,
            hb_port=self.hb_port,
            connection_file=cf,
            config=self.config,
        )
        # start the kernel
        if not self.existing:
            kwargs = dict(ipython=not self.pure)
            kwargs['extra_arguments'] = self.kernel_argv
            self.kernel_manager.start_kernel(**kwargs)
        elif self.sshserver:
            # ssh, write new connection file
            self.kernel_manager.write_connection_file()
        self.kernel_manager.start_channels()
Exemplo n.º 11
0
 def _mathjax_url_default(self):
     if not self.enable_mathjax:
         return u''
     static_url_prefix = self.tornado_settings.get("static_url_prefix",
                      url_path_join(self.base_url, "static")
     )
     
     # try local mathjax, either in nbextensions/mathjax or static/mathjax
     for (url_prefix, search_path) in [
         (url_path_join(self.base_url, "nbextensions"), self.nbextensions_path),
         (static_url_prefix, self.static_file_path),
     ]:
         self.log.debug("searching for local mathjax in %s", search_path)
         try:
             mathjax = filefind(os.path.join('mathjax', 'MathJax.js'), search_path)
         except IOError:
             continue
         else:
             url = url_path_join(url_prefix, u"mathjax/MathJax.js")
             self.log.info("Serving local MathJax from %s at %s", mathjax, url)
             return url
     
     # no local mathjax, serve from CDN
     url = u"https://cdn.mathjax.org/mathjax/latest/MathJax.js"
     self.log.info("Using MathJax from CDN: %s", url)
     return url
Exemplo n.º 12
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'])
Exemplo n.º 13
0
    def _mathjax_url_default(self):
        if not self.enable_mathjax:
            return u''
        static_url_prefix = self.webapp_settings.get(
            "static_url_prefix", url_path_join(self.base_url, "static"))

        # try local mathjax, either in nbextensions/mathjax or static/mathjax
        for (url_prefix, search_path) in [
            (url_path_join(self.base_url,
                           "nbextensions"), self.nbextensions_path),
            (static_url_prefix, self.static_file_path),
        ]:
            self.log.debug("searching for local mathjax in %s", search_path)
            try:
                mathjax = filefind(os.path.join('mathjax', 'MathJax.js'),
                                   search_path)
            except IOError:
                continue
            else:
                url = url_path_join(url_prefix, u"mathjax/MathJax.js")
                self.log.info("Serving local MathJax from %s at %s", mathjax,
                              url)
                return url

        # no local mathjax, serve from CDN
        if self.certfile:
            # HTTPS: load from Rackspace CDN, because SSL certificate requires it
            host = u"https://c328740.ssl.cf1.rackcdn.com"
        else:
            host = u"http://cdn.mathjax.org"

        url = host + u"/mathjax/latest/MathJax.js"
        self.log.info("Using MathJax from CDN: %s", url)
        return url
 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'])
Exemplo n.º 15
0
    def init_connection_file(self):
        """find the connection file, and load the info if found.
        
        The current working directory and the current profile's security
        directory will be searched for the file if it is not given by
        absolute path.
        
        When attempting to connect to an existing kernel and the `--existing`
        argument does not match an existing file, it will be interpreted as a
        fileglob, and the matching file in the current profile's security dir
        with the latest access time will be used.
        
        After this method is called, self.connection_file contains the *full path*
        to the connection file, never just its name.
        """
        if self.existing:
            try:
                cf = find_connection_file(self.existing)
            except Exception:
                self.log.critical(
                    "Could not find existing kernel connection file %s",
                    self.existing)
                self.exit(1)
            self.log.debug("Connecting to existing kernel: %s" % cf)
            self.connection_file = cf
        else:
            # not existing, check if we are going to write the file
            # and ensure that self.connection_file is a full path, not just the shortname
            try:
                cf = find_connection_file(self.connection_file)
            except Exception:
                # file might not exist
                if self.connection_file == os.path.basename(
                        self.connection_file):
                    # just shortname, put it in security dir
                    cf = os.path.join(self.profile_dir.security_dir,
                                      self.connection_file)
                else:
                    cf = self.connection_file
                self.connection_file = cf
        try:
            self.connection_file = filefind(
                self.connection_file, ['.', self.profile_dir.security_dir])
        except IOError:
            self.log.debug("Connection File not found: %s",
                           self.connection_file)
            return

        # should load_connection_file only be used for existing?
        # as it is now, this allows reusing ports if an existing
        # file is requested
        try:
            self.load_connection_file()
        except Exception:
            self.log.error("Failed to load connection file: %r",
                           self.connection_file,
                           exc_info=True)
            self.exit(1)
Exemplo n.º 16
0
def find_in_pythonpath (filename):
    global _my_custom_paths

    try:
        path_dirs = ['.'] + _my_custom_paths + os.environ['PYTHONPATH'].split(':')
    except:
        path_dirs = ['.'] + _my_custom_paths
    
    return filefind(filename, path_dirs)
Exemplo n.º 17
0
    def init_connection_file(self):
        """find the connection file, and load the info if found.

        The current working directory and the current profile's security
        directory will be searched for the file if it is not given by
        absolute path.

        When attempting to connect to an existing kernel and the `--existing`
        argument does not match an existing file, it will be interpreted as a
        fileglob, and the matching file in the current profile's security dir
        with the latest access time will be used.

        After this method is called, self.connection_file contains the *full path*
        to the connection file, never just its name.
        """
        if self.existing:
            try:
                cf = find_connection_file(self.existing)
            except Exception:
                self.log.critical(
                    "Could not find existing kernel connection file %s", self.existing)
                self.exit(1)
            self.log.debug("Connecting to existing kernel: %s" % cf)
            self.connection_file = cf
        else:
            # not existing, check if we are going to write the file
            # and ensure that self.connection_file is a full path, not just the
            # shortname
            try:
                cf = find_connection_file(self.connection_file)
            except Exception:
                # file might not exist
                if self.connection_file == os.path.basename(self.connection_file):
                    # just shortname, put it in security dir
                    cf = os.path.join(
                        self.profile_dir.security_dir, self.connection_file)
                else:
                    cf = self.connection_file
                self.connection_file = cf
        try:
            self.connection_file = filefind(
                self.connection_file, ['.', self.profile_dir.security_dir])
        except IOError:
            self.log.debug(
                "Connection File not found: %s", self.connection_file)
            return

        # should load_connection_file only be used for existing?
        # as it is now, this allows reusing ports if an existing
        # file is requested
        try:
            self.load_connection_file()
        except Exception:
            self.log.error(
                "Failed to load connection file: %r", self.connection_file, exc_info=True)
            self.exit(1)
Exemplo n.º 18
0
def find_connection_file(filename='kernel-*.json', path=None):
    """find a connection file, and return its absolute path.

    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.

    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.

    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    path : str or list of strs[optional]
        Paths in which to search for connection files.

    Returns
    -------
    str : The absolute path of the connection file.
    """
    if path is None:
        path = ['.']
    if isinstance(path, string_types):
        path = [path]
    
    try:
        # first, try explicit name
        return filefind(filename, path)
    except IOError:
        pass

    # not found by full name

    if '*' in filename:
        # given as a glob already
        pat = filename
    else:
        # accept any substring match
        pat = '*%s*' % filename
    
    matches = []
    for p in path:
        matches.extend(glob.glob(os.path.join(p, pat)))
    
    if not matches:
        raise IOError("Could not find %r in %r" % (filename, path))
    elif len(matches) == 1:
        return matches[0]
    else:
        # get most recent match, by access time:
        return sorted(matches, key=lambda f: os.stat(f).st_atime)[-1]
Exemplo n.º 19
0
def find_connection_file(filename='kernel-*.json', path=None):
    """find a connection file, and return its absolute path.

    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.

    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.

    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    path : str or list of strs[optional]
        Paths in which to search for connection files.

    Returns
    -------
    str : The absolute path of the connection file.
    """
    if path is None:
        path = ['.']
    if isinstance(path, string_types):
        path = [path]

    try:
        # first, try explicit name
        return filefind(filename, path)
    except IOError:
        pass

    # not found by full name

    if '*' in filename:
        # given as a glob already
        pat = filename
    else:
        # accept any substring match
        pat = '*%s*' % filename

    matches = []
    for p in path:
        matches.extend(glob.glob(os.path.join(p, pat)))

    if not matches:
        raise IOError("Could not find %r in %r" % (filename, path))
    elif len(matches) == 1:
        return matches[0]
    else:
        # get most recent match, by access time:
        return sorted(matches, key=lambda f: os.stat(f).st_atime)[-1]
Exemplo n.º 20
0
 def get_absolute_path(cls, roots, path):
     """locate a file to serve on our static file search path"""
     with cls._lock:
         if path in cls._static_paths:
             return cls._static_paths[path]
         try:
             abspath = os.path.abspath(filefind(path, roots))
         except IOError:
             # IOError means not found
             return ''
         
         cls._static_paths[path] = abspath
         return abspath
Exemplo n.º 21
0
 def get_absolute_path(cls, roots, path):
     """locate a file to serve on our static file search path"""
     with cls._lock:
         if path in cls._static_paths:
             return cls._static_paths[path]
         try:
             abspath = os.path.abspath(filefind(path, roots))
         except IOError:
             # empty string should always give exists=False
             return ''
         
         cls._static_paths[path] = abspath
         return abspath
Exemplo n.º 22
0
 def get_absolute_path(cls, roots, path):
     """locate a file to serve on our static file search path"""
     with cls._lock:
         if path in cls._static_paths:
             return cls._static_paths[path]
         try:
             abspath = os.path.abspath(filefind(path, roots))
         except IOError:
             # IOError means not found
             return ''
         
         cls._static_paths[path] = abspath
         return abspath
Exemplo n.º 23
0
def find_connection_file(filename='kernel-*.json', profile=None):
    """find a connection file, and return its absolute path.

    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.

    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.

    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    profile : str [optional]
        The name of the profile to use when searching for the connection file,
        if different from the current IPython session or 'default'.

    Returns
    -------
    str : The absolute path of the connection file.
    """
    from IPython.core.application import BaseIPythonApplication as IPApp
    try:
        # quick check for absolute path, before going through logic
        return filefind(filename)
    except IOError:
        pass

    if profile is None:
        # profile unspecified, check if running from an IPython app
        if IPApp.initialized():
            app = IPApp.instance()
            profile_dir = app.profile_dir
        else:
            # not running in IPython, use default profile
            profile_dir = ProfileDir.find_profile_dir_by_name(
                get_ipython_dir(), 'default')
    else:
        # find profiledir by profile name:
        profile_dir = ProfileDir.find_profile_dir_by_name(
            get_ipython_dir(), profile)
    security_dir = profile_dir.security_dir

    return jupyter_client.find_connection_file(filename,
                                               path=['.', security_dir])
Exemplo n.º 24
0
def get_connection_file(app=None):
    """Return the path to the connection file of an app
    
    Parameters
    ----------
    app : KernelApp instance [optional]
        If unspecified, the currently running app will be used
    """
    if app is None:
        from IPython.zmq.ipkernel import IPKernelApp
        if not IPKernelApp.initialized():
            raise RuntimeError("app not specified, and not in a running Kernel")

        app = IPKernelApp.instance()
    return filefind(app.connection_file, ['.', app.profile_dir.security_dir])
Exemplo n.º 25
0
 def init_connection_file(self):
     if not self.connection_file:
         self.connection_file = "kernel-%s.json"%os.getpid()
     try:
         self.connection_file = 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
     try:
         self.load_connection_file()
     except Exception:
         self.log.error("Failed to load connection file: %r", self.connection_file, exc_info=True)
         self.exit(1)
Exemplo n.º 26
0
def get_connection_file(app=None):
    """Return the path to the connection file of an app
    
    Parameters
    ----------
    app : KernelApp instance [optional]
        If unspecified, the currently running app will be used
    """
    if app is None:
        from IPython.zmq.kernelapp import KernelApp
        if not KernelApp.initialized():
            raise RuntimeError("app not specified, and not in a running Kernel")

        app = KernelApp.instance()
    return filefind(app.connection_file, ['.', app.profile_dir.security_dir])
Exemplo n.º 27
0
 def _exec_file(self, fname):
     full_filename = filefind(fname, [u".", self.ipython_dir])
     if os.path.isfile(full_filename):
         if full_filename.endswith(u".py"):
             self.log.info("Running file in user namespace: %s" % full_filename)
             # Ensure that __file__ is always defined to match Python behavior
             self.shell.user_ns["__file__"] = fname
             try:
                 self.shell.safe_execfile(full_filename, self.shell.user_ns)
             finally:
                 del self.shell.user_ns["__file__"]
         elif full_filename.endswith(".ipy"):
             self.log.info("Running file in user namespace: %s" % full_filename)
             self.shell.safe_execfile_ipy(full_filename)
         else:
             self.log.warn("File does not have a .py or .ipy extension: <%s>" % full_filename)
Exemplo n.º 28
0
def find_connection_file(filename='kernel-*.json', profile=None):
    """find a connection file, and return its absolute path.

    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.

    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.

    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.

    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    profile : str [optional]
        The name of the profile to use when searching for the connection file,
        if different from the current IPython session or 'default'.

    Returns
    -------
    str : The absolute path of the connection file.
    """
    from IPython.core.application import BaseIPythonApplication as IPApp
    try:
        # quick check for absolute path, before going through logic
        return filefind(filename)
    except IOError:
        pass

    if profile is None:
        # profile unspecified, check if running from an IPython app
        if IPApp.initialized():
            app = IPApp.instance()
            profile_dir = app.profile_dir
        else:
            # not running in IPython, use default profile
            profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), 'default')
    else:
        # find profiledir by profile name:
        profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
    security_dir = profile_dir.security_dir
    
    return jupyter_client.find_connection_file(filename, path=['.', security_dir])
Exemplo n.º 29
0
 def locate_file(cls, path, roots):
     """locate a file to serve on our static file search path"""
     with cls._lock:
         if path in cls._static_paths:
             return cls._static_paths[path]
         try:
             abspath = os.path.abspath(filefind(path, roots))
         except IOError:
             # empty string should always give exists=False
             return ''
     
         # os.path.abspath strips a trailing /
         # it needs to be temporarily added back for requests to root/
         if not (abspath + os.path.sep).startswith(roots):
             raise HTTPError(403, "%s is not in root static directory", path)
     
         cls._static_paths[path] = abspath
         return abspath
Exemplo n.º 30
0
 def locate_file(cls, path, roots):
     """locate a file to serve on our static file search path"""
     with cls._lock:
         if path in cls._static_paths:
             return cls._static_paths[path]
         try:
             abspath = os.path.abspath(filefind(path, roots))
         except IOError:
             # empty string should always give exists=False
             return ''
     
         # os.path.abspath strips a trailing /
         # it needs to be temporarily added back for requests to root/
         if not (abspath + os.path.sep).startswith(roots):
             raise HTTPError(403, "%s is not in root static directory", path)
     
         cls._static_paths[path] = abspath
         return abspath
Exemplo n.º 31
0
    def _mathjax_url_default(self):
        if not self.enable_mathjax:
            return u""
        static_url_prefix = self.webapp_settings.get("static_url_prefix", "/static/")
        try:
            mathjax = filefind(os.path.join("mathjax", "MathJax.js"), self.static_file_path)
        except IOError:
            if self.certfile:
                # HTTPS: load from Rackspace CDN, because SSL certificate requires it
                base = u"https://c328740.ssl.cf1.rackcdn.com"
            else:
                base = u"http://cdn.mathjax.org"

            url = base + u"/mathjax/latest/MathJax.js"
            self.log.info("Using MathJax from CDN: %s", url)
            return url
        else:
            self.log.info("Using local MathJax from %s" % mathjax)
            return static_url_prefix + u"mathjax/MathJax.js"
Exemplo n.º 32
0
 def init_connection_file(self):
     if not self.connection_file:
         self.connection_file = "kernel-%s.json" % os.getpid()
     try:
         self.connection_file = 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
     try:
         self.load_connection_file()
     except Exception:
         self.log.error("Failed to load connection file: %r",
                        self.connection_file,
                        exc_info=True)
         self.exit(1)
Exemplo n.º 33
0
 def _exec_file(self, fname):
     full_filename = filefind(fname, [u'.', self.ipython_dir])
     if os.path.isfile(full_filename):
         if full_filename.endswith(u'.py'):
             self.log.info("Running file in user namespace: %s" %
                           full_filename)
             # Ensure that __file__ is always defined to match Python behavior
             self.shell.user_ns['__file__'] = fname
             try:
                 self.shell.safe_execfile(full_filename, self.shell.user_ns)
             finally:
                 del self.shell.user_ns['__file__']
         elif full_filename.endswith('.ipy'):
             self.log.info("Running file in user namespace: %s" %
                           full_filename)
             self.shell.safe_execfile_ipy(full_filename)
         else:
             self.log.warn("File does not have a .py or .ipy extension: <%s>"
                            % full_filename)
Exemplo n.º 34
0
    def get_version(cls, settings, path):
        """Generate the version string to be used in static URLs.

        This method may be overridden in subclasses (but note that it
        is a class method rather than a static method).  The default
        implementation uses a hash of the file's contents.

        ``settings`` is the `Application.settings` dictionary and ``path``
        is the relative location of the requested asset on the filesystem.
        The returned value should be a string, or ``None`` if no version
        could be determined.
        """
        # begin subclass override:
        static_paths = settings['static_path']
        if isinstance(static_paths, basestring):
            static_paths = [static_paths]
        roots = tuple(
            os.path.abspath(os.path.expanduser(p)) + os.path.sep for p in static_paths
        )

        try:
            abs_path = filefind(path, roots)
        except IOError:
            logging.error("Could not find static file %r", path)
            return None
        
        # end subclass override
        
        with cls._lock:
            hashes = cls._static_hashes
            if abs_path not in hashes:
                try:
                    f = open(abs_path, "rb")
                    hashes[abs_path] = hashlib.md5(f.read()).hexdigest()
                    f.close()
                except Exception:
                    logging.error("Could not open static file %r", path)
                    hashes[abs_path] = None
            hsh = hashes.get(abs_path)
            if hsh:
                return hsh[:5]
        return None
Exemplo n.º 35
0
    def get_version(cls, settings, path):
        """Generate the version string to be used in static URLs.

        This method may be overridden in subclasses (but note that it
        is a class method rather than a static method).  The default
        implementation uses a hash of the file's contents.

        ``settings`` is the `Application.settings` dictionary and ``path``
        is the relative location of the requested asset on the filesystem.
        The returned value should be a string, or ``None`` if no version
        could be determined.
        """
        # begin subclass override:
        static_paths = settings['static_path']
        if isinstance(static_paths, basestring):
            static_paths = [static_paths]
        roots = tuple(
            os.path.abspath(os.path.expanduser(p)) + os.path.sep
            for p in static_paths)

        try:
            abs_path = filefind(path, roots)
        except IOError:
            app_log.error("Could not find static file %r", path)
            return None

        # end subclass override

        with cls._lock:
            hashes = cls._static_hashes
            if abs_path not in hashes:
                try:
                    f = open(abs_path, "rb")
                    hashes[abs_path] = hashlib.md5(f.read()).hexdigest()
                    f.close()
                except Exception:
                    app_log.error("Could not open static file %r", path)
                    hashes[abs_path] = None
            hsh = hashes.get(abs_path)
            if hsh:
                return hsh[:5]
        return None
Exemplo n.º 36
0
 def _mathjax_url_default(self):
     if not self.enable_mathjax:
         return u''
     static_url_prefix = self.webapp_settings.get("static_url_prefix",
                                                  "/static/")
     try:
         mathjax = filefind(os.path.join('mathjax', 'MathJax.js'), self.static_file_path)
     except IOError:
         if self.certfile:
             # HTTPS: load from Rackspace CDN, because SSL certificate requires it
             base = u"https://c328740.ssl.cf1.rackcdn.com"
         else:
             base = u"http://cdn.mathjax.org"
         
         url = base + u"/mathjax/latest/MathJax.js"
         self.log.info("Using MathJax from CDN: %s", url)
         return url
     else:
         self.log.info("Using local MathJax from %s" % mathjax)
         return static_url_prefix+u"mathjax/MathJax.js"
Exemplo n.º 37
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"])
Exemplo n.º 38
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'])
Exemplo n.º 39
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"]
Exemplo n.º 40
0
def find_in_syspath(filename):
    return filefind(filename, sys.path)
Exemplo n.º 41
0
def test_filefind():
    """Various tests for filefind"""
    f = tempfile.NamedTemporaryFile()
    # print 'fname:',f.name
    alt_dirs = paths.get_ipython_dir()
    t = path.filefind(f.name, alt_dirs)
Exemplo n.º 42
0
def find_connection_file(filename, profile=None):
    """find a connection file, and return its absolute path.
    
    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.
    
    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.
    
    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.
    
    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    profile : str [optional]
        The name of the profile to use when searching for the connection file,
        if different from the current IPython session or 'default'.
    
    Returns
    -------
    str : The absolute path of the connection file.
    """
    from IPython.core.application import BaseIPythonApplication as IPApp
    try:
        # quick check for absolute path, before going through logic
        return filefind(filename)
    except IOError:
        pass

    if profile is None:
        # profile unspecified, check if running from an IPython app
        if IPApp.initialized():
            app = IPApp.instance()
            profile_dir = app.profile_dir
        else:
            # not running in IPython, use default profile
            profile_dir = ProfileDir.find_profile_dir_by_name(
                get_ipython_dir(), 'default')
    else:
        # find profiledir by profile name:
        profile_dir = ProfileDir.find_profile_dir_by_name(
            get_ipython_dir(), profile)
    security_dir = profile_dir.security_dir

    try:
        # first, try explicit name
        return filefind(filename, ['.', security_dir])
    except IOError:
        pass

    # not found by full name

    if '*' in filename:
        # given as a glob already
        pat = filename
    else:
        # accept any substring match
        pat = '*%s*' % filename
    matches = glob.glob(os.path.join(security_dir, pat))
    if not matches:
        raise IOError("Could not find %r in %r" % (filename, security_dir))
    elif len(matches) == 1:
        return matches[0]
    else:
        # get most recent match, by access time:
        return sorted(matches, key=lambda f: os.stat(f).st_atime)[-1]
Exemplo n.º 43
0
 def _find_file(self):
     """Try to find the file by searching the paths."""
     self.full_filename = filefind(self.filename, self.path)
Exemplo n.º 44
0
def find_connection_file(filename, profile=None):
    """find a connection file, and return its absolute path.
    
    The current working directory and the profile's security
    directory will be searched for the file if it is not given by
    absolute path.
    
    If profile is unspecified, then the current running application's
    profile will be used, or 'default', if not run from IPython.
    
    If the argument does not match an existing file, it will be interpreted as a
    fileglob, and the matching file in the profile's security dir with
    the latest access time will be used.
    
    Parameters
    ----------
    filename : str
        The connection file or fileglob to search for.
    profile : str [optional]
        The name of the profile to use when searching for the connection file,
        if different from the current IPython session or 'default'.
    
    Returns
    -------
    str : The absolute path of the connection file.
    """
    from IPython.core.application import BaseIPythonApplication as IPApp
    try:
        # quick check for absolute path, before going through logic
        return filefind(filename)
    except IOError:
        pass
    
    if profile is None:
        # profile unspecified, check if running from an IPython app
        if IPApp.initialized():
            app = IPApp.instance()
            profile_dir = app.profile_dir
        else:
            # not running in IPython, use default profile
            profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), 'default')
    else:
        # find profiledir by profile name:
        profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
    security_dir = profile_dir.security_dir
    
    try:
        # first, try explicit name
        return filefind(filename, ['.', security_dir])
    except IOError:
        pass
    
    # not found by full name
    
    if '*' in filename:
        # given as a glob already
        pat = filename
    else:
        # accept any substring match
        pat = '*%s*' % filename
    matches = glob.glob( os.path.join(security_dir, pat) )
    if not matches:
        raise IOError("Could not find %r in %r" % (filename, security_dir))
    elif len(matches) == 1:
        return matches[0]
    else:
        # get most recent match, by access time:
        return sorted(matches, key=lambda f: os.stat(f).st_atime)[-1]
Exemplo n.º 45
0
 def _find_file(self):
     """Try to find the file by searching the paths."""
     self.full_filename = filefind(self.filename, self.path)
Exemplo n.º 46
0
def test_filefind():
    """Various tests for filefind"""
    f = tempfile.NamedTemporaryFile()
    # print 'fname:',f.name
    alt_dirs = path.get_ipython_dir()
    t = path.filefind(f.name, alt_dirs)