1
#!/usr/bin/env python
2
#-*- coding:utf-8 -*-
3
#
4
# Copyright © 2009 Germán Póo-Caamaño <gpoo@gnome.org>
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU Library General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
"""
20
    Handle the git log output.
21
"""
22
23
import os
24
import sys
25
from dataloader import load_modules_from_csv, cmp_version
26
from settings import settings
27
28
29
def dump_git_log(module, log_filter, fd_output):
30
    """
31
        Dump the git log command of a repository (repo) into
32
        a file (fd_output) given a filter (log_filter).
33
        The log dumped may be user for future analysis.
34
    """
35
36
    command = 'git log -M --numstat %s --' % (log_filter)
37
38
    if os.path.exists(module):
39
        cwd = os.getcwd()
40
        os.chdir(module)
41
        data = os.popen(command, 'r')
42
43
        try:
44
            line = data.readline()
45
            while line:
46
                fd_output.write(line)
47
                line = data.readline()
48
        finally:
49
            data.close()
50
51
        os.chdir(cwd)
52
    else:
53
        print >> sys.stderr, '%s: Repository does not exists' % module
54
55
    return
56
57
58
def create_logfile(module, version, output_path, force=False):
59
    """
60
        Given a module and its version create a file
61
        where to dump the data.  It returns a file
62
        descriptor.
63
    """
64
65
    log_name = '%s-%s.log' % (module, version)
66
67
    log = os.path.join(output_path, module, log_name)
68
    dst = os.path.dirname(log)
69
70
    if not os.path.exists(dst):
71
        os.makedirs(dst)
72
73
    if not os.path.isfile(log) or force:
74
        fd = open(log, 'w')
75
        return fd
76
    else:
77
        return None
78
79
80
def logger(releases, modules, gitrepo_path, output_path, force=False):
81
    """
82
        Given a dict of modules to analize, the base path of the
83
        git repositories and to base path for the output, call
84
        gitdm for every module and tags.
85
86
        It skips a module already processed.  The option 'force'
87
        may force to process all modules.
88
    """
89
    
90
    for (repo, pkg) in modules.iteritems():
91
        print >> sys.stderr, '%20s:\r' % repo,
92
        status = ''
93
94
        repo_path = os.path.join(gitrepo_path, repo)
95
        versions = sorted(pkg['tags'].keys(), cmp_version, reverse=True)
96
97
        for i in range(0, len(versions)-1):
98
            cur_version = versions[i]
99
            prev_version = versions[i+1]
100
101
            next = pkg['tags'][cur_version]
102
            prev = pkg['tags'][prev_version]
103
104
            log_filter = '%s..%s' % (prev, next)
105
106
            print >> sys.stderr, '%-20s:%s→%-6s\r' %(repo, status, cur_version),
107
108
            fd = create_logfile(repo, cur_version, output_path, force)
109
110
            if fd:
111
                status += (' %s' % cur_version)
112
                dump_git_log(repo_path, log_filter, fd)
113
                fd.close()
114
115
        # Process the missing version in the table.  That is, from the
116
        # beggining until the tag for the first version.
117
        if len(versions) >= 1:
118
            cur_version = versions[len(versions)-1]
119
            next = pkg['tags'][cur_version]
120
121
            # no previous version, since the very beggining
122
            log_filter = '%s' % (next)
123
124
            print >> sys.stderr, '%-20s:%s→%-6s\r' %(repo, status, cur_version),
125
126
            fd = create_logfile(repo, cur_version, output_path, force)
127
128
            if fd:
129
                status += (' %s' % cur_version)
130
                dump_git_log(repo_path, log_filter, fd)
131
                fd.close()
132
133
        print >> sys.stderr, '%-20s:%s %-6s\n' % (repo, status, 'done'),
134
135
136
if __name__ == '__main__':
137
    releases, modules = load_modules_from_csv(settings['csvfile'])
138
    gitrepo_path = settings['gitrepo']
139
    output_path = settings['logoutput']
140
141
    logger(releases, modules, gitrepo_path, output_path, force=False)