From 36550a0f23ca1509d48ea3972342de1637598b3a Mon Sep 17 00:00:00 2001 From: Rovanion Luckey Date: Fri, 28 Mar 2014 12:02:24 +0100 Subject: [PATCH 1/5] 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 3119e1bfa3..8239396736 100644 --- a/beets/library.py +++ b/beets/library.py @@ -480,7 +480,7 @@ def try_write(self, path=None): # 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 @@ def move_file(self, dest, copy=False): 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 428de312a0..afa54411cf 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 From 68b6317c598404dc94a98684793a496a27e55461 Mon Sep 17 00:00:00 2001 From: Rovanion Luckey Date: Fri, 18 Apr 2014 23:24:13 +0200 Subject: [PATCH 2/5] Now reads config['import']['link'], style fix and default setting added. --- beets/config_default.yaml | 1 + beets/util/__init__.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/beets/config_default.yaml b/beets/config_default.yaml index 82dea99d5c..1854b103dd 100644 --- a/beets/config_default.yaml +++ b/beets/config_default.yaml @@ -5,6 +5,7 @@ import: write: yes copy: yes move: no + link: no delete: no resume: ask incremental: no diff --git a/beets/util/__init__.py b/beets/util/__init__.py index afa54411cf..f7894501eb 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -449,7 +449,9 @@ def move(path, dest, replace=False): 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`.""" + """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 From 3b898163eeb3cfd1f5c728ecff4d1e026082674a Mon Sep 17 00:00:00 2001 From: Rovanion Luckey Date: Fri, 18 Apr 2014 23:35:57 +0200 Subject: [PATCH 3/5] Style fixes. --- beets/library.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beets/library.py b/beets/library.py index 8239396736..1c7f564219 100644 --- a/beets/library.py +++ b/beets/library.py @@ -769,7 +769,7 @@ def remove(self, delete=False, with_items=True): for item in self.items(): item.remove(delete, False) - def move_art(self, copy=False): + def move_art(self, copy=False, link=False): """Move or copy any existing album art so that it remains in the same directory as the items. """ @@ -785,6 +785,8 @@ def move_art(self, copy=False): log.debug('moving album art %s to %s' % (old_art, new_art)) if copy: util.copy(old_art, new_art) + elif link: + util.link(old_art, new_art) else: util.move(old_art, new_art) self.artpath = new_art @@ -895,7 +897,7 @@ def store(self): item.store() -# Query construction helper. +# Query construction and parsing helpers. def get_query_sort(val, model_cls): """Take a value which may be None, a query string, a query string From b0a843cfbe31ff5e29d0c7719056692af8735dee Mon Sep 17 00:00:00 2001 From: Rovanion Luckey Date: Sat, 26 Apr 2014 13:13:31 +0200 Subject: [PATCH 4/5] Failing to link a file now throws a FilesystemError. --- beets/util/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index f7894501eb..072afa9ce1 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -460,11 +460,12 @@ def link(path, dest, replace=False): 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!" + raise FilesystemError('Operating system does not support symbolic ' + 'links.', 'link', (path, dest), + traceback.format_exc()) def unique_path(path): From a08cb4efb8de15e758a941f11d62f5be9df5a35a Mon Sep 17 00:00:00 2001 From: Rovanion Luckey Date: Wed, 10 Sep 2014 12:33:12 +0200 Subject: [PATCH 5/5] Should have corrected some of the style checking errors. --- beets/util/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 072afa9ce1..529bbb2f33 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -448,9 +448,10 @@ 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 + """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 @@ -464,7 +465,7 @@ def link(path, dest, replace=False): os.symlink(path, dest) except OSError: raise FilesystemError('Operating system does not support symbolic ' - 'links.', 'link', (path, dest), + 'links.', 'link', (path, dest), traceback.format_exc())