hgdb.py

Wed, 26 Aug 2015 05:39:23 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Wed, 26 Aug 2015 05:39:23 +0300
changeset 157
aaa5618e8dc2
parent 154
df862cca1773
permissions
-rw-r--r--

Make cobalt not forget its pages the moment someone days something
Added .strftime

'''
	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 os
import sqlite3
import hgpoll
from datetime import datetime
from configfile import Config

class HgCommitsDatabase (object):
	def __init__(self):
		dbname = Config.get_node('hg').get_value('commits_db', 'commits.db')
		needNew = not os.path.isfile (dbname)
		self.db = sqlite3.connect (dbname)

		if needNew:
			self.create_new()

	def create_new (self):
		self.db.executescript ('''
			DROP TABLE IF EXISTS COMMITS;
			DROP TABLE IF EXISTS REPOS;
			DROP TABLE IF EXISTS REPOCOMMITS;
			CREATE TABLE IF NOT EXISTS COMMITS
			(
				Node        text NOT NULL,
				Dateversion text NOT NULL,
				PRIMARY KEY (Node)
			);
			CREATE TABLE IF NOT EXISTS REPOS
			(
				Name     text NOT NULL,
				PRIMARY KEY (Name)
			);
			CREATE TABLE IF NOT EXISTS REPOCOMMITS
			(
				Reponame text,
				Node     text,
				FOREIGN KEY (Reponame) REFERENCES REPOS(Name),
				FOREIGN KEY (Node) REFERENCES COMMITS(Node)
			);
		''')

		print ('Building commits.db...')
		for repo in hgpoll.Repositories:
			print ('Adding commits from %s...' % repo.name)

			for line in repo.hg ('log', '--template', '{node} {date|hgdate}\n').splitlines():
				changeset, timestamp, tz = line.split(' ')
				self.add_commit (repo, changeset, int (timestamp))

		self.commit()

	def add_commit (self, repo, changeset, timestamp):
		dateversion = datetime.utcfromtimestamp (timestamp).strftime ('%y%m%d-%H%M')
		self.db.execute ('''
			INSERT OR IGNORE INTO REPOS
			VALUES (?)
		''', (repo.name,))

		self.db.execute ('''
			INSERT OR IGNORE INTO COMMITS
			VALUES (?, ?)
		''', (changeset, dateversion))

		self.db.execute ('''
			INSERT INTO REPOCOMMITS 
			VALUES (?, ?)
		''', (repo.name, changeset))

	def get_commit_repos (self, node):
		cursor = self.db.execute ('''
			SELECT Reponame
			FROM REPOCOMMITS
			WHERE Node LIKE ?
		''', (node + '%',))

		names = cursor.fetchall()
		names = list (zip (*names))[0] if names else []
		return [hgpoll.RepositoriesByName[name] for name in set (names)]

	def find_commit_by_dateversion (self, dateversion):
		cursor = self.db.execute ('''
			SELECT Node
			FROM COMMITS
			WHERE Dateversion = ?
		''', (dateversion,))
		result = cursor.fetchone()
		return result[0] if result else None

	def commit(self):
		self.db.commit()

mercurial