]> git.ipfire.org Git - thirdparty/glibc.git/blob - scripts/vcs_to_changelog/misc_util.py
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / scripts / vcs_to_changelog / misc_util.py
1 # General Utility functions.
2 # Copyright (C) 2019-2020 Free Software Foundation, Inc.
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 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 import sys
18
19 class DebugUtil:
20 debug = False
21 def __init__(self, debug):
22 self.debug = debug
23
24 def eprint(self, *args, **kwargs):
25 ''' Print to stderr.
26 '''
27 print(*args, file=sys.stderr, **kwargs)
28
29
30 def print(self, *args, **kwargs):
31 ''' Convenience function to print diagnostic information in the program.
32 '''
33 if self.debug:
34 self.eprint(*args, **kwargs)
35
36
37 def decode(string):
38 ''' Attempt to decode a string.
39
40 Decode a string read from the source file. The multiple attempts are needed
41 due to the presence of the page break characters and some tests in locales.
42 '''
43 codecs = ['utf8', 'cp1252']
44
45 for i in codecs:
46 try:
47 return string.decode(i)
48 except UnicodeDecodeError:
49 pass
50
51 DebugUtil.eprint('Failed to decode: %s' % string)