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"))
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
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 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)
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 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 == '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 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) self._in_init_profile_dir = False
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 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 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'])
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'])
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)
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)
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(py3compat.doctest_refactor_print('print zzz\n')) # validate output tt.ipexec_validate( fname, '123', '', options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test'])
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'])
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 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 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 temporary_ipython_dir(prefix=None): # FIXME: cleanup has issues on Windows # this is *probably* a real bug of holding open files, # but it is preventing feedback about test failures td_obj = TemporaryDirectory(suffix=".ipython", prefix=prefix) td = td_obj.name with mock.patch.dict(os.environ, {"IPYTHONDIR": td}): assert IPython.paths.get_ipython_dir() == td pd = ProfileDir.create_profile_dir_by_name(td, name="default") # configure fast heartbeats for quicker tests with small numbers of local engines with open(os.path.join(pd.location, "ipcontroller_config.py"), "w") as f: f.write("c.HeartMonitor.period = 200") try: yield td finally: try: td_obj.cleanup() except Exception as e: print(f"Failed to cleanup {td}: {e}", file=sys.stderr)
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 _default_profile_dir(profile=None): """Locate the default IPython profile directory Priorities: - named profile, if specified - current IPython profile, if run inside IPython - $IPYTHONDIR/profile_default Returns absolute profile directory path, ensuring it exists """ if not profile: ip = get_ipython() if ip is not None: return ip.profile_dir.location ipython_dir = get_ipython_dir() profile = profile or 'default' try: pd = ProfileDir.find_profile_dir_by_name(ipython_dir, name=profile) except ProfileDirError: pd = ProfileDir.create_profile_dir_by_name(ipython_dir, name=profile) return pd.location
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")
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')
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"