66import errno
77import os
88import shutil
9- import stat
109import sys
1110from collections .abc import Callable , Iterator
1211from contextlib import contextmanager
@@ -87,9 +86,10 @@ def onexc(function: object, path: str, excinfo: object) -> None: # noqa:ARG001
8786 nonlocal has_errors
8887 has_errors = True
8988 try :
90- # chmod u+w
89+ # chmod u+rwx
90+ # Note that directories need the 'x' bit!
9191 mode = os .stat (path ).st_mode
92- os .chmod (path , mode | stat . S_IWUSR )
92+ os .chmod (path , mode | 0o700 )
9393 except OSError : # pragma: nocover
9494 pass
9595
@@ -130,7 +130,13 @@ def onexc(function: object, path: str, excinfo: object) -> None: # noqa:ARG001
130130
131131
132132def chdir (path : str | Path ) -> None :
133- """Move the present-working directory (pwd) into the target directory."""
133+ """Move the present-working directory (pwd) into the target directory.
134+
135+ .. note::
136+
137+ This function changes the pwd for the entire process, and as such it
138+ is thread-unsafe and not context-aware.
139+ """
134140 path = Path (path )
135141 log .info ("chdir %s" , path )
136142 os .chdir (resolve_env (path ))
@@ -151,6 +157,13 @@ def pushd(path: str | Path) -> Iterator[None]:
151157 pushd mydir
152158 ...
153159 popd
160+
161+
162+ .. note::
163+
164+ This function changes the pwd for the entire process, not just the
165+ code inside the with block, and as such it is thread-unsafe and not
166+ context-aware.
154167 """
155168 path = Path (path )
156169 cwd = os .getcwd ()
@@ -332,18 +345,13 @@ def symlink(
332345
333346 log .info ("Creating symlink %s => %s" , src , dst )
334347
335- if abspath :
336- os .symlink (real_src , real_dst )
337- else :
338- cwd_backup = os .getcwd ()
339- os .chdir (os .path .realpath (os .path .dirname (real_dst )))
340- try :
341- # Generate shortest possible relative path
342- real_src = os .path .relpath (os .path .realpath (real_src ))
343- real_dst = os .path .relpath (os .path .realpath (real_dst ))
344- os .symlink (real_src , real_dst )
345- finally :
346- os .chdir (cwd_backup )
348+ if not abspath :
349+ # Generate shortest possible relative path
350+ real_src = os .path .relpath (
351+ os .path .realpath (real_src ),
352+ start = os .path .realpath (os .path .dirname (real_src )),
353+ )
354+ os .symlink (real_src , real_dst )
347355
348356
349357def exists (path : str | Path ) -> bool :
0 commit comments