chiark / gitweb /
Synchronize with disorder.dev
[disorder] / tests / recode.py
1 #! /usr/bin/env python -u
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 2 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, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # 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, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 # USA
20 #
21 import dtest,disorder,sys,os,unicodedata
22
23 def recode(name, f, t):
24   recoded = 0
25   # First recode basename
26   d = os.path.dirname(name)
27   b = os.path.basename(name)
28   nb = unicodedata.normalize("NFC", unicode(b, f)).encode(t)
29   if b != nb:
30     print "  %s -> %s" % (repr(b), repr(nb))
31     name = os.path.join(d, nb)
32     os.rename(os.path.join(d, b), name)
33     recoded += 1
34   # Recurse into directories
35   if os.path.isdir(name):
36     for c in os.listdir(name):
37       recoded += recode(os.path.join(name, c), f, t)
38   return recoded
39
40 def test():
41     """Test encoding conversion"""
42     if sys.platform == "darwin":
43       print "Sorry, cannot run this test on Darwin"
44       # ...because local fs is always UTF-8
45       sys.exit(77)
46     dtest.start_daemon()
47     dtest.create_user()
48     dtest.rescan()
49     dtest.check_files()
50     dtest.stop_daemon()
51     print " recoding as ISO-8859-1"
52     recoded = recode(dtest.tracks, "UTF-8", "ISO-8859-1")
53     print " ...recoded %d filenames" % recoded
54     print " regenerating config"
55     dtest.default_config(encoding="ISO-8859-1")
56     print " checking recoded files"
57     dtest.start_daemon()
58     dtest.rescan()
59     dtest.check_files()
60     dtest.stop_daemon()
61     print " recoding as UTF-8"
62     recoded = recode(dtest.tracks, "ISO-8859-1", "UTF-8")
63     print " ...recoded %d filenames" % recoded
64     print " regenerating config"
65     dtest.default_config(encoding="UTF-8")
66     print " checking recoded files"
67     dtest.start_daemon()
68     dtest.rescan()
69     dtest.check_files()
70     
71
72 if __name__ == '__main__':
73     dtest.run()