hgrepo.py

Tue, 02 Feb 2016 20:47:47 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 02 Feb 2016 20:47:47 +0200
changeset 167
032c90f1727b
parent 165
8131cf387e3d
permissions
-rw-r--r--

Stuff

'''
	Copyright 2015 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. The name of the author may not be used to endorse or promote products
	   derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

import subprocess
import sys
import os
from configfile import Config

class HgRepository (object):
	def __init__ (self, name):
		name = name.lower()
		repoconfig = Config.get_node ('hg').get_node ('repos')
		repoinfo = repoconfig.get_node (name)

		if not repoinfo:
			raise ValueError ('Unknown repository "%s"' % name)

		self.name = name
		self.published = not bool (repoinfo.get_value ('extrarepo', default=False))
		self.url = repoinfo.get_value ('url')
		self.version = repoinfo.get_value ('version', default='')
		self.color = int (repoinfo.get_value ('colorcode', default=0))

		if not self.url:
			raise ValueError ('Repository %s has no url!' % name)

	def hg (self, *args):
		output = subprocess.check_output (['hg', '--cwd', self.name] + list (args))
		return output.decode ('utf-8', 'ignore')

	def is_valid (self):
		return os.path.isdir (os.path.join (self.name, '.hg'))

	def split_template (self, kvargs, separator):
		return separator.join ('{%s}' % x[1] for x in kvargs.items())

	def merge_template (self, data, args):
		result = {}
		items = list (args.items())
		assert (len (items) == len (data)), '''Bad amount of items from hg log'''

		for i in range (0, len (items)):
			result[items[i][0]] = data[i]

		return result

	def get_commit_data (self, rev, **kvargs):
		try:
			if not kvargs:
				raise ValueError ('template arguments must be provided')

			separator = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
			template = self.split_template (kvargs, separator)
			data = self.hg ('log', '--limit', '1', '--rev', rev, '--template', template)
			data = data.split (separator)
			return self.merge_template (data, kvargs)
		except subprocess.CalledProcessError:
			raise ValueError ('''couldn't find changeset %s in %s''' % (rev, repo.name))

	def incoming (self, maxcommits=0, **kvargs):
		if not kvargs:
			raise ValueError ('template arguments must be provided')

		commit_data = []
		separator = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
		separator2 = '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
		template = self.split_template (kvargs, separator)
		limit = ['--limit', str(maxcommits)] if maxcommits else []

		try:
			args = ['incoming'] + limit + ['--quiet', '--template', template + separator2]
			data = self.hg (*args)
			data = data.split (separator2)

			if not data[-1]:
				data = data[:-1]

			return [self.merge_template (data=x.split (separator), args=kvargs) for x in data]
		except subprocess.CalledProcessError:
			return []

	def clone (self):
		print ('Cloning %s from %s...' % (self.name, self.url))
		subprocess.call (['hg', 'clone', self.url, self.name])

mercurial