From 75fe23b1c851c0988a5d4ae4ff61a760ce2b70c2 2024-08-15 11:52:03 From: M Bussonnier Date: 2024-08-15 11:52:03 Subject: [PATCH] Only copy files in startup dir if we just created it. Closes #14496 --- diff --git a/IPython/core/profiledir.py b/IPython/core/profiledir.py index 1e33b55..95bc59f 100644 --- a/IPython/core/profiledir.py +++ b/IPython/core/profiledir.py @@ -14,6 +14,8 @@ from ..paths import get_ipython_package_dir from ..utils.path import expand_path, ensure_dir_exists from traitlets import Unicode, Bool, observe +from typing import Optional + #----------------------------------------------------------------------------- # Module errors #----------------------------------------------------------------------------- @@ -68,18 +70,31 @@ class ProfileDir(LoggingConfigurable): self.pid_dir = os.path.join(new, self.pid_dir_name) self.static_dir = os.path.join(new, self.static_dir_name) self.check_dirs() - - def _mkdir(self, path, mode=None): + + def _mkdir(self, path: str, mode: Optional[int] = None) -> bool: """ensure a directory exists at a given path This is a version of os.mkdir, with the following differences: - - returns True if it created the directory, False otherwise + - returns wether the directory has been created or not. - ignores EEXIST, protecting against race conditions where the dir may have been created in between the check and the creation - sets permissions if requested and the dir already exists + + Parameters + ---------- + path: str + path of the dir to create + mode: int + see `mode` of `os.mkdir` + + Returns + ------- + bool: + returns True if it created the directory, False otherwise """ + if os.path.exists(path): if mode and os.stat(path).st_mode != mode: try: @@ -109,16 +124,20 @@ class ProfileDir(LoggingConfigurable): @observe('startup_dir') def check_startup_dir(self, change=None): - self._mkdir(self.startup_dir) - - readme = os.path.join(self.startup_dir, 'README') - src = os.path.join(get_ipython_package_dir(), u'core', u'profile', u'README_STARTUP') - - if not os.path.exists(src): - self.log.warning("Could not copy README_STARTUP to startup dir. Source file %s does not exist.", src) - - if os.path.exists(src) and not os.path.exists(readme): - shutil.copy(src, readme) + if self._mkdir(self.startup_dir): + readme = os.path.join(self.startup_dir, "README") + src = os.path.join( + get_ipython_package_dir(), "core", "profile", "README_STARTUP" + ) + + if not os.path.exists(src): + self.log.warning( + "Could not copy README_STARTUP to startup dir. Source file %s does not exist.", + src, + ) + + if os.path.exists(src) and not os.path.exists(readme): + shutil.copy(src, readme) @observe('security_dir') def check_security_dir(self, change=None):