From 36550a0f23ca1509d48ea3972342de1637598b3a Mon Sep 17 00:00:00 2001 From: Rovanion Luckey Date: Fri, 28 Mar 2014 12:02:24 +0100 Subject: [PATCH] First commit towards a linking move_file(). --- beets/library.py | 6 +++++- beets/util/__init__.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/beets/library.py b/beets/library.py index 3119e1bfa..823939673 100644 --- a/beets/library.py +++ b/beets/library.py @@ -480,7 +480,7 @@ class Item(LibModel): # Files themselves. - def move_file(self, dest, copy=False): + def move_file(self, dest, copy=False, link=False): """Moves or copies the item's file, updating the path value if the move succeeds. If a file exists at ``dest``, then it is slightly modified to be unique. @@ -491,6 +491,10 @@ class Item(LibModel): util.copy(self.path, dest) plugins.send("item_copied", item=self, source=self.path, destination=dest) + elif link: + util.link(self.path, dest) + plugins.send("item_linked", item=self, source=self.path, + destination=dest) else: plugins.send("before_item_moved", item=self, source=self.path, destination=dest) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 428de312a..afa54411c 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -448,6 +448,22 @@ def move(path, dest, replace=False): raise FilesystemError(exc, 'move', (path, dest), traceback.format_exc()) +def link(path, dest, replace=False): + """Create a symbolic link from path to `dest`. Raises an OSError if `dest` already exists, unless `replace` is True. Does nothing if `path` == `dest`.""" + if (samefile(path, dest)): + return + + path = syspath(path) + dest = syspath(dest) + if os.path.exists(dest) and not replace: + raise FilesystemError('file exists', 'rename', (path, dest), + traceback.format_exc()) + + try: + os.symlink(path, dest) + except OSError: + print "WAAT!" + def unique_path(path): """Returns a version of ``path`` that does not exist on the