diff -r d86a81540a71 -r b3d1b356e544 mod_hg.py --- a/mod_hg.py Sat Jul 18 16:59:44 2015 +0300 +++ b/mod_hg.py Sun Aug 02 17:15:00 2015 +0300 @@ -26,11 +26,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' -from hgapi import hgapi, Repo from datetime import datetime import hgpoll as HgPoll import re import bt as Bt +import subprocess from configfile import Config from modulecore import command_error @@ -54,7 +54,7 @@ { 'name': 'hg', 'description': 'Executes a hg command', - 'args': ' ', + 'args': '', 'level': 'admin', }, @@ -64,20 +64,6 @@ 'args': ' ', 'level': 'admin', # TODO }, - - { - 'name': 'rebuildcommitsdb', - 'description': '''Rebuilds commits.db''', - 'args': None, - 'level': 'admin', - }, - - { - 'name': 'updatezadevtopic', - 'description': """Updates #zadev topic""", - 'args': None, - 'level': 'admin', - }, ] } @@ -127,135 +113,109 @@ data = "" node, reponame = resolve_node (args['key']) repourl = HgPoll.get_repo_info (reponame).get_value ('url') - repo = Repo (reponame) delim = '@@@@@@@@@@@@' try: - data = repo.hg_command ("log", "-l1", "-r", node, "--template", - delim.join (["{node|short}", - "{desc}", - "{author}", - "{diffstat}", - "{date|hgdate}", - "{bookmarks}", - "{latesttagdistance}", - "{latesttag}"])).split (delim) - except hgapi.HgException: + data = subprocess.check_output (['hg', '--cwd', reponame, "log", "-l1", "-r", node, + "--template", delim.join (["{node|short}", + "{desc}", + "{author|person}", + "{diffstat}", + "{date|hgdate}", + "{bookmarks}", + "{latesttagdistance}", + "{latesttag}", + "{author|email}"] + ) + ]).split (delim) + except Subprocess.CalledProcessError: command_error ('''couldn't find changeset %s in %s''' % (node, reponame)) - return + + node = data[0] + message = data[1] + author = data[2] + diffstat = data[3] + date = datetime.utcfromtimestamp (int (data[4].split (' ')[0])) + bookmarks = data[5] + latesttagdistance = int (data[6]) + latesttag = data[7] + email = data[8] + delta = datetime.utcnow() - date + datestring = '' + + if bookmarks: + bookmarks = HgPoll.prettify_bookmarks (bookmarks) + + # Find out the Zandronum version of this changeset + data = subprocess.check_output (['hg', '--cwd', reponame, 'cat', '--rev', node, 'src/version.h']) + zanversion = '' try: - node = data[0] - message = data[1] - author = data[2] - diffstat = data[3] - date = datetime.utcfromtimestamp (int (data[4].split (' ')[0])) - bookmarks = data[5] - latesttagdistance = int (data[6]) - latesttag = data[7] - delta = datetime.utcnow() - date - datestring = '' + regexps = [ \ + re.compile (r'#define\s+GAMEVER_STRING\s+"([^"]+)"'), \ + re.compile (r'#define\s+DOTVERSIONSTR_NOREV\s+"([^"]+)"'), \ + re.compile (r'#define\s+DOTVERSIONSTR\s+"([^"]+)"')] - if bookmarks: - bookmarks = HgPoll.prettify_bookmarks (bookmarks) - - # Find out the Zandronum version of this changeset - repo.hg_command ('revert', '-r', node, 'src/version.h') - zanversion = '' + for line in data.splitlines(): + for rex in regexps: + match = rex.match (line) + if match != None: + zanversion = match.group (1) + break - try: - with open (reponame + '/src/version.h') as version_file: - regexps = [ \ - re.compile (r'#define\s+GAMEVER_STRING\s+"([^"]+)"'), \ - re.compile (r'#define\s+DOTVERSIONSTR_NOREV\s+"([^"]+)"'), \ - re.compile (r'#define\s+DOTVERSIONSTR\s+"([^"]+)"')] + if match != None: + break + except IOError: + pass + + subprocess.call (['hg', '--cwd', reponame, 'revert', '--all']) + username = Config.find_developer_by_email (email) - for line in version_file: - for rex in regexps: - match = rex.match (line) - if match != None: - zanversion = match.group (1) - break + if username != '': + author = username - if match != None: - break - except IOError: - pass + # Try prettify the diffstat + match = re.match (r'^([0-9]+): \+([0-9]+)/-([0-9]+)$', diffstat) - repo.hg_command ('revert', '--all') - - # Remove the email address from the author if possible - match = re.compile (r'^(.+) <([^>]+)>$.*').match (author) - if match: - author = match.group (1) - email = match.group (2) + if match: + diffstat = "%s\003:\0033 +%s\003/\0034-%s\003" % (match.group (1), match.group (2), match.group (3)) - username = Config.find_developer_by_email (email) - - if username != '': - author = username - - # Try prettify the diffstat - rex = re.compile (r'^([0-9]+): \+([0-9]+)/-([0-9]+)$') - match = rex.match (diffstat) - - if match: - diffstat = "%s\003:\0033 +%s\003/\0034-%s\003" % (match.group (1), match.group (2), match.group (3)) - - if delta.days < 4: - if delta.days == 0: - if delta.seconds < 60: - datestring = 'just now' - elif delta.seconds < 3600: - minutes = delta.seconds / 60 - datestring = '%d minute%s ago' % (minutes, plural (minutes)) - else: - hours = delta.seconds / 3600 - datestring = '%d hour%s ago' % (hours, plural (hours)) + if delta.days < 4: + if delta.days == 0: + if delta.seconds < 60: + datestring = 'just now' + elif delta.seconds < 3600: + minutes = delta.seconds / 60 + datestring = '%d minute%s ago' % (minutes, plural (minutes)) else: - datestring = '%d day%s ago' % (delta.days, plural (delta.days)) + hours = delta.seconds / 3600 + datestring = '%d hour%s ago' % (hours, plural (hours)) else: - datestring = 'on %s' % (str (date)) - - versionstring = "" - if latesttagdistance != 0: - versionstring = '%s %s, %d hops from %s' % (zanversion, date.strftime ('%y%m%d-%H%M'), latesttagdistance, latesttag) - else: - versionstring = latesttag + datestring = '%d day%s ago' % (delta.days, plural (delta.days)) + else: + datestring = 'on %s' % (str (date)) - reply ('changeset\0035 %s%s\003 (%s)\003: committed by\0032 %s\003 %s,\0032 %s' % \ - (node, bookmarks, versionstring, author, datestring, diffstat)) - - for line in message.split ('\n'): - reply (' ' + line) + versionstring = "" + if latesttagdistance != 0: + versionstring = '%s %s, %d hops from %s' % (zanversion, date.strftime ('%y%m%d-%H%M'), + latesttagdistance, latesttag) + else: + versionstring = latesttag - reply ('url: %s/commits/%s' % (repourl, node)) - except hgapi.HgException as e: - result = HgPoll.decipher_hgapi_error (e) + reply ('changeset\0035 %s%s\003 (%s)\003: committed by\0032 %s\003 %s,\0032 %s' % \ + (node, bookmarks, versionstring, author, datestring, diffstat)) - if result[0]: - command_error (result[1]) - else: - command_error (`e`) + for line in message.split ('\n'): + reply (' ' + line) + + reply ('url: %s/commits/%s' % (repourl, node)) def cmd_hg (bot, args, reply, **rest): try: - repo = hgapi.Repo (args['repo']) - result = repo.hg_command (*args['command']) + result = subprocess.check_output (['hg'] + args['command']) reply (result) - except hgapi.hgapi.HgException as e: - result = HgPoll.decipher_hgapi_error (e) - - if result[0]: - command_error (result[1]) - else: - command_error (`e`) + except Exception as e: + command_error (str (e)) def cmd_resolves (bot, args, **rest): - HgPoll.announce_ticket_resolved (args['ticket'], args['changeset']) - -def cmd_rebuildcommitsdb (bot, args, **rest): - HgPoll.g_CommitsDb.create_new() - -def cmd_updatezadevtopic (bot, **rest): - HgPoll.update_zadev_topic() \ No newline at end of file + HgPoll.announce_ticket_resolved (args['ticket'], args['changeset']) \ No newline at end of file