summaryrefslogtreecommitdiffstats
path: root/eodb/events/management/commands/feed_repositories.py
blob: c289c8328855accb97871176fce684be6a54f200 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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