import re hg_url_base = r'http://sourceforge.net/p/roundup/code/ci/' 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 the typical number:hash format of hg's own output # and then use use hash instead of the number (re.compile(r'(?P(^|\s+))(?P(rev\s*|hg\s*|changeset: ))(?P\d+):(?P[0-9a-fA-F]{12,40})(?P\W+|$)'), r'\g\g\g:\g\g'), # matching hg 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'), ] 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, substr = True): testcount['run'] += 1 replacedstr = local_replace(msgstr) if not (not replacedstr == msgstr ) == should_replace: print("(fail)", end=' ') testcount['failed'] += 1 elif substr and (msgstr not in replacedstr): print("(fail)", end=' ') testcount['failed'] += 1 if replacedstr == msgstr: print( "'%s'" % (msgstr,)) else: print("'%s' -> '%s'" % (msgstr, replacedstr)) if "__main__" == __name__: testcount = {'run':0 , 'failed': 0} print("Replacement examples:") quicktest(" debian:#222", substr=False) quicktest(" #555", substr=False) quicktest("issue333") quicktest(" revision 222", substr=False) quicktest(" r 222", substr=False) 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", substr=False) quicktest(" rev7140eb ", substr=False) quicktest("rev7140eb") quicktest("rev7140eb,", substr=False) quicktest("rev4891:ad3d628e73f2") quicktest("hg4891:ad3d628e73f2") quicktest("changeset: 4542:46239c21a1eb") quicktest("rev 4542:46239c21a1eb") quicktest("rev 4542:46239c21a1eb") # many spaces print() print(testcount)