add management command to get data from repositories

This commit is contained in:
Frédéric Péters 2017-10-14 17:20:29 +02:00
parent 58cc380688
commit cb4a7ef263
3 changed files with 53 additions and 0 deletions

View File

View File

@ -0,0 +1,53 @@
import os
import subprocess
import time
from django.core.management.base import BaseCommand
from django.utils.dateparse import parse_datetime
from eodb.events.models import Commit
author_map = {
'fpeters@0d.be': 'fpeters@entrouvert.com',
'jschneider@entrouvrt.org': 'jschneider@entrouvert.com',
'bdauvergne@ciboulette.entrouvert.og': 'bdauvergne@entrouvert.com',
'bdauvergne@entrouvert.org': 'bdauvergne@entrouvert.com',
'bdauvergne@moria.entrouvert.org': 'bdauvergne@entrouvert.com',
'bdauvergne@lupin.entrouvert.com': 'bdauvergne@entrouvert.com',
'benjamin.dauvergne@a716ebb1-153a-0410-b759-cfb97c6a1b53': 'bdauvergne@entrouvert.com',
'mikael.ates@gmail.com': 'mates@entrouvert.com',
'mikael@xyzstation.(none)': 'mates@entrouvert.com',
'serghei@hekla.home': 'smihai@entrouvert.com',
'serghei.mihai@devlibre.net': 'smihai@entrouvert.com',
'Damien Laniel@localhost': 'dlaniel@entrouvert.com',
}
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('repository', nargs='+')
def handle(self, *args, **options):
t0 = time.time()
for repository in sorted(options.get('repository')):
os.chdir(repository)
module = os.path.basename(repository).replace('.git', '')
print module
try:
for line in subprocess.check_output(['git', 'log', '--format=format:%H|%an|%ae|%aI|%cI']).splitlines():
sha, author_name, author_email, author_date, commit_date = line.strip().split('|')
if not author_email:
author_email = author_name + '@entrouvert.com'
author_email = author_map.get(author_email, author_email)
commit, created = Commit.objects.get_or_create(sha=sha, defaults={
'module': module,
'author_datetime': parse_datetime(author_date),
'commit_datetime': parse_datetime(commit_date),
'author_email': author_email,})
if commit.author_email != author_email:
commit.author_email = author_email
commit.save()
except subprocess.CalledProcessError as e:
print ' ', e
print time.time() - t0