1# vim:set et ts=4 sw=4: 

2 

3"""Text utility functions 

4 

5@contact: Debian FTP Master <ftpmaster@debian.org> 

6@copyright: 2000, 2001, 2002, 2003, 2004, 2005, 2006 James Troup <james@nocrew.org> 

7@license: GNU General Public License version 2 or later 

8""" 

9 

10# This program is free software; you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation; either version 2 of the License, or 

13# (at your option) any later version. 

14 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19 

20# You should have received a copy of the GNU General Public License 

21# along with this program; if not, write to the Free Software 

22# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

23 

24import email.header 

25 

26from .dak_exceptions import * 

27from .regexes import re_parse_maintainer 

28 

29################################################################################ 

30 

31 

32def rfc2047_encode(s): 

33 """ 

34 Encodes a (header) string per RFC2047 if necessary. If the 

35 string is neither ASCII nor UTF-8, it's assumed to be ISO-8859-1. 

36 """ 

37 for enc in ('ascii', 'utf-8', 'iso-8859-1'): 37 ↛ 44line 37 didn't jump to line 44, because the loop on line 37 didn't complete

38 try: 

39 return email.header.Header(s, enc, 998).encode() 

40 except UnicodeEncodeError: 

41 pass 

42 

43 # If we get here, we're boned beyond belief 

44 raise RuntimeError("Failed to encode string") 

45 

46################################################################################ 

47 

48# <Culus> 'The standard sucks, but my tool is supposed to interoperate 

49# with it. I know - I'll fix the suckage and make things 

50# incompatible!' 

51 

52 

53def fix_maintainer(maintainer: str) -> tuple[str, str, str, str]: 

54 """ 

55 Parses a Maintainer or Changed-By field and returns: 

56 1. an RFC822 compatible version, 

57 2. an RFC2047 compatible version, 

58 3. the name 

59 4. the email 

60 

61 The name is forced to UTF-8 for both 1. and 3.. If the name field 

62 contains '.' or ',' (as allowed by Debian policy), 1. and 2. are 

63 switched to 'email (name)' format. 

64 

65 """ 

66 maintainer = maintainer.strip() 

67 if not maintainer: 

68 return ('', '', '', '') 

69 

70 if maintainer.find("<") == -1: 

71 email = maintainer 

72 name = "" 

73 elif (maintainer[0] == "<" and maintainer[-1:] == ">"): 

74 email = maintainer[1:-1] 

75 name = "" 

76 else: 

77 m = re_parse_maintainer.match(maintainer) 

78 if not m: 

79 raise ParseMaintError("Doesn't parse as a valid Maintainer field.") 

80 name = m.group(1) 

81 email = m.group(2) 

82 

83 # Get an RFC2047 compliant version of the name 

84 rfc2047_name = rfc2047_encode(name) 

85 

86 if name.find(',') != -1 or name.find('.') != -1: 

87 rfc822_maint = "%s (%s)" % (email, name) 

88 rfc2047_maint = "%s (%s)" % (email, rfc2047_name) 

89 else: 

90 rfc822_maint = "%s <%s>" % (name, email) 

91 rfc2047_maint = "%s <%s>" % (rfc2047_name, email) 

92 

93 if email.find("@") == -1 and email.find("buildd_") != 0: 

94 raise ParseMaintError("No @ found in email address part.") 

95 

96 return (rfc822_maint, rfc2047_maint, name, email) 

97 

98################################################################################ 

99 

100 

101def split_uploaders(field): 

102 import re 

103 for u in re.sub(">[ ]*,", ">\t", field).split("\t"): 

104 u = u.strip() 

105 # Trailing commas will give an empty final uploader 

106 if u: 106 ↛ 103line 106 didn't jump to line 103, because the condition on line 106 was never false

107 yield u