From ab35db7b7a8813e8b9a44b3cb1c36957f963dca3 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 18 Jan 2011 20:13:57 -0800 Subject: [PATCH] truncate path components to 30 characters on Windows (work around #120) --- NEWS | 1 + beets/library.py | 6 ++++-- test/test_db.py | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 4c22bfb28..58e81fb27 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,7 @@ completely wrong association of track names to files. The order applied was always just alphabetical by filename, which is frequently but not always what you want. +* Filenames are now truncated to 30 characters on Windows. * Fix crash in lastid when the artist name is not available. * Fixed a spurious crash when LANG or a related environment variable is set to an invalid value (such as 'UTF-8' on some installations of Mac diff --git a/beets/library.py b/beets/library.py index 87af2bcd6..1c3e845c9 100644 --- a/beets/library.py +++ b/beets/library.py @@ -24,6 +24,7 @@ from beets.mediafile import MediaFile, UnreadableFileError, FileTypeError from beets import plugins MAX_FILENAME_LENGTH = 200 +MAX_WINDOWS_FILENAME_LENGTH = 30 # Fields in the "items" database table; all the metadata available for # items in the library. These are used directly in SQL; they are @@ -203,8 +204,9 @@ def _sanitize_path(path, pathmod=None): comp = regex.sub(repl, comp) # Truncate each component. - if len(comp) > MAX_FILENAME_LENGTH: - comp = comp[:MAX_FILENAME_LENGTH] + maxlen = MAX_WINDOWS_FILENAME_LENGTH if windows else MAX_FILENAME_LENGTH + if len(comp) > maxlen: + comp = comp[:maxlen] comps[i] = comp return pathmod.join(*comps) diff --git a/test/test_db.py b/test/test_db.py index 76d503046..6abc78f9b 100644 --- a/test/test_db.py +++ b/test/test_db.py @@ -259,6 +259,14 @@ class DestinationTest(unittest.TestCase): p = beets.library._sanitize_path(u':', posixpath) self.assertEqual(p, u'-') + def test_sanitize_windows_uses_very_short_names(self): + p = beets.library._sanitize_path('X'*300 + '/' + 'Y'*200, ntpath) + self.assertLessEqual(len(p), 100) + + def test_sanitize_unix_uses_longer_names(self): + p = beets.library._sanitize_path('X'*300 + '/' + 'Y'*200, posixpath) + self.assertGreaterEqual(len(p), 100) + def test_path_with_format(self): self.lib.path_format = '$artist/$album ($format)' p = self.lib.destination(self.i)