hgrepo.py

Sat, 08 Aug 2015 14:17:30 +0300

author
Teemu Piippo <tsapii@utu.fi>
date
Sat, 08 Aug 2015 14:17:30 +0300
changeset 149
1759c15cd0ca
parent 146
c17b82b1f573
child 151
e24793fae424
permissions
-rw-r--r--

Added ability to define commands into the manifest

'''
	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, reponame):
		reponame = reponame.lower()
		repoconfig = Config.get_node ('hg').get_node ('repos')
		repoinfo = repoconfig.get_node (reponame)

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

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

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

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

		if sys.version_info >= (3, 0):
			output = output.decode ('utf-8', 'ignore')

		return output

	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):
		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)

	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():
		print ('Cloning %s...' % repo.name)
		subprocess.call (['hg', 'clone', repo.url, repo.name])

mercurial