1# -*- coding: utf-8 -*- 2# Copyright 2014 Christoph Reiter 3# 4# This program is free software; you can redistribute it and/or modify 5# it under the terms of the GNU General Public License as published by 6# the Free Software Foundation; either version 2 of the License, or 7# (at your option) any later version. 8 9import os 10import re 11 12from tests import TestCase 13 14import mutagen 15 16 17class TSourceEncoding(TestCase): 18 """Enforce utf-8 source encoding everywhere. 19 Plus give helpful message for fixing it. 20 """ 21 22 def _check_encoding(self, path): 23 with open(path, "r") as h: 24 match = None 25 for i, line in enumerate(h): 26 # https://www.python.org/dev/peps/pep-0263/ 27 match = match or re.search("coding[:=]\\s*([-\\w.]+)", line) 28 if i >= 2: 29 break 30 if match: 31 match = match.group(1) 32 self.assertEqual(match, "utf-8", 33 msg="%s has no utf-8 source encoding set\n" 34 "Insert:\n# -*- coding: utf-8 -*-" % path) 35 36 def test_main(self): 37 root = os.path.dirname(mutagen.__path__[0]) 38 39 skip = [os.path.join(root, "docs"), os.path.join(root, "venv")] 40 for dirpath, dirnames, filenames in os.walk(root): 41 if any((dirpath.startswith(s + os.sep) or s == dirpath) 42 for s in skip): 43 continue 44 45 for filename in filenames: 46 if filename.endswith('.py'): 47 path = os.path.join(dirpath, filename) 48 self._check_encoding(path) 49