chiark / gitweb /
Switch to GPL v3
[disorder] / tests / recode.py
CommitLineData
abf54aca 1#! /usr/bin/env python
031f8feb
RK
2#
3# This file is part of DisOrder.
4# Copyright (C) 2008 Richard Kettlewell
5#
e7eb3a27 6# This program is free software: you can redistribute it and/or modify
031f8feb 7# it under the terms of the GNU General Public License as published by
e7eb3a27 8# the Free Software Foundation, either version 3 of the License, or
031f8feb
RK
9# (at your option) any later version.
10#
e7eb3a27
RK
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#
031f8feb 16# You should have received a copy of the GNU General Public License
e7eb3a27 17# along with this program. If not, see <http://www.gnu.org/licenses/>.
031f8feb
RK
18#
19import dtest,disorder,sys,os,unicodedata
20
21def 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
38def 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
3ece470d 43 sys.exit(77)
031f8feb
RK
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
70if __name__ == '__main__':
71 dtest.run()