Debian Codegraph @

extras/check_utf8.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2009 Alexander Belchenko <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 """check_utf8 command for setup.py.
21 It checks contents of source files to be UTF-8 encoded.
22 """
23
24 from distutils.core import Command
25 from distutils.errors import DistutilsPlatformError
26 from distutils import log
27 import os
28 import sys
29
30
31 class check_utf8(Command):
32
33 description = ("check contents of source files to be UTF-8 encoded")
34 user_options = []
35
36 def initialize_options(self):
37 pass
38
39 def finalize_options(self):
40 pass
41
42 def run(self):
43 for root, dirs, files in os.walk('.'):
44 for fname in sorted(files):
45 ext = os.path.splitext(fname)[1].lower()
46 if ext in ('.py', '.txt', '.ui'):
47 fullname = os.path.join(root, fname)[2:] # first 2 characters is ./
48 # verbose: 0=quiet, 1=normal, 2=verbose
49 if self.verbose:
50 log.info('checking ' + fullname)
51 if not self.dry_run:
52 f = open(fullname, 'rb')
53 content = f.read()
54 f.close()
55 try:
56 content.decode('utf-8')
57 except UnicodeDecodeError as e:
58 log.error(fullname + ': ' + str(e))
59 # skip some directories
60 for dname in dirs[:]:
61 fullname = os.path.join(root, dname).replace('\\','/')
62 if fullname in ('./_lib', './build', './dist', './installer'):
63 dirs.remove(dname)