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)
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
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])
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']))
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
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])
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
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)
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
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)
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])
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()
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)
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)
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
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)
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
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 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)
def main(args): parser = argparse.ArgumentParser() parser.add_argument('--port', type=int) parser.add_argument('--kernel') parser.add_argument('--conn-file') args = parser.parse_args() if args.conn_file: if runtime_dir: conn_file = (args.conn_file if os.path.isabs(args.conn_file) else os.path.join(runtime_dir(), args.conn_file)) else: # IPython 3 pd = ProfileDir.find_profile_dir_by_name(get_ipython_dir(), 'default') conn_file = os.path.join(pd.security_dir, args.conn_file) kwargs = {'connection_file': conn_file} if args.kernel: kwargs['kernel_name'] = args.kernel manager = KernelManager(**kwargs) semaphore = multiprocessing.Semaphore() semaphore.acquire() def onsignal(*args): semaphore.release() signal.signal(signal.SIGTERM, onsignal) import platform if platform.system() == 'Windows': signal.signal(signal.SIGBREAK, onsignal) else: signal.signal(signal.SIGQUIT, onsignal) # Emacs sends SIGHUP upon exit signal.signal(signal.SIGHUP, onsignal) manager.start_kernel() try: semaphore.acquire() except KeyboardInterrupt: pass manager.shutdown_kernel() else: app = make_app() app.listen(args.port) tornado.ioloop.IOLoop.current().start()
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 """)
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
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
def get_profile_dir(self, name, path): p = ProfileDir.find_profile_dir_by_name(path,name=name) return p.location
def get_profile_dir(self, name, path): p = ProfileDir.find_profile_dir_by_name(path,name=name) return p.location
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]
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"])
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]
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