def _create_base_ipython_dirs():
    """Create default user directories to prevent potential race conditions downstream.
    """
    utils.safe_makedir(get_ipython_dir())
    ProfileDir.create_profile_dir_by_name(get_ipython_dir())
    utils.safe_makedir(os.path.join(get_ipython_dir(), "db"))
    utils.safe_makedir(os.path.join(locate_profile(), "db"))
Example #2
0
    def _cluster_started(self, cluster_id):
        home_dir = os.path.expanduser('~')
        started = False
        self.connection_files = [get_ipython_dir() + '/profile_sge/security/ipcontroller-' + cluster_id + '-client.json',
                                 get_ipython_dir() + '/profile_sge/security/ipcontroller-' + cluster_id + '-engine.json',
            ]
        if os.path.isfile(self.connection_files[0]):
            if os.path.isfile(self.connection_files[1]):
                started = True

        return started
Example #3
0
def setup():
    cp = Popen('ipcontroller --profile iptest -r --log-level 10 --log-to-file --usethreads'.split(), stdout=blackhole, stderr=STDOUT)
    processes.append(cp)
    engine_json = os.path.join(get_ipython_dir(), 'cluster_iptest', 'security', 'ipcontroller-engine.json')
    client_json = os.path.join(get_ipython_dir(), 'cluster_iptest', 'security', 'ipcontroller-client.json')
    tic = time.time()
    while not os.path.exists(engine_json) or not os.path.exists(client_json):
        if cp.poll() is not None:
            print cp.poll()
            raise RuntimeError("The test controller failed to start.")
        elif time.time()-tic > 10:
            raise RuntimeError("Timeout waiting for the test controller to start.")
        time.sleep(0.1)
    add_engines(1)
Example #4
0
def test_get_ipython_dir_2():
    """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
    path.get_home_dir = lambda : "someplace"
    os.name = "posix"
    env.pop('IPYTHON3_DIR', None)
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, os.path.join("someplace", ".ipython3"))
Example #5
0
def setup():
    
    # show tracebacks for RemoteErrors
    class RemoteErrorWithTB(error.RemoteError):
        def __str__(self):
            s = super(RemoteErrorWithTB, self).__str__()
            return '\n'.join([s, self.traceback or ''])
    
    error.RemoteError = RemoteErrorWithTB
    
    cluster_dir = os.path.join(get_ipython_dir(), 'profile_iptest')
    engine_json = os.path.join(cluster_dir, 'security', 'ipcontroller-engine.json')
    client_json = os.path.join(cluster_dir, 'security', 'ipcontroller-client.json')
    for json in (engine_json, client_json):
        if os.path.exists(json):
            os.remove(json)
    
    cp = TestProcessLauncher()
    cp.cmd_and_args = ipcontroller_cmd_argv + \
                ['--profile=iptest', '--log-level=20', '--ping=250', '--dictdb']
    cp.start()
    launchers.append(cp)
    tic = time.time()
    while not os.path.exists(engine_json) or not os.path.exists(client_json):
        if cp.poll() is not None:
            raise RuntimeError("The test controller exited with status %s" % cp.poll())
        elif time.time()-tic > 15:
            raise RuntimeError("Timeout waiting for the test controller to start.")
        time.sleep(0.1)
    add_engines(1)
Example #6
0
def test_get_ipython_dir_7():
    """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
    path._writable_dir = lambda path: True
    home_dir = os.path.normpath(os.path.expanduser('~'))
    env['IPYTHONDIR'] = os.path.join('~', 'somewhere')
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, os.path.join(home_dir, 'somewhere'))
Example #7
0
def list_running_servers(profile='default'):
    """Iterate over the server info files of running notebook servers.
    
    Given a profile name, find nbserver-* files in the security directory of
    that profile, and yield dicts of their information, each one pertaining to
    a currently running notebook server instance.
    """
    pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), name=profile)
    for file in os.listdir(pd.security_dir):
        if file.startswith('nbserver-'):
            with io.open(
                    os.path.join(pd.security_dir, file),
                    encoding='utf-8') as f:
                info = json.load(f)

            # Simple check whether that process is really still running
            # Also remove leftover files from IPython 2.x without a pid field
            if ('pid' in info) and check_pid(info['pid']):
                yield info
            else:
                # If the process has died, try to delete its info file
                try:
                    os.unlink(file)
                except OSError:
                    pass  # TODO: This should warn or log or something
Example #8
0
def test_get_ipython_dir_1():
    """test_get_ipython_dir_1, Testcase to see if we can call get_ipython_dir without Exceptions."""
    env_ipdir = os.path.join("someplace", ".ipython")
    path._writable_dir = lambda path: True
    env['IPYTHONDIR'] = env_ipdir
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, env_ipdir)
Example #9
0
def test_get_ipython_dir_7():
    """test_get_ipython_dir_7, test home directory expansion on IPYTHONDIR"""
    path._writable_dir = lambda path: True
    home_dir = os.path.expanduser("~")
    env["IPYTHONDIR"] = os.path.join("~", "somewhere")
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, os.path.join(home_dir, "somewhere"))
Example #10
0
def write_ipynb_local_js(location=None, d3_src=None):

    try:
        from IPython.html import install_nbextension
    except ImportError:
        location = os.getcwd()
        nbextension = False
    else:
        nbextension = True

    d3_src = os.path.join(os.path.dirname(__file__), 'd3.v3.min.js')

    d3js = os.path.basename(d3_src)

    if not os.path.exists(d3_src):
        raise ValueError("d3 src not found at '{0}'".format(d3_src))

    if nbextension:
        try:
            install_nbextension(d3_src,user=True)
        except IOError:
            # files may be read only. We'll try deleting them and re-installing
            from IPython.utils.path import get_ipython_dir
            nbext = os.path.join(get_ipython_dir(), "nbextensions")

            for src in [d3_src]:
                dest = os.path.join(nbext, os.path.basename(src))
                if os.path.exists(dest):
                    os.remove(dest)
            install_nbextension(d3_src, user=True)
Example #11
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])
Example #12
0
def test_get_ipython_dir_3():
    """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
    path.get_home_dir = lambda : "someplace"
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
Example #13
0
def get_config(profile_name='default'):
    basefilename = 'nbx_config'

    ipython_dir = get_ipython_dir()
    profiledir = ProfileDir.find_profile_dir_by_name(ipython_dir, profile_name)
    pyloader = PyFileConfigLoader(basefilename+'.py', path=profiledir.location)
    config = pyloader.load_config()
    return config
Example #14
0
def create_sardana_profile(profile, door_name):

    ipython_dir = get_ipython_dir()
    try:
        ProfileDir.find_profile_dir_by_name(ipython_dir, profile)
    except ProfileDirError:
        from sardana.spock.genutils import create_spock_profile
        create_spock_profile(ipython_dir, "spock", profile, door_name)
Example #15
0
 def load_subconfig(self, fname, path=None, profile=None):
     if profile is not None:
         try:
             profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
         except ProfileDirError:
             return
         path = profile_dir.location
     return super(ProfileAwareConfigLoader, self).load_subconfig(fname, path=path)
Example #16
0
def is_installed(ipydir=None, profile='tango'):
    ipython_dir = ipydir or get_ipython_dir()
    try:
        p_dir = ProfileDir.find_profile_dir_by_name(ipython_dir, profile)
    except ProfileDirError:
        return False
    abs_config_file_name = os.path.join(p_dir.location, _CONFIG_FILE_NAME)
    return os.path.isfile(abs_config_file_name)
Example #17
0
def test_get_ipython_dir_4():
    """test_get_ipython_dir_4, use XDG if both exist."""
    path.get_home_dir = lambda : HOME_TEST_DIR
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
    xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, xdg_ipdir)
Example #18
0
def test_get_ipython_dir_6():
    """test_get_ipython_dir_6, use XDG if defined and neither exist."""
    path.get_home_dir = lambda : 'somehome'
    path.get_xdg_dir = lambda : 'somexdg'
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    xdg_ipdir = os.path.join("somexdg", "ipython")
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, xdg_ipdir)
 def _getEngineInfoLogFile(self):
     # Store all logs inside the ipython directory
     ipdir = get_ipython_dir()
     pjoin = os.path.join
     logdir_base = pjoin(ipdir,'log')
     if not os.path.isdir(logdir_base):
         os.makedirs(logdir_base)
     logfile = os.path.join(logdir_base,'ipcontroller-%s-engine-info.log' % os.getpid())
     return logfile
Example #20
0
def test_get_ipython_dir_5():
    """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
    path.get_home_dir = lambda : HOME_TEST_DIR
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
    os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, IP_TEST_DIR)
Example #21
0
def get_local_magics_dir():
    """
    Ensures that there is a ~/.ipython/metakernel/magics directory,
    and returns the path to it.
    """
    base = get_ipython_dir()
    dname = os.path.join(base, 'metakernel', 'magics')
    if not os.path.exists(dname):
        os.makedirs(dname)
    return dname
Example #22
0
def _find_profile(profile_name):
    from IPython.utils.path import locate_profile, get_ipython_dir
    try:
        profile_path = locate_profile(profile_name)
        exists = True
    except IOError:
        profile_path = os.path.join(get_ipython_dir(),
                                    "profile_%s" % profile_name)
        exists = False
    return profile_path, exists
Example #23
0
def load_default_config(ipython_dir=None):
    """Load the default config file from the default ipython_dir.

    This is useful for embedded shells.
    """
    if ipython_dir is None:
        ipython_dir = get_ipython_dir()
    cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
    config = cl.load_config()
    return config
Example #24
0
def test_get_ipython_dir_6():
    """test_get_ipython_dir_6, use XDG if defined and neither exist."""
    path.get_home_dir = lambda: 'somehome'
    path.get_xdg_dir = lambda: 'somexdg'
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    xdg_ipdir = os.path.join("somexdg", "ipython")
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, xdg_ipdir)
Example #25
0
 def _getEngineInfoLogFile(self):
     # Store all logs inside the ipython directory
     ipdir = get_ipython_dir()
     pjoin = os.path.join
     logdir_base = pjoin(ipdir, 'log')
     if not os.path.isdir(logdir_base):
         os.makedirs(logdir_base)
     logfile = os.path.join(logdir_base,
                            'ipcontroller-%s-engine-info.log' % os.getpid())
     return logfile
Example #26
0
def test_get_ipython_dir_4():
    """test_get_ipython_dir_4, use XDG if both exist."""
    path.get_home_dir = lambda: HOME_TEST_DIR
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
    xdg_ipdir = os.path.join(XDG_TEST_DIR, "ipython")
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, xdg_ipdir)
Example #27
0
def load_default_config(ipython_dir=None):
    """Load the default config file from the default ipython_dir.

    This is useful for embedded shells.
    """
    if ipython_dir is None:
        ipython_dir = get_ipython_dir()
    cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
    config = cl.load_config()
    return config
Example #28
0
def test_get_ipython_dir_5():
    """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
    path.get_home_dir = lambda: HOME_TEST_DIR
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
    os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, IP_TEST_DIR)
Example #29
0
def test_get_ipython_dir_3():
    """test_get_ipython_dir_3, use XDG if defined, and .ipython doesn't exist."""
    path.get_home_dir = lambda: "someplace"
    path._writable_dir = lambda path: True
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, os.path.join(XDG_TEST_DIR, "ipython"))
Example #30
0
def get_ipython_profiles(path=None):
    """list profiles in a given root directory"""
    if path is None:
        path = get_ipython_dir()
    files = os.listdir(path)
    profiles = []
    for f in files:
        full_path = os.path.join(path, f)
        if os.path.isdir(full_path) and f.startswith('profile_'):
            profiles.append(f.split('_',1)[-1])
    return profiles
Example #31
0
def test_get_ipython_dir_2():
    """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
    with patch_get_home_dir('someplace'):
        path.get_xdg_dir = lambda: None
        path._writable_dir = lambda path: True
        os.name = "posix"
        env.pop('IPYTHON_DIR', None)
        env.pop('IPYTHONDIR', None)
        env.pop('XDG_CONFIG_HOME', None)
        ipdir = path.get_ipython_dir()
        nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
Example #32
0
def test_get_ipython_dir_2():
    """test_get_ipython_dir_2, Testcase to see if we can call get_ipython_dir without Exceptions."""
    with patch_get_home_dir('someplace'):
        path.get_xdg_dir = lambda : None
        path._writable_dir = lambda path: True
        os.name = "posix"
        env.pop('IPYTHON_DIR', None)
        env.pop('IPYTHONDIR', None)
        env.pop('XDG_CONFIG_HOME', None)
        ipdir = path.get_ipython_dir()
        nt.assert_equal(ipdir, os.path.join("someplace", ".ipython"))
Example #33
0
def get_ipython_profiles(path=None):
    """list profiles in a given root directory"""
    if path is None:
        path = get_ipython_dir()
    files = os.listdir(path)
    profiles = []
    for f in files:
        full_path = os.path.join(path, f)
        if os.path.isdir(full_path) and f.startswith('profile_'):
            profiles.append(f.split('_', 1)[-1])
    return profiles
Example #34
0
def config_ipw(ipw):
    """Apply and then modify default settings of IPython Qt widget"""
    ipython_dir = get_ipython_dir()
    profile_dir = os.path.join(ipython_dir, 'profile_default')
    cl = PyFileConfigLoader('ipython_qtconsole_config.py', profile_dir)
    config = cl.load_config()
    ipw.config = config

    ipw.set_default_style(colors='Linux')
    ipw.font = QFont('Lucida Console', 11) # 3rd arg can be e.g. QFont.Bold
    ipw.font.setFixedPitch(True)
Example #35
0
def setup():
    cp = Popen(
        'ipcontroller --profile iptest -r --log-level 10 --log-to-file --usethreads'
        .split(),
        stdout=blackhole,
        stderr=STDOUT)
    processes.append(cp)
    engine_json = os.path.join(get_ipython_dir(), 'cluster_iptest', 'security',
                               'ipcontroller-engine.json')
    client_json = os.path.join(get_ipython_dir(), 'cluster_iptest', 'security',
                               'ipcontroller-client.json')
    tic = time.time()
    while not os.path.exists(engine_json) or not os.path.exists(client_json):
        if cp.poll() is not None:
            print cp.poll()
            raise RuntimeError("The test controller failed to start.")
        elif time.time() - tic > 10:
            raise RuntimeError(
                "Timeout waiting for the test controller to start.")
        time.sleep(0.1)
    add_engines(1)
Example #36
0
def list_running_servers(profile='default'):
    """Iterate over the server info files of running notebook servers.
    
    Given a profile name, find nbserver-* files in the security directory of
    that profile, and yield dicts of their information, each one pertaining to
    a currently running notebook server instance.
    """
    pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), name=profile)
    for file in os.listdir(pd.security_dir):
        if file.startswith('nbserver-'):
            with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f:
                yield json.load(f)
Example #37
0
 def load_subconfig(self, fname, path=None, profile=None):
     if profile is not None:
         try:
             profile_dir = ProfileDir.find_profile_dir_by_name(
                 get_ipython_dir(),
                 profile,
             )
         except ProfileDirError:
             return
         path = profile_dir.location
     return super(ProfileAwareConfigLoader, self).load_subconfig(fname,
                                                                 path=path)
Example #38
0
def list_running_servers(profile='default'):
    """Iterate over the server info files of running notebook servers.
    
    Given a profile name, find nbserver-* files in the security directory of
    that profile, and yield dicts of their information, each one pertaining to
    a currently running notebook server instance.
    """
    pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), name=profile)
    for file in os.listdir(pd.security_dir):
        if file.startswith('nbserver-'):
            with io.open(os.path.join(pd.security_dir, file), encoding='utf-8') as f:
                yield json.load(f)
Example #39
0
def test_get_ipython_dir_8():
    """test_get_ipython_dir_8, test / home directory"""
    old = path._writable_dir, path.get_xdg_dir
    try:
        path._writable_dir = lambda path: bool(path)
        path.get_xdg_dir = lambda: None
        env.pop("IPYTHON_DIR", None)
        env.pop("IPYTHONDIR", None)
        env["HOME"] = "/"
        nt.assert_equal(path.get_ipython_dir(), "/.ipython")
    finally:
        path._writable_dir, path.get_xdg_dir = old
Example #40
0
def test_get_ipython_dir_8():
    """test_get_ipython_dir_8, test / home directory"""
    old = path._writable_dir, path.get_xdg_dir
    try:
        path._writable_dir = lambda path: bool(path)
        path.get_xdg_dir = lambda: None
        env.pop('IPYTHON_DIR', None)
        env.pop('IPYTHONDIR', None)
        env['HOME'] = '/'
        nt.assert_equal(path.get_ipython_dir(), '/.ipython')
    finally:
        path._writable_dir, path.get_xdg_dir = old
Example #41
0
def test_get_ipython_dir_8():
    """test_get_ipython_dir_8, test / home directory"""
    old = path._writable_dir, path.get_xdg_dir
    try:
        path._writable_dir = lambda path: bool(path)
        path.get_xdg_dir = lambda: None
        env.pop('IPYTHON_DIR', None)
        env.pop('IPYTHONDIR', None)
        env['HOME'] = '/'
        nt.assert_equal(path.get_ipython_dir(), '/.ipython')
    finally:
        path._writable_dir, path.get_xdg_dir = old
Example #42
0
def test_get_ipython_dir_4():
    """test_get_ipython_dir_4, use XDG if both exist."""
    path.get_home_dir = lambda: HOME_TEST_DIR
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
    ipdir = path.get_ipython_dir()
    if sys.platform == "darwin":
        expected = os.path.join(HOME_TEST_DIR, ".ipython")
    else:
        expected = os.path.join(XDG_TEST_DIR, "ipython")
    nt.assert_equal(ipdir, expected)
Example #43
0
def test_not_writable_ipdir():
    tmpdir = tempfile.mkdtemp()
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env.pop('XDG_CONFIG_HOME', None)
    env['HOME'] = tmpdir
    ipdir = os.path.join(tmpdir, '.ipython')
    os.mkdir(ipdir)
    os.chmod(ipdir, 600)
    with AssertPrints('is not a writable location', channel='stderr'):
        ipdir = path.get_ipython_dir()
    env.pop('IPYTHON_DIR', None)
Example #44
0
    def create_default_config(self):
        """Create defaults that can't be set elsewhere.

        For the most part, we try to set default in the class attributes
        of Configurables.  But, defaults the top-level Application (which is
        not a HasTraits or Configurables) are not set in this way.  Instead
        we set them here.  The Global section is for variables like this that
        don't belong to a particular configurable.
        """
        c = Config()
        c.Global.ipython_dir = get_ipython_dir()
        c.Global.log_level = self.log_level
        self.default_config = c
Example #45
0
def test_not_writable_ipdir():
    tmpdir = tempfile.mkdtemp()
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env.pop('XDG_CONFIG_HOME', None)
    env['HOME'] = tmpdir
    ipdir = os.path.join(tmpdir, '.ipython')
    os.mkdir(ipdir)
    os.chmod(ipdir, 600)
    with AssertPrints('is not a writable location', channel='stderr'):
        ipdir = path.get_ipython_dir()
    env.pop('IPYTHON_DIR', None)
 def update_profiles(self):
     """List all profiles in the ipython_dir and cwd.
     """
     for path in [get_ipython_dir(), os.getcwdu()]:
         for profile in list_profiles_in(path):
             pd = self.get_profile_dir(profile, path)
             if profile not in self.profiles:
                 self.log.debug("Adding cluster profile '%s'" % profile)
                 self.profiles[profile] = {
                     'profile': profile,
                     'profile_dir': pd,
                     'status': 'stopped'
                 }
Example #47
0
def get_args(argv):

    script_name = argv[0]
    _, session = os.path.split(script_name)
    script_name = os.path.realpath(script_name)

    macro_server = None
    door = None

    # Define the profile file
    profile = "spockdoor"
    try:
        for _, arg in enumerate(argv[1:]):
            if arg.startswith('--profile='):
                profile = arg[10:]
                break
        else:
            argv.append("--profile=" + profile)
    except:
        pass

    ipython_dir = get_ipython_dir()
    try:
        ProfileDir.find_profile_dir_by_name(ipython_dir, profile)
    except ProfileDirError:
        r = ''
        while not r in ('y', 'n'):
            prompt = 'Profile \'%s\' does not exist. Do you want to create '\
                     'one now ([y]/n)? ' % profile
            r = raw_input(prompt) or 'y'
        if r.lower() == 'y':
            create_spock_profile(ipython_dir, profile)
        else:
            sys.stdout.write(
                'No spock door extension profile was created. Starting normal spock...\n'
            )
            sys.stdout.flush()
            profile = ''

    # inform the shell of the profile it should use
    if not '--profile=' in argv and profile:
        argv.append('--profile=' + profile)

    user_ns = {
        'MACRO_SERVER_NAME': macro_server,
        'DOOR_NAME': door,
        'PROFILE': profile
    }

    return user_ns
Example #48
0
def test_get_ipython_dir_5():
    """test_get_ipython_dir_5, use .ipython if exists and XDG defined, but doesn't exist."""
    with patch_get_home_dir(HOME_TEST_DIR):
        os.name = "posix"
        env.pop('IPYTHON_DIR', None)
        env.pop('IPYTHONDIR', None)
        env['XDG_CONFIG_HOME'] = XDG_TEST_DIR
        try:
            os.rmdir(os.path.join(XDG_TEST_DIR, 'ipython'))
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise
        ipdir = path.get_ipython_dir()
        nt.assert_equal(ipdir, IP_TEST_DIR)
Example #49
0
    def __init__(self):
        """
        Utility to manage Sage kernels

        EXAMPLES::

            sage: from sage.repl.ipython_kernel.install import SageKernelSpec
            sage: spec = SageKernelSpec()
            sage: spec._display_name    # random output
            'Sage 6.6.beta2'
        """
        self._display_name = 'Sage {0}'.format(SAGE_VERSION)
        self._ipython_dir = get_ipython_dir()
        self._mkdirs()
Example #50
0
def test_get_ipython_dir_6():
    """test_get_ipython_dir_6, use XDG if defined and neither exist."""
    xdg = os.path.join(HOME_TEST_DIR, 'somexdg')
    os.mkdir(xdg)
    shutil.rmtree(os.path.join(HOME_TEST_DIR, '.ipython'))
    path.get_home_dir = lambda: HOME_TEST_DIR
    path.get_xdg_dir = lambda: xdg
    os.name = "posix"
    env.pop('IPYTHON_DIR', None)
    env.pop('IPYTHONDIR', None)
    env.pop('XDG_CONFIG_HOME', None)
    xdg_ipdir = os.path.join(xdg, "ipython")
    ipdir = path.get_ipython_dir()
    nt.assert_equal(ipdir, xdg_ipdir)
Example #51
0
    def _select_profile(self):
        # See IPython.core.profileapp:list_profile_in()
        profiles = []
        for filename in os.listdir(get_ipython_dir()):
            if filename.startswith('profile_'):
                profiles.append(filename[8:])

        if profiles == ['default'] and not qt_available:
            self.profile = 'default'
        elif not qt_available:
            raise ValueError("'default' IPython profile does not exist "
                             "and PyQt4 is not available")
        else:
            self.profile = choose_profile(profiles)
Example #52
0
class BasicConfig(LinkedConfig):
    """Config options that inherited from IPython."""

    profile = Unicode('nbgrader',
                      config=True,
                      help="Default IPython profile to use")

    overwrite = Bool(
        False,
        config=True,
        help="Whether to overwrite existing config files when copying")

    auto_create = Bool(True,
                       config=True,
                       help="Whether to automatically generate the profile")

    extra_config_file = Unicode(config=True,
                                help=dedent("""
            Path to an extra config file to load. If specified, load this config
            file in addition to any other IPython config.
            """))

    copy_config_files = Bool(False,
                             config=True,
                             help=dedent("""
            Whether to install the default config files into the profile dir.
            If a new profile is being created, and IPython contains config files
            for that profile, then they will be staged into the new directory.
            Otherwise, default config files will be automatically generated.
            """))

    ipython_dir = Unicode(get_ipython_dir(),
                          config=True,
                          help=dedent("""
            The name of the IPython directory. This directory is used for logging
            configuration (through profiles), history storage, etc. The default
            is usually $HOME/.ipython. This option can also be specified through
            the environment variable IPYTHONDIR.
            """))

    log_datefmt = Unicode(
        "%Y-%m-%d %H:%M:%S",
        config=True,
        help="The date format used by logging formatters for %(asctime)s")

    log_format = Unicode(
        "%(color)s[%(name)s | %(levelname)s]%(end_color)s %(message)s",
        config=True,
        help="The logging format template")
def copyNeededFilesToWebServer():

    nbextension = True
    try:
        if IPython.version_info[0] >= 4:
            from notebook import install_nbextension
        else:
            from IPython.html import install_nbextension
    except ImportError:
        nbextension = False

    moduleDir = os.path.dirname(os.path.abspath(__file__))

    tulipjs_files = os.path.join(moduleDir, 'tulipjs')

    required_files = [tulipjs_files]

    if nbextension:

        def _install_nbextension(extensions):
            """Wrapper for IPython.html.install_nbextension."""
            if IPython.version_info[0] >= 3:
                for extension in extensions:
                    install_nbextension(extension, user=True, verbose=0)
            else:
                install_nbextension(extensions, verbose=0)

        try:
            _install_nbextension(required_files)
        except IOError:
            # files may be read only. We'll try deleting them and re-installing
            from IPython.utils.path import get_ipython_dir
            nbext = os.path.join(get_ipython_dir(), "nbextensions")

            for req_file in required_files:
                dest = os.path.join(nbext, os.path.basename(req_file))
                if os.path.exists(dest):
                    os.remove(dest)
            _install_nbextension(required_files)

    prefix = '/nbextensions/tulipjs/'
    urls = {}
    urls['tulipjs'] = prefix + 'tulip.js'
    urls['base64utilsjs'] = prefix + 'base64utils.js'
    urls['jquerytoolbarjs'] = prefix + 'jquery.toolbar.min.js'
    urls['jquerytoolbarcss'] = prefix + 'css/jquery.toolbar.css'
    urls['fontawesomecss'] = prefix + 'css/font-awesome.min.css'

    return urls
Example #54
0
def load_default_config(ipython_dir=None):
    """Load the default config file from the default ipython_dir.

    This is useful for embedded shells.
    """
    if ipython_dir is None:
        ipython_dir = get_ipython_dir()
    profile_dir = os.path.join(ipython_dir, 'profile_default')
    cl = PyFileConfigLoader("ipython_config.py", profile_dir)
    try:
        config = cl.load_config()
    except ConfigFileNotFound:
        # no config found
        config = Config()
    return config
Example #55
0
def load_default_config(ipython_dir=None):
    """Load the default config file from the default ipython_dir.

    This is useful for embedded shells.
    """
    if ipython_dir is None:
        ipython_dir = get_ipython_dir()

    profile_dir = os.path.join(ipython_dir, 'profile_default')

    config = Config()
    for cf in Application._load_config_files("ipython_config", path=profile_dir):
        config.update(cf)

    return config
Example #56
0
    def _find_furl(self, profile='default', cluster_dir=None, 
                   furl_or_file=None, furl_file_name=None,
                   ipython_dir=None):
        """Find a FURL file.

        If successful, this returns a FURL file that exists on the file
        system. The contents of the file have not been checked though. This
        is because we often have to deal with FURL file whose buffers have
        not been flushed.

        This raises an :exc:`~IPython.kernel.fcutil.FURLError` exception 
        if a FURL file can't be found.

        This tries the following:
        
        1. By the name ``furl_or_file``.
        2. By ``cluster_dir`` and ``furl_file_name``.
        3. By cluster profile with a default of ``default``. This uses
           ``ipython_dir``.
        """
        # Try by furl_or_file
        if furl_or_file is not None:
            if is_valid_furl_or_file(furl_or_file):
                return furl_or_file

        if furl_file_name is None:
            raise FURLError('A furl_file_name must be provided if furl_or_file is not')

        # Try by cluster_dir
        if cluster_dir is not None:
            cluster_dir_obj = ClusterDir.find_cluster_dir(cluster_dir)
            sdir = cluster_dir_obj.security_dir
            furl_file = os.path.join(sdir, furl_file_name)
            validate_furl_or_file(furl_file)
            return furl_file

        # Try by profile
        if ipython_dir is None:
            ipython_dir = get_ipython_dir()
        if profile is not None:
            cluster_dir_obj = ClusterDir.find_cluster_dir_by_profile(
                ipython_dir, profile)
            sdir = cluster_dir_obj.security_dir
            furl_file = os.path.join(sdir, furl_file_name)
            validate_furl_or_file(furl_file)
            return furl_file

        raise FURLError('Could not find a valid FURL file.')
Example #57
0
def prepare_cmdline(argv=None):
    if argv is None:
        argv = sys.argv

    script_name = argv[0]
    _, session = os.path.split(script_name)
    script_name = os.path.realpath(script_name)

    # Define the profile file
    profile, append_profile = "spockdoor", True
    try:
        # in ipython the last option in the list takes precedence
        # so reversing order for searching of the profile
        reversed_argv = reversed(argv[1:])
        for _, arg in enumerate(reversed_argv):
            if arg.startswith('--profile='):
                profile = arg[10:]
                append_profile = False
                break
    except:
        pass

    ipython_dir = get_ipython_dir()
    try:
        pd = ProfileDir.find_profile_dir_by_name(ipython_dir, profile)
    except ProfileDirError:
        r = ''
        while not r in ('y', 'n'):
            prompt = "Profile '%s' does not exist. Do you want to create "\
                     "one now ([y]/n)? " % profile
            r = raw_input(prompt) or 'y'
        if r.lower() == 'y':
            create_spock_profile(ipython_dir, profile)
        else:
            sys.stdout.write('No spock profile was created. '
                             'Starting ipython with default profile...\n')
            sys.stdout.flush()
            # removing all options refering to profile
            for _, arg in enumerate(argv[1:]):
                if arg.startswith('--profile='):
                    argv.remove(arg)
            return
    else:
        ipy_profile_dir = pd.location  # directory with the spock profile
        check_for_upgrade(ipy_profile_dir)

    if append_profile:
        argv.append("--profile=" + profile)
Example #58
0
def _get_nbext_dir(nbextensions_dir=None, user=False, prefix=None):
    """Return the nbextension directory specified"""
    if sum(map(bool, [user, prefix, nbextensions_dir])) > 1:
        raise ArgumentConflict(
            "Cannot specify more than one of user, prefix, or nbextensions_dir."
        )
    if user:
        nbext = pjoin(get_ipython_dir(), u'nbextensions')
    else:
        if prefix:
            nbext = pjoin(prefix, 'share', 'jupyter', 'nbextensions')
        elif nbextensions_dir:
            nbext = nbextensions_dir
        else:
            nbext = SYSTEM_NBEXTENSIONS_INSTALL_DIR
    return nbext