Commit dd837476 authored by Inkhey's avatar Inkhey

first commit

parents
*.pyc
*.pyd
__pycache__
*.db
This diff is collapsed.
sqlGit
=======
SqlGit est un petit programme écrit en python dont le but est de créer
une base de données sql à partir des informations contenu dans un dépôt
git au sujet des auteurs, des commits et des fichiers concernés.
Le but étant de pouvoir utiliser ces informations afin de pouvoir les
étudier sous forme de graphes dans des programmes comme Gephi.
## Contexte ##
Ce programme à été réaliser dans le cadre d'un projet de l'enseignement
IC05 à l'UTC (Université Technologique de Compiègne,France).
## Contributors ##
- Guénaël Muller
## Pré-requis ##
* python, de préférence python 3.
## License ##
Le code source est sous la licence GPL version 3.
git est un marque déposée de …
sqlGit
=======
Small python program to create an sql database with data from git about
author,commit and files in order to be able to display them as a graph
in programs like Gephi.
## Context ##
Program reated for a projet in IC05 class at UTC (Université Téchnologique
de Compiègne, France) .
## Contributors ##
- Guénaël Muller
## Requirement ##
* Python, preferably python 3
## License ##
Code-source is under GPLv3 license.
git is a trademark of …
#!/usr/bin/python
# -*- coding: utf-8 -*-
# sqlGit
#
# Copyright 2015 Guénaël Muller <contact@inkey-art.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
import sqlite3
import os
# TODO:  import walkdir <- use walkir instead of os.walk to be able
# to select a depth
class sqlGit(object):
''' Transform Git data into sqlite database to
analyse these datas into program like gephi'''
# CREATE
def __init__(self, db):
''' Initialise sqlite database file'''
self.conn = sqlite3.connect(db)
def create_table(self):
''' create SQL tables : author, commits, file, link_author
commit and link commit_file .'''
c = self.conn.cursor()
c.execute('''CREATE TABLE author(
name text,
email text,
primary key(name,email));''')
c.execute('''CREATE TABLE commits(
subject text,
hash text primary key,
abbrev_hash text,
date integer);''')
# value of dir :
# root -> "."
# folder -> "./dossier"
# subfolders-> "./dossier1/dossier2"
c.execute('''CREATE TABLE file(
name text,
size integer,
dir text )''')
# Link
c.execute('''CREATE TABLE link_author_commits(
author_name text,
author_mail text,
commits text
references commits(hash),
foreign key (author_name,author_mail)
references author(name,mail) );''')
# TODO : chercher à voir si l'on trouve des données
# ajout/suppressions de lignes sur le commit pour un fichier défini.
c.execute('''CREATE TABLE link_file_commits(
file text,
dir text,
commits text,
foreign key (file,dir)
references author(name,dir),
foreign key (commits)
references commits(hash));''')
self.conn.commit()
# INSERT
# DATA
def getContent(self, path, depth):
os.chdir(path)
self.getAuthor()
self.getCommit()
self.getFile(1)
self.getFile(depth)
self.getLink_A_C()
self.getLink_F_C()
self.deleteUnusedFile()
def getAuthor(self):
''' insert author data into author table'''
f = os.popen("git log --format='%aN;;%aE'| sort -u")
dAuthor = f.read().splitlines()
c = self.conn.cursor()
for line in dAuthor:
data = line.split(';;')
c.execute(
'''INSERT INTO author(name,email)
values (?,?);''', data)
self.conn.commit()
def getCommit(self):
''' insert commit data into commits table'''
f = os.popen("git log --format='%s;;%H;;%t;;%at' | sort -u")
dCommit = f.read().splitlines()
c = self.conn.cursor()
i = 0
for line in dCommit:
data = line.split(';;')
c.execute(
'''INSERT INTO commits(subject,hash,abbrev_hash,date)
values (?,?,?,?);''', data)
self.conn.commit()
def getFile(self, depth):
''' insert file data into file table, depth define how precise
file data will be.'''
c = self.conn.cursor()
# TODO : gérer profondeur et ignorer fichier dans .git ?
for root, dirs, files in os.walk(".", topdown=True):
for fn in files:
path = os.path.join(root, fn)
size = os.stat(path).st_size
data = (root, size, fn)
c.execute(
'''INSERT INTO file(name,size,dir)
values (?,?,?);''', data)
self.conn.commit()
# LINKS
def getLink_A_C(self):
''' insert link data between author and commit into
link_author_commits table'''
f = os.popen('git log --pretty=format:"%aN;;%aE;;%H"')
dLinkC_A = f.read().splitlines()
c = self.conn.cursor()
i = 0
for line in dLinkC_A:
data = line.split(';;')
c.execute(
'''INSERT INTO link_author_commits(author_name,
author_mail,commits)
values (?,?,?);''', data)
self.conn.commit()
def getLink_F_C(self):
''' insert link data between file and commit into
link_author_commits table'''
# TODO : utiliser git show ?
c = self.conn.cursor()
c.execute("SELECT name,dir FROM file;")
for row in c.fetchall():
path = os.path.join(row[0], row[1])
f = os.popen('git log --pretty=format:"%H "' + ' -- ' + path)
dLinkF_C = f.read().splitlines()
for commits in dLinkF_C:
data = (row[0], row[1], commits)
c.execute(
'''INSERT INTO link_file_commits(file,dir,commits)
values (?,?,?);''', data)
self.conn.commit()
# CLEAN
def deleteUnusedFile(self):
''' delete unused file (who are not linked to any commit.)'''
c = self.conn.cursor()
c.execute(
'''select f.name,f.dir
from file f left join link_file_commits l
on f.name=l.file and f.dir=l.dir
where l.commits is null;''')
for row in c.fetchall():
c.execute(
'''DELETE from file
where name="''' + row[0] +
'" and dir="' + row[1] + '";')
self.conn.commit()
def cleanDB(self):
''' drop alls tables'''
c = self.conn.cursor()
listTable = ('author', 'commits', 'file', 'link_author_commits',
'link_file_commits')
for table in listTable:
c.execute('DROP TABLE ' + table + ' ;')
self.conn.commit()
# autotest
if __name__ == '__main__':
db = sqlGit('self.db')
db.cleanDB()
db.create_table()
db.getContent('.', 0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment