Antoine Pitrou avatar Antoine Pitrou committed 236dcf1

Add a *target_is_directory* argument to Path.symlink_to()

Comments (0)

Files changed (4)

+Version 0.7
+- Add a *target_is_directory* argument to Path.symlink_to()
 Version 0.6
    Remove this directory.  The directory must be empty.
-.. method:: Path.symlink_to(target)
+.. method:: Path.symlink_to(target, target_is_directory=False)
-   Make this path a symbolic link to *target*.
+   Make this path a symbolic link to *target*.  Under Windows,
+   *target_is_directory* must be True (default False) if the link's target
+   is a directory.  Under POSIX, *target_is_directory*'s value is ignored.
       >>> p = Path('mylink')
       >>> p.symlink_to('')
         rename = _wrap_binary_atfunc(os.renameat)
-        def symlink(self, target, pathobj):
+        def symlink(self, target, pathobj, target_is_directory):
             parent_fd, name = _fdnamepair(pathobj)
             os.symlinkat(str(target), parent_fd, name)
     rename = _wrap_binary_strfunc(os.rename)
-    symlink = _wrap_binary_strfunc(os.symlink)
+    if nt:
+        symlink = _wrap_binary_strfunc(os.symlink)
+    else:
+        # Under POSIX, os.symlink() takes two args
+        @staticmethod
+        def symlink(a, b, target_is_directory):
+            return os.symlink(str(a), str(b))
     def init_path(self, pathobj):
         self._accessor.rename(self, target)
-    def symlink_to(self, target):
+    def symlink_to(self, target, target_is_directory=False):
         Make this path a symlink pointing to the given path.
         Note the order of arguments (self, target) is the reverse of os.symlink's.
         if self._closed:
-        # XXX how about target_is_directory?
-        self._accessor.symlink(target, self)
+        self._accessor.symlink(target, self, target_is_directory)
     # Convenience functions for querying the stat results
         self.assertEqual(link.stat(), target.stat())
         self.assertNotEqual(link.lstat(), target.stat())
+        self.assertFalse(link.is_dir())
+        # Symlinking to a directory
+        target = P['dirB']
+        link = P['dirA', 'linkAAAA']
+        link.symlink_to(target, target_is_directory=True)
+        self.assertEqual(link.stat(), target.stat())
+        self.assertNotEqual(link.lstat(), target.stat())
+        self.assertTrue(link.is_dir())
+        self.assertTrue(list(link))
     def test_is_dir(self):
         P = self.cls(BASE)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.