Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[disorder] / tests / recode.py
1 #! /usr/bin/env python
2 #
3 # This file is part of DisOrder.
4 # Copyright (C) 2008 Richard Kettlewell
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 import dtest,disorder,sys,os,unicodedata
20
21 def recode(name, f, t):
22 recoded = 0
23 # First recode basename
24 d = os.path.dirname(name)
25 b = os.path.basename(name)
26 nb = unicodedata.normalize("NFC", unicode(b, f)).encode(t)
27 if b != nb:
28 print " %s -> %s" % (repr(b), repr(nb))
29 name = os.path.join(d, nb)
30 os.rename(os.path.join(d, b), name)
31 recoded += 1
32 # Recurse into directories
33 if os.path.isdir(name):
34 for c in os.listdir(name):
35 recoded += recode(os.path.join(name, c), f, t)
36 return recoded
37
38 def test():
39 """Test encoding conversion"""
40 if sys.platform == "darwin":
41 print "Sorry, cannot run this test on Darwin"
42 # ...because local fs is always UTF-8
43 sys.exit(77)
44 dtest.start_daemon()
45 dtest.create_user()
46 dtest.rescan()
47 dtest.check_files()
48 dtest.stop_daemon()
49 print " recoding as ISO-8859-1"
50 recoded = recode(dtest.tracks, "UTF-8", "ISO-8859-1")
51 print " ...recoded %d filenames" % recoded
52 print " regenerating config"
53 dtest.default_config(encoding="ISO-8859-1")
54 print " checking recoded files"
55 dtest.start_daemon()
56 dtest.rescan()
57 dtest.check_files()
58 dtest.stop_daemon()
59 print " recoding as UTF-8"
60 recoded = recode(dtest.tracks, "ISO-8859-1", "UTF-8")
61 print " ...recoded %d filenames" % recoded
62 print " regenerating config"
63 dtest.default_config(encoding="UTF-8")
64 print " checking recoded files"
65 dtest.start_daemon()
66 dtest.rescan()
67 dtest.check_files()
68
69
70 if __name__ == '__main__':
71 dtest.run()