- reworked repository watching. Now uses incoming only once to get a list of commits, then pulls, then operates on pulled data. The incoming command doesn't seem to support the '-r' switch properly? Should also be a bit faster now and should use less networking.

Sat, 04 Oct 2014 17:52:41 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 04 Oct 2014 17:52:41 +0300
changeset 39
afd590258a93
parent 38
71046c4430d1
child 40
f6512aacb114

- reworked repository watching. Now uses incoming only once to get a list of commits, then pulls, then operates on pulled data. The incoming command doesn't seem to support the '-r' switch properly? Should also be a bit faster now and should use less networking.

cobalt.py file | annotate | diff | comparison | revisions
--- a/cobalt.py	Sat Oct 04 15:06:38 2014 +0300
+++ b/cobalt.py	Sat Oct 04 17:52:41 2014 +0300
@@ -206,19 +206,6 @@
 	python = sys.executable
 	os.execl (python, python, * sys.argv)
 
-' Retrieves hg incoming from zandronum repository '
-def get_incoming_data (zanrepo, rev, template):
-	try:
-		if rev != '':
-			return zanrepo.hg_command ('incoming', '--quiet', '-r', rev, '--template', template)
-		else:
-			return zanrepo.hg_command ('incoming', '--quiet', '--template', template)
-		#fi
-	except:
-		pass
-	return ""
-#enddef
-
 ' Check if a repository exists '
 def check_repo_exists (repo_name):
 	print 'Checking that %s exists...' % repo_name
@@ -245,6 +232,21 @@
 
 repocheck_timeout = {'zandronum':(time.time()) + 15, 'zandronum-stable':(time.time() + 15)}
 
+def get_commit_data (zanrepo, rev, template):
+	return zanrepo.hg_command ('log', '-l', '1', '-r', rev, '--template', template)
+#enddef
+
+def decipher_hgapi_error (e):
+	# Blah, hgapi, why must your error messages be so mangled?
+	try:
+		rawmsg = e.message.replace('\n', '').replace('" +','').replace('\t','')
+		errmsg = re.compile (r'.*: tErr: (.*)Out:.*').match (rawmsg).group (1)
+		return [True, errmsg]
+	except:
+		return [False, '']
+	#endtry
+#enddef
+
 ' Retrieves and processes commits for zandronum repositories '
 ' Ensure both repositories are OK before using this! '
 def process_zan_repo_updates (repo_name):
@@ -262,8 +264,22 @@
 
 	repocheck_timeout[repo_name] = time.time() + (cfg ('hg_checkinterval', 15) * 60)
 	zanrepo = hgapi.Repo (repo_name)
-	data = get_incoming_data (zanrepo, '', '{node|short} {desc}\n')
-	commits_to_pull = [];
+	commit_data = []
+
+	try:
+		data = zanrepo.hg_command ('incoming', '--quiet', '--template', '{node|short} {desc}\n')
+	except hgapi.hgapi.HgException as e:
+		deciphered = decipher_hgapi_error (e)
+
+		if deciphered[0] and len(deciphered[1]) > 0:
+			chanlog ("error while using hg import on %s: %s" % (repo_name, deciphered[1]))
+		#fi
+
+		return
+	except Exception as e:
+		chanlog ("%s" % `e`)
+		return
+	#tried
 
 	for line in data.split ('\n'):
 		if line == '':
@@ -281,9 +297,37 @@
 
 		commit_node = match.group (1)
 		commit_message = match.group (2)
+		commit_data.append ([commit_node, commit_message])
+	#done
+
+	if len (commit_data) > 0:
+		pull_args = ['pull']
+
+		for commit in commit_data:
+			pull_args.append ('-r');
+			pull_args.append (commit[0]);
+		#done
 
 		try:
+			zanrepo.hg_command (*pull_args)
 
+			# Also pull these commits to the zandronum main repository
+			if usestable:
+				devrepo = hgapi.Repo ('zandronum')
+				pull_args.insert (1, '../zandronum-stable')
+				devrepo.hg_command (*pull_args)
+			#fi
+		except Exception as e:
+			chanlog ('Warning: unable to pull: %s' % `e`)
+			return
+		#tried
+	#fi
+
+	for commit in commit_data:
+		commit_node = commit[0]
+		commit_message = commit[1]
+
+		try:
 			rex = re.compile (r'^.*(fixes|resolves|addresses) ([0-9]+).*$')
 			match = rex.match (commit_message)
 
@@ -294,11 +338,11 @@
 			ticket_id = int (match.group (2))
 
 			# Acquire additional data
-			moredata = get_incoming_data (zanrepo, commit_node,
+			moredata = get_commit_data (zanrepo, commit_node,
 				'{author|nonempty}\n{date(date, \'%A %d %B %Y %T\')}\n{diffstat|nonempty}').split('\n')
 
 			if len (moredata) != 3:
-				chanlog ('commit %s: malformed hg data' % commit_node)
+				raise logical_exception ('malformed hg data')
 			#fi
 
 			commit_author = moredata[0]
@@ -306,11 +350,13 @@
 			commit_diffstat = moredata[2]
 			commit_email = ""
 
-			ticket_data = suds_client.service.mc_issue_get (g_config['trackeruser'], g_config['trackerpassword'], ticket_id)
-			if not ticket_data:
-				chanlog ("error: commit %s: ticket %s not found" % (commit_node, ticket_id))
+			try:
+				ticket_data = suds_client.service.mc_issue_get (g_config['trackeruser'],
+					g_config['trackerpassword'], ticket_id)
+			except Exception as e:
+				raise logical_exception ('%s' % `e`)
 				continue
-			#fi
+			#tried
 
 			# Remove the email address from the author if possible
 			rex = re.compile (r'^(.+) <([^>]+)>$.*')
@@ -320,6 +366,7 @@
 				commit_email = match.group (2)
 			#fi
 
+			# Try parse and prettify the diffstat
 			rex = re.compile (r'([0-9]+): \+([0-9]+)/-([0-9]+)')
 			match = rex.match (commit_diffstat)
 
@@ -336,9 +383,9 @@
 					's' if deleted != 1 else '')
 			#fi
 
-			files_added = filter (None, get_incoming_data (zanrepo, commit_node, '{file_adds}').split ('\n'))
-			files_removed = filter (None, get_incoming_data (zanrepo, commit_node, '{file_dels}').split ('\n'))
-			files_changed = filter (None, get_incoming_data (zanrepo, commit_node, '{file_mods}').split ('\n'))
+			files_added = filter (None, get_commit_data (zanrepo, commit_node, '{file_adds}').split (' '))
+			files_removed = filter (None, get_commit_data (zanrepo, commit_node, '{file_dels}').split (' '))
+			files_changed = filter (None, get_commit_data (zanrepo, commit_node, '{file_mods}').split (' '))
 
 			# Compare the email addresses against known developer usernames
 			commit_trackeruser = ''
@@ -431,34 +478,10 @@
 			num_commits += 1
 		except Exception as e:
 			chanlog ('Error while processing %s: %s' % (commit_node, `e`))
-			failed = True
+			continue
 		#tried
-
-		commits_to_pull.append (commit_node)
 	#done
 
-	if len (commits_to_pull) > 0:
-		pull_args = ['pull']
-
-		for commit in commits_to_pull:
-			pull_args.append ('-r');
-			pull_args.append (commit);
-		#done
-
-		try:
-			zanrepo.hg_command (*pull_args)
-
-			# Also pull these commits to the zandronum main repository
-			if usestable:
-				devrepo = hgapi.Repo ('zandronum')
-				pull_args.insert (1, '../zandronum-stable')
-				devrepo.hg_command (*pull_args)
-			#fi
-		except Exception as e:
-			chanlog ('Warning: unable to pull: %s' % `e`)
-		#tried
-	#fi
-
 	return num_commits > 0
 #enddef
 

mercurial