Example #1
0
def create_spock_profile(userdir, profile, door_name=None):
    """Create spock profile directory and configuration file from a template
    file

    :param userdir: directory where the spock profile will be created
    :param profile: profile name
    :param door_name: door name, if None, user will be asked for the door name
    :"""
    if not os.path.isdir(userdir):
        ProfileDir.create_profile_dir(userdir)
    p_dir = ProfileDir.create_profile_dir_by_name(userdir, profile)

    ipy_profile_dir = p_dir.location

    try:
        _create_config_file(ipy_profile_dir)
    # catch BaseException in order to catch also KeyboardInterrupt
    except BaseException:
        import shutil
        try:
            shutil.rmtree(ipy_profile_dir)
        except OSError:
            msg = ('Could not remove spock profile directory {0}. '
                   'Remove it by hand e.g. rmdir {0}').format(ipy_profile_dir)
            print(msg)
        sys.exit(-1)
Example #2
0
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 #3
0
    def init_profile_dir(self):
        """initialize the profile dir"""
        self._in_init_profile_dir = True
        if self.profile_dir is not None:
            # already ran
            return
        if 'ProfileDir.location' not in self.config:
            # location not specified, find by profile name
            try:
                p = ProfileDir.find_profile_dir_by_name(
                    self.ipython_dir, self.profile, self.config)
            except ProfileDirError:
                # not found, maybe create it (always create default profile)
                if self.auto_create or self.profile == 'default':
                    try:
                        p = ProfileDir.create_profile_dir_by_name(
                            self.ipython_dir, self.profile, self.config)
                    except ProfileDirError:
                        self.log.fatal(
                            "Could not create profile: %r" % self.profile)
                        self.exit(1)
                    else:
                        self.log.info("Created profile dir: %r" % p.location)
                else:
                    self.log.fatal("Profile %r not found." % self.profile)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r" % p.location)
        else:
            location = self.config.ProfileDir.location
            # location is fully specified
            try:
                p = ProfileDir.find_profile_dir(location, self.config)
            except ProfileDirError:
                # not found, maybe create it
                if self.auto_create:
                    try:
                        p = ProfileDir.create_profile_dir(
                            location, self.config)
                    except ProfileDirError:
                        self.log.fatal(
                            "Could not create profile directory: %r" % location)
                        self.exit(1)
                    else:
                        self.log.info(
                            "Creating new profile dir: %r" % location)
                else:
                    self.log.fatal(
                        "Profile directory %r not found." % location)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r" % location)
            # if profile_dir is specified explicitly, set profile name
            dir_name = os.path.basename(p.location)
            if dir_name.startswith('profile_'):
                self.profile = dir_name[8:]

        self.profile_dir = p
        self.config_file_paths.append(p.location)
        self._in_init_profile_dir = False
Example #4
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 #5
0
 def ensure_profile():
     """ """
     canonical_prof = os.path.join(SMASHLIB_DIR, 'ipython_config.py')
     ProfileDir.create_profile_dir_by_name(SMASH_DIR, main_profile_name)
     profile_dir = os.path.join(SMASH_DIR, 'profile_' + main_profile_name)
     config_file = os.path.join(profile_dir, 'ipython_config.py')
     shutil.copy(canonical_prof, config_file,)
     return profile_dir
Example #6
0
    def init_profile_dir(self):
        """initialize the profile dir"""
        try:
            # location explicitly specified:
            location = self.config.ProfileDir.location
        except AttributeError:
            # location not specified, find by profile name
            try:
                p = ProfileDir.find_profile_dir_by_name(
                    self.ipython_dir, self.profile, self.config)
            except ProfileDirError:
                # not found, maybe create it (always create default profile)
                if self.auto_create or self.profile == 'python3':
                    try:
                        p = ProfileDir.create_profile_dir_by_name(
                            self.ipython_dir, self.profile, self.config)
                    except ProfileDirError:
                        self.log.fatal(
                            "Could not create profile: %r" % self.profile)
                        self.exit(1)
                    else:
                        self.log.info("Created profile dir: %r" % p.location)
                else:
                    self.log.fatal("Profile %r not found." % self.profile)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r" % p.location)
        else:
            # location is fully specified
            try:
                p = ProfileDir.find_profile_dir(location, self.config)
            except ProfileDirError:
                # not found, maybe create it
                if self.auto_create:
                    try:
                        p = ProfileDir.create_profile_dir(
                            location, self.config)
                    except ProfileDirError:
                        self.log.fatal("Could not create profile directory: %r"
                                       % location)
                        self.exit(1)
                    else:
                        self.log.info(
                            "Creating new profile dir: %r" % location)
                else:
                    self.log.fatal(
                        "Profile directory %r not found." % location)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r" % location)

        self.profile_dir = p
        self.config_file_paths.append(p.location)
Example #7
0
def create_spock_profile(userdir, profile, door_name=None):
    """Create spock profile directory and configuration file from a template
    file

    :param userdir: directory where the spock profile will be created
    :param profile: profile name
    :param door_name: door name, if None, user will be asked for the door name
    """

    if not os.path.isdir(userdir):
        ProfileDir.create_profile_dir(userdir)
    p_dir = ProfileDir.create_profile_dir_by_name(userdir, profile)
    ipy_profile_dir = p_dir.location

    _create_config_file(ipy_profile_dir)
Example #8
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 #9
0
    def __init__(self, exec_lines=None):

        self.cout = StringIO()

        if exec_lines is None:
            exec_lines = []

        # Create config object for IPython
        config = Config()
        config.HistoryManager.hist_file = ":memory:"
        config.InteractiveShell.autocall = False
        config.InteractiveShell.autoindent = False
        config.InteractiveShell.colors = "NoColor"

        # create a profile so instance history isn't saved
        tmp_profile_dir = tempfile.mkdtemp(prefix="profile_")
        profname = "auto_profile_sphinx_build"
        pdir = os.path.join(tmp_profile_dir, profname)
        profile = ProfileDir.create_profile_dir(pdir)

        # Create and initialize global ipython, but don't start its mainloop.
        # This will persist across different EmbededSphinxShell instances.
        IP = InteractiveShell.instance(config=config, profile_dir=profile)
        atexit.register(self.cleanup)

        sys.stdout = self.cout
        sys.stderr = self.cout

        # For debugging, so we can see normal output, use this:
        # from IPython.utils.io import Tee
        # sys.stdout = Tee(self.cout, channel='stdout') # dbg
        # sys.stderr = Tee(self.cout, channel='stderr') # dbg

        # Store a few parts of IPython we'll need.
        self.IP = IP
        self.user_ns = self.IP.user_ns
        self.user_global_ns = self.IP.user_global_ns

        self.input = ""
        self.output = ""
        self.tmp_profile_dir = tmp_profile_dir

        self.is_verbatim = False
        self.is_doctest = False
        self.is_suppress = False

        # Optionally, provide more detailed information to shell.
        # this is assigned by the SetUp method of IPythonDirective
        # to point at itself.
        #
        # So, you can access handy things at self.directive.state
        self.directive = None

        # on the first call to the savefig decorator, we'll import
        # pyplot as plt so we can make a call to the plt.gcf().savefig
        self._pyplot_imported = False

        # Prepopulate the namespace.
        for line in exec_lines:
            self.process_input_line(line, store_history=False)
Example #10
0
def test_list_profiles_in():
    # No need to remove these directories and files, as they will get nuked in
    # the module-level teardown.
    td = tempfile.mkdtemp(dir=TMP_TEST_DIR)
    td = py3compat.str_to_unicode(td)
    for name in ('profile_foo', 'profile_hello', 'not_a_profile'):
        os.mkdir(os.path.join(td, name))
    if dec.unicode_paths:
        os.mkdir(os.path.join(td, 'profile_ünicode'))

    with open(os.path.join(td, 'profile_file'), 'w') as f:
        f.write("I am not a profile directory")
    profiles = list_profiles_in(td)
    
    # unicode normalization can turn u'ünicode' into u'u\0308nicode',
    # so only check for *nicode, and that creating a ProfileDir from the
    # name remains valid
    found_unicode = False
    for p in list(profiles):
        if p.endswith('nicode'):
            pd = ProfileDir.find_profile_dir_by_name(td, p)
            profiles.remove(p)
            found_unicode = True
            break
    if dec.unicode_paths:
        nt.assert_true(found_unicode)
    nt.assert_equal(set(profiles), set(['foo', 'hello']))
Example #11
0
def get_security_file(filename, profile='default'):
    """Return the absolute path of a security file given by filename and profile
    
    This allows users and developers to find security files without
    knowledge of the IPython directory structure. The search path
    will be ['.', profile.security_dir]
    
    Parameters
    ----------
    
    filename : str
        The file to be found. If it is passed as an absolute path, it will
        simply be returned.
    profile : str [default: 'default']
        The name of the profile to search.  Leaving this unspecified
        The file to be found. If it is passed as an absolute path, fname will
        simply be returned.
    
    Returns
    -------
    Raises :exc:`IOError` if file not found or returns absolute path to file.
    """
    # import here, because profiledir also imports from utils.path
    from IPython.core.profiledir import ProfileDir
    try:
        pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
    except Exception:
        # will raise ProfileDirError if no such profile
        raise IOError("Profile %r not found")
    return filefind(filename, ['.', pd.security_dir])
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
0
def install(ipydir=None, verbose=True, profile='tango'):
    if verbose:
        def out(msg):
            sys.stdout.write(msg)
            sys.stdout.flush()
    else:
        out = lambda x: None

    ipython_dir = ipydir or get_ipython_dir()
    ensure_dir_exists(ipython_dir)
    try:
        p_dir = ProfileDir.find_profile_dir_by_name(ipython_dir, profile)
    except ProfileDirError:
        p_dir = ProfileDir.create_profile_dir_by_name(ipython_dir, profile)
    abs_config_file_name = os.path.join(p_dir.location, _CONFIG_FILE_NAME)
    create_config = True
    if os.path.isfile(abs_config_file_name):
        msg = "Tango configuration file {0} already exists.\n"
        msg += "Do you wish to replace it (y/n)?"
        msg = msg.format(abs_config_file_name)
        create_config = ask_yes_no(msg, default='y')

    if not create_config:
        return

    out("Installing tango extension to ipython... ")

    profile = __PROFILE.format(pytangover=tango.Release.version,
                               ipyver=IPython.release.version,
                               protected_block=__PROTECTED_BLOCK)
    with open(abs_config_file_name, "w") as f:
        f.write(profile)
        f.close()
    out("[DONE]\n\n")
    out("""\
To start ipython with tango interface simply type on the command line:
%% ipython --profile=tango

For more information goto:
http://pytango.readthedocs.io

Have fun with ITango!
The PyTango team
""")
    def __init__(self, exec_lines=None,state=None):

        self.cout = DecodingStringIO(u'')

        if exec_lines is None:
            exec_lines = []

        self.state = state

        # Create config object for IPython
        config = Config()
        config.InteractiveShell.autocall = False
        config.InteractiveShell.autoindent = False
        config.InteractiveShell.colors = 'NoColor'

        # create a profile so instance history isn't saved
        tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
        profname = 'auto_profile_sphinx_build'
        pdir = os.path.join(tmp_profile_dir,profname)
        profile = ProfileDir.create_profile_dir(pdir)

        # Create and initialize global ipython, but don't start its mainloop.
        # This will persist across different EmbededSphinxShell instances.
        IP = InteractiveShell.instance(config=config, profile_dir=profile)

        # io.stdout redirect must be done after instantiating InteractiveShell
        io.stdout = self.cout
        io.stderr = self.cout

        # For debugging, so we can see normal output, use this:
        #from IPython.utils.io import Tee
        #io.stdout = Tee(self.cout, channel='stdout') # dbg
        #io.stderr = Tee(self.cout, channel='stderr') # dbg

        # Store a few parts of IPython we'll need.
        self.IP = IP
        self.user_ns = self.IP.user_ns
        self.user_global_ns = self.IP.user_global_ns

        self.input = ''
        self.output = ''

        self.is_verbatim = False
        self.is_doctest = False
        self.is_suppress = False

        # Optionally, provide more detailed information to shell.
        self.directive = None

        # on the first call to the savefig decorator, we'll import
        # pyplot as plt so we can make a call to the plt.gcf().savefig
        self._pyplot_imported = False

        # Prepopulate the namespace.
        for line in exec_lines:
            self.process_input_line(line, store_history=False)
Example #19
0
def find_connection_file(filename='kernel-*.json', profile=None):
    """DEPRECATED: find a connection file, and return its absolute path.
    
    THIS FUNCION IS DEPRECATED. Use juptyer_client.find_connection_file instead.

    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.
    """
    
    import warnings
    warnings.warn("""ipykernel.find_connection_file is deprecated, use jupyter_client.find_connection_file""",
        DeprecationWarning, stacklevel=2)
    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 #20
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)
    if not os.path.isfile(abs_config_file_name):
        return False
    with open(abs_config_file_name) as f:
        return __PROTECTED_BLOCK in f.read()
Example #21
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 #22
0
def locate_profile(profile='default'):
    """Find the path to the folder associated with a given profile.
    
    I.e. find $IPYTHONDIR/profile_whatever.
    """
    from IPython.core.profiledir import ProfileDir, ProfileDirError
    try:
        pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
    except ProfileDirError:
        # IOError makes more sense when people are expecting a path
        raise IOError("Couldn't find profile %r" % profile)
    return pd.location
Example #23
0
def install(ipydir=None, verbose=True, profile='tango'):
    if verbose:
        def out(msg):
            sys.stdout.write(msg)
            sys.stdout.flush()
    else:
        out = lambda x : None
    
    ipython_dir = ipydir or get_ipython_dir()
    try:
        p_dir = ProfileDir.find_profile_dir_by_name(ipython_dir, profile)
    except ProfileDirError:
        p_dir = ProfileDir.create_profile_dir_by_name(ipython_dir, profile)
    abs_config_file_name = os.path.join(p_dir.location, _CONFIG_FILE_NAME)
    create_config = True
    if os.path.isfile(abs_config_file_name):
        create_config = ask_yes_no("Tango configuration file already exists. "\
                                   "Do you wish to replace it?", default='y')
    
    if not create_config:
        return

    out("Installing tango extension to ipython... ")

    profile = __PROFILE.format(pytangover=PyTango.Release.version,
                               ipyver=IPython.release.version)
    with open(abs_config_file_name, "w") as f:
        f.write(profile)
        f.close()
    out("[DONE]\n\n")
    out("""\
To start ipython with tango interface simply type on the command line:
%% ipython --profile=tango

For more information goto:
http://www.tango-controls.org/static/PyTango/latest/doc/html/

Have fun with ITango!
The PyTango team
""")
Example #24
0
def test_startup_py():
    # create profile dir
    pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test')
    # write startup python file
    with open(os.path.join(pd.startup_dir, '00-start.py'), 'w') as f:
        f.write('zzz=123\n')
    # write simple test file, to check that the startup file was run
    fname = os.path.join(TMP_TEST_DIR, 'test.py')
    with open(fname, 'w') as f:
        f.write('print zzz\n')
    # validate output
    tt.ipexec_validate(fname, '123', '', 
        options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test'])
Example #25
0
def test_startup_ipy():
    # create profile dir
    pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test')
    # write startup ipython file
    with open(os.path.join(pd.startup_dir, '00-start.ipy'), 'w') as f:
        f.write('%profile\n')
    # write empty script, because we don't need anything to happen
    # after the startup file is run
    fname = os.path.join(TMP_TEST_DIR, 'test.py')
    with open(fname, 'w') as f:
        f.write('')
    # validate output
    tt.ipexec_validate(fname, 'test', '', 
        options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test'])
Example #26
0
    def __init__(self):

        self.cout = cStringIO.StringIO()

        # Create config object for IPython
        config = Config()
        config.Global.display_banner = False
        config.Global.exec_lines = ['import numpy as np',
                                    'from pylab import *'
                                    ]
        config.InteractiveShell.autocall = False
        config.InteractiveShell.autoindent = False
        config.InteractiveShell.colors = 'NoColor'
        config.InteractiveShell.cache_size = 0

        # create a profile so instance history isn't saved
        tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
        profname = 'auto_profile_sphinx_build'
        pdir = os.path.join(tmp_profile_dir, profname)
        profile = ProfileDir.create_profile_dir(pdir)

        # Create and initialize ipython, but don't start its mainloop
        IP = InteractiveShell.instance(config=config, profile_dir=profile)

        # io.stdout redirect must be done *after* instantiating
        # InteractiveShell
        io.stdout = self.cout
        io.stderr = self.cout

        # For debugging, so we can see normal output, use this:
        # from IPython.utils.io import Tee
        # io.stdout = Tee(self.cout, channel='stdout') # dbg
        # io.stderr = Tee(self.cout, channel='stderr') # dbg

        # Store a few parts of IPython we'll need.
        self.IP = IP
        self.user_ns = self.IP.user_ns
        self.user_global_ns = self.IP.user_global_ns

        self.input = ''
        self.output = ''

        self.is_verbatim = False
        self.is_doctest = False
        self.is_suppress = False

        # on the first call to the savefig decorator, we'll import
        # pyplot as plt so we can make a call to the plt.gcf().savefig
        self._pyplot_imported = False
Example #27
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 #28
0
    def load_config_file(self, *args, **kwds):
        from IPython.config.loader import PyFileConfigLoader, ConfigFileNotFound
        from IPython.core.profiledir import ProfileDir
        from IPython.utils.path import get_ipython_dir

        conf = Config()
        conf._merge(DEFAULT_SAGE_CONFIG)
        conf._merge(self.command_line_config)

        # Get user config.
        sage_profile_dir = ProfileDir.find_profile_dir_by_name(
            get_ipython_dir(), 'sage').location
        try:
            cl = PyFileConfigLoader('ipython_config.py', sage_profile_dir)
            conf._merge(cl.load_config())
        except ConfigFileNotFound:
            pass
        self.update_config(conf)
def client_json_file(profile='default', cluster_id=None):
    """
    Get the path to the ipcontroller-client.json file. This really shouldn't be necessary, except that
    IPython doesn't automatically insert the cluster_id in the way that it should. I submitted a pull
    request to fix it, but here is a monkey patch in the mean time
    """
    from IPython.core.profiledir import ProfileDir
    from IPython.utils.path import get_ipython_dir
    
    profile_dir = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), profile)
    if not cluster_id:
        client_json = 'ipcontroller-client.json'
    else:
        client_json = 'ipcontroller-%s-client.json' % cluster_id
    filename = os.path.join(profile_dir.security_dir, client_json)
    if not os.path.exists(filename):
        raise ValueError('controller information not found at: %s' % filename)
    return filename
Example #30
0
        def load_subconfig(fname, profile=None):
            # import here to prevent circular imports
            from IPython.core.profiledir import ProfileDir, ProfileDirError

            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
            else:
                path = self.path
            loader = PyFileConfigLoader(fname, path)
            try:
                sub_config = loader.load_config()
            except IOError:
                # Pass silently if the sub config is not there. This happens
                # when a user s using a profile, but not the default config.
                pass
            else:
                self.config._merge(sub_config)
Example #31
0
def test_ipcluster_start_stop(request, ipython_dir, daemonize):
    default_profile = ProfileDir.find_profile_dir_by_name(ipython_dir)
    default_profile_dir = default_profile.location

    # cleanup the security directory to avoid leaking files from one test to the next
    def cleanup_security():
        for f in glob.glob(os.path.join(default_profile.security_dir,
                                        "*.json")):
            print(f"Cleaning up {f}")
            try:
                os.remove(f)
            except Exception as e:
                print(f"Error removing {f}: {e}")

    request.addfinalizer(cleanup_security)

    n = 2
    start_args = ["-n", str(n)]
    if daemonize:
        start_args.append("--daemonize")
    start = Popen(
        [sys.executable, "-m", "ipyparallel.cluster", "start", "--debug"] +
        start_args)
    request.addfinalizer(start.terminate)
    if daemonize:
        # if daemonize, should exit after starting
        start.wait(30)
    else:
        # wait for file to appear
        # only need this if not daemonize
        cluster_file = ipp.Cluster(profile_dir=default_profile_dir,
                                   cluster_id="").cluster_file
        for i in range(100):
            if os.path.isfile(cluster_file) or start.poll() is not None:
                break
            else:
                time.sleep(0.1)
        assert os.path.isfile(cluster_file)

    # list should show a file
    out = ipcluster_list()
    assert len(out.splitlines()) == 2

    # cluster running, try to connect with default args
    cluster = ipp.Cluster.from_file(log_level=10)

    try:
        with cluster.connect_client_sync() as rc:
            rc.wait_for_engines(n=2, timeout=60)
            rc[:].apply_async(os.getpid).get(timeout=10)
    except Exception:
        print("controller output")
        print(cluster.controller.get_output())
        print("engine output")
        for engine_set in cluster.engines.values():
            print(engine_set.get_output())
        raise

    # stop with ipcluster stop
    check_call([sys.executable, "-m", "ipyparallel.cluster", "stop"])
    # start should exit when this happens
    start.wait(30)

    # and ipcluster list should return empty
    out = ipcluster_list()
    assert len(out.splitlines()) == 1

    # stop all succeeds even if there's nothing to stop
    check_call([sys.executable, "-m", "ipyparallel.cluster", "stop", "--all"])
Example #32
0
 def setUp(self):
     # create profile dir
     self.pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test')
     self.options = ['--ipython-dir', IP_TEST_DIR, '--profile', 'test']
     self.fname = os.path.join(TMP_TEST_DIR, 'test.py')
Example #33
0
 def get_profile_dir(self, name, path):
     p = ProfileDir.find_profile_dir_by_name(path, name=name)
     return p.location
Example #34
0
 def setUp(self):
     # create profile dir
     self.pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, "test")
     self.options = ["--ipython-dir", IP_TEST_DIR, "--profile", "test"]
     self.fname = TMP_TEST_DIR / "test.py"
    def __init__(self, exec_lines=None):

        self.cout = StringIO()

        if exec_lines is None:
            exec_lines = []

        # Create config object for IPython
        config = Config()
        config.HistoryManager.hist_file = ':memory:'
        config.InteractiveShell.autocall = False
        config.InteractiveShell.autoindent = False
        config.InteractiveShell.colors = 'NoColor'

        # create a profile so instance history isn't saved
        tmp_profile_dir = tempfile.mkdtemp(prefix='profile_')
        profname = 'auto_profile_sphinx_build'
        pdir = os.path.join(tmp_profile_dir, profname)
        profile = ProfileDir.create_profile_dir(pdir)

        # Create and initialize global ipython, but don't start its mainloop.
        # This will persist across different EmbededSphinxShell instances.
        IP = InteractiveShell.instance(config=config, profile_dir=profile)
        atexit.register(self.cleanup)

        # io.stdout redirect must be done after instantiating InteractiveShell
        io.stdout = self.cout
        io.stderr = self.cout

        # For debugging, so we can see normal output, use this:
        #from IPython.utils.io import Tee
        #io.stdout = Tee(self.cout, channel='stdout') # dbg
        #io.stderr = Tee(self.cout, channel='stderr') # dbg

        # Store a few parts of IPython we'll need.
        self.IP = IP
        self.user_ns = self.IP.user_ns
        self.user_global_ns = self.IP.user_global_ns

        self.input = ''
        self.output = ''
        self.tmp_profile_dir = tmp_profile_dir

        self.is_verbatim = False
        self.is_doctest = False
        self.is_suppress = False

        # Optionally, provide more detailed information to shell.
        # this is assigned by the SetUp method of IPythonDirective
        # to point at itself.
        #
        # So, you can access handy things at self.directive.state
        self.directive = None

        # on the first call to the savefig decorator, we'll import
        # pyplot as plt so we can make a call to the plt.gcf().savefig
        self._pyplot_imported = False

        # Prepopulate the namespace.
        for line in exec_lines:
            self.process_input_line(line, store_history=False)
Example #36
0
    def init_profile_dir(self):
        """initialize the profile dir"""
        self._in_init_profile_dir = True
        if self.profile_dir is not None:
            # already ran
            return
        if 'ProfileDir.location' not in self.config:
            # location not specified, find by profile name
            try:
                p = ProfileDir.find_profile_dir_by_name(
                    self.ipython_dir, self.profile, self.config)
            except ProfileDirError:
                # not found, maybe create it (always create default profile)
                if self.auto_create or self.profile == 'default':
                    try:
                        p = ProfileDir.create_profile_dir_by_name(
                            self.ipython_dir, self.profile, self.config)
                    except ProfileDirError:
                        self.log.fatal("Could not create profile: %r" %
                                       self.profile)
                        self.exit(1)
                    else:
                        self.log.info("Created profile dir: %r" % p.location)
                else:
                    self.log.fatal("Profile %r not found." % self.profile)
                    self.exit(1)
            else:
                self.log.debug("Using existing profile dir: %r" % p.location)
        else:
            location = self.config.ProfileDir.location
            # location is fully specified
            try:
                p = ProfileDir.find_profile_dir(location, self.config)
            except ProfileDirError:
                # not found, maybe create it
                if self.auto_create:
                    try:
                        p = ProfileDir.create_profile_dir(
                            location, self.config)
                    except ProfileDirError:
                        self.log.fatal(
                            "Could not create profile directory: %r" %
                            location)
                        self.exit(1)
                    else:
                        self.log.debug("Creating new profile dir: %r" %
                                       location)
                else:
                    self.log.fatal("Profile directory %r not found." %
                                   location)
                    self.exit(1)
            else:
                self.log.info("Using existing profile dir: %r" % location)
            # if profile_dir is specified explicitly, set profile name
            dir_name = os.path.basename(p.location)
            if dir_name.startswith('profile_'):
                self.profile = dir_name[8:]

        self.profile_dir = p
        self.config_file_paths.append(p.location)
        self._in_init_profile_dir = False
Example #37
0
 def get_profile_dir(self, name, path):
     from IPython.core.profiledir import ProfileDir
     p = ProfileDir.find_profile_dir_by_name(path,name=name)
     return p.location
Example #38
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]