mailmap.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # mailmap.py -- Mailmap reader
  2. # Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Mailmap file reader."""
  21. def parse_identity(text):
  22. # TODO(jelmer): Integrate this with dulwich.fastexport.split_email and
  23. # dulwich.repo.check_user_identity
  24. (name, email) = text.rsplit(b"<", 1)
  25. name = name.strip()
  26. email = email.rstrip(b">").strip()
  27. if not name:
  28. name = None
  29. if not email:
  30. email = None
  31. return (name, email)
  32. def read_mailmap(f):
  33. """Read a mailmap.
  34. Args:
  35. f: File-like object to read from
  36. Returns: Iterator over
  37. ((canonical_name, canonical_email), (from_name, from_email)) tuples
  38. """
  39. for line in f:
  40. # Remove comments
  41. line = line.split(b"#")[0]
  42. line = line.strip()
  43. if not line:
  44. continue
  45. (canonical_identity, from_identity) = line.split(b">", 1)
  46. canonical_identity += b">"
  47. if from_identity.strip():
  48. parsed_from_identity = parse_identity(from_identity)
  49. else:
  50. parsed_from_identity = None
  51. parsed_canonical_identity = parse_identity(canonical_identity)
  52. yield parsed_canonical_identity, parsed_from_identity
  53. class Mailmap:
  54. """Class for accessing a mailmap file."""
  55. def __init__(self, map=None):
  56. self._table = {}
  57. if map:
  58. for (canonical_identity, from_identity) in map:
  59. self.add_entry(canonical_identity, from_identity)
  60. def add_entry(self, canonical_identity, from_identity=None):
  61. """Add an entry to the mail mail.
  62. Any of the fields can be None, but at least one of them needs to be
  63. set.
  64. Args:
  65. canonical_identity: The canonical identity (tuple)
  66. from_identity: The from identity (tuple)
  67. """
  68. if from_identity is None:
  69. from_name, from_email = None, None
  70. else:
  71. (from_name, from_email) = from_identity
  72. (canonical_name, canonical_email) = canonical_identity
  73. if from_name is None and from_email is None:
  74. self._table[canonical_name, None] = canonical_identity
  75. self._table[None, canonical_email] = canonical_identity
  76. else:
  77. self._table[from_name, from_email] = canonical_identity
  78. def lookup(self, identity):
  79. """Lookup an identity in this mailmail."""
  80. if not isinstance(identity, tuple):
  81. was_tuple = False
  82. identity = parse_identity(identity)
  83. else:
  84. was_tuple = True
  85. for query in [identity, (None, identity[1]), (identity[0], None)]:
  86. canonical_identity = self._table.get(query)
  87. if canonical_identity is not None:
  88. identity = (
  89. canonical_identity[0] or identity[0],
  90. canonical_identity[1] or identity[1],
  91. )
  92. break
  93. if was_tuple:
  94. return identity
  95. else:
  96. return identity[0] + b" <" + identity[1] + b">"
  97. @classmethod
  98. def from_path(cls, path):
  99. with open(path, "rb") as f:
  100. return cls(read_mailmap(f))