import re hg_url_base = r'https://github.com/Intevation/' substitutions = [ (re.compile(r'debian:\#(?P\d+)'), r'debian#\g' ), (re.compile(r'\#(?P\s*)(?P\d+)'), r"#\g\g" ), (re.compile(r'(?P^|\s+)issue(?P\s*)(?P\d+)'), r"\gissue\g\g" ), # matching revison number or hash (re.compile(r'(?P(^|\s+))(?P(revision|rev|r)\s?)(?P([1-9][0-9]*)|[0-9a-fA-F]{4,40})(?P\W+|$)'), r'\g\g\g\g'), (re.compile(r""" (?P(^|\s+)) # start w beginning of line or at least 1 whitespace (?P(intelmq)?[:/]?) # optional keyword 'mailgen' and opt sep char (?P([0-9a-fA-F]{7})) # mandatory 7 chars of hex code (?P([0-9a-fA-F]{0,33})) # optional additional hex code 0-33 chars (?P\W+|$) # followed by >=1 non-alpha-num char or end of line """, re.X), r'\g\g\g\g'), (re.compile(r""" (?P(^|\s+)) # start w beginning of line or at least 1 whitespace (?P(mailgen)[:/]?) # keyword 'mailgen' followed by optional sep char (?P([0-9a-fA-F]{7})) # mandatory 7 chars of hex code (?P([0-9a-fA-F]{0,33})) # optional additional hex code 0-33 chars (?P\W+|$) # followed by >=1 non-alpha-num char or end of line """, re.X), r'\g\g\g\g'), (re.compile(r""" (?P(^|\s+)) # start w beginning of line or at least 1 whitespace (?P(manager)[:/]?) # keyword 'manager' followed by optional sep char (?P([0-9a-fA-F]{7})) # mandatory 7 chars of hex code (?P([0-9a-fA-F]{0,33})) # optional additional hex code 0-33 chars (?P\W+|$) # followed by >=1 non-alpha-num char or end of line """, re.X), r'\g\g\g\g'), (re.compile(r""" (?P(^|\s+)) # start w beginning of line or at least 1 whitespace (?P(certtools)[:/]?) # keyword 'mailgen' followed by optional sep char (?P([0-9a-fA-F]{7})) # mandatory 7 chars of hex code (?P([0-9a-fA-F]{0,33})) # optional additional hex code 0-33 chars (?P\W+|$) # followed by >=1 non-alpha-num char or end of line """, re.X), r'\g\g\g\g'), ] def local_replace(message): for cre, replacement in substitutions: message = cre.sub(replacement, message) return message def init(instance): instance.registerUtil('localReplace', local_replace) def quicktest(msgstr, should_replace = True): if not should_replace: print "(no)", print "'%s' -> '%s'" % (msgstr, local_replace(msgstr)) if "__main__" == __name__: print "Replacement examples. '(no)' should result in no replacement:" quicktest(" debian:#222") quicktest(" #555") quicktest("issue333") quicktest(" revision 222") quicktest(" r 222") quicktest(" wordthatendswithr 222", False) quicktest(" references", False) quicktest(" too many spaces r 222", False) quicktest("re-evaluate", False) quicktest("rex140eb", False) quicktest("rev 012", False) # too short for a hg hash quicktest("rev 0123") quicktest("re140eb") quicktest(" r7140eb") quicktest(" rev7140eb ") quicktest("rev7140eb") quicktest("rev7140eb,") quicktest("rev4891:ad3d628e73f2", False) quicktest("hg4891:ad3d628e73f2", False) quicktest("changeset: 4542:46239c21a1eb", False) quicktest("mailgen2a53f77") quicktest("manager83318fcc66e365848c8ae07cbf02ed80f5e89591") quicktest("manager/83318fcc66e365848c8ae07cbf02ed80f5e89591") quicktest("manager:83318fcc") quicktest("37a3db9850c7ae67f586693b3393d5b5086d2177") quicktest("certtools:87409c6b40a64eec3fc6b8f2a749fab46ee3715d")