578f50416dc435bdeeeaaa69dd7968e4373fb881
[tools/git-buildpackage.git] / examples / zeitgeist-git.py
1 #! /usr/bin/python3
2 # vim: set fileencoding=utf-8 :
3 #
4 # (C) 2010 Guido Guenther <agx@sigxcpu.org>
5 #    This program is free software; you can redistribute it and/or modify
6 #    it under the terms of the GNU General Public License as published by
7 #    the Free Software Foundation; either version 2 of the License, or
8 #    (at your option) any later version.
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program; if not, write to the Free Software
17 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 #
19 # Simple Zeitgeist Git data source
20
21 """Post-commit hook to submit the commit to Zeitgeist (http://www.zeitgeist-project.com)
22
23 copy as post-commit to
24
25     .git/hooks/post-commit
26
27 in existing repositories or to
28
29     /usr/share/git-core/templates
30
31 so it get's used for new ones.
32 """
33
34
35 import os
36 import subprocess
37 import sys
38 import time
39
40 CLIENT = None
41
42 try:
43     from zeitgeist.client import ZeitgeistClient
44     from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
45 except ImportError:
46     pass
47 else:
48     try:
49         CLIENT = ZeitgeistClient()
50     except RuntimeError as e:
51         print("Unable to connect to Zeitgeist, won't send events. Reason: '%s'" %e)
52
53
54 def get_repo():
55     """Get uri of remote repository and its name"""
56     repo = None
57     uri = subprocess.Popen(['git', 'config', '--get', 'remote.origin.url'],
58                              stdout=subprocess.PIPE).communicate()[0]
59
60     if uri:
61         uri = uri.strip().decode(sys.getfilesystemencoding())
62         if '/' in uri:
63             sep = '/'
64         else:
65             sep = ':'
66         try:
67             repo = str(uri.rsplit(sep, 1)[1])
68         except IndexError: # no known separator
69             repo = uri
70         repo = repo.rsplit('.git', 1)[0]
71     return repo, uri
72
73
74 def main(argv):
75     interpretation = Interpretation.MODIFY_EVENT.uri
76
77     # FIXME: I'd be great if zeitgeist would allow for more detail:
78     #           * branch
79     #           * log summary (git log -1 --format=%s HEAD)
80     curdir = os.path.abspath(os.curdir).decode(sys.getfilesystemencoding())
81     uri = "file://%s" % curdir
82
83     repo, origin = get_repo()
84     if not repo:
85         repo = str(curdir.rsplit('/', 1)[1])
86         origin = uri
87
88     subject = Subject.new_for_values(
89                 uri = uri,
90                 interpretation = Interpretation.DOCUMENT.TEXT_DOCUMENT.PLAIN_TEXT_DOCUMENT.SOURCE_CODE.uri,
91                 manifestation = Manifestation.FILE_DATA_OBJECT.uri,
92                 text = repo,
93                 origin = origin)
94     event = Event.new_for_values(
95                 timestamp = int(time.time() * 1000),
96                 interpretation = interpretation,
97                 manifestation = Manifestation.USER_ACTIVITY.uri,
98                 actor = "application://gitg.desktop",
99                 subjects = [subject])
100     CLIENT.insert_event(event)
101
102 if __name__ == '__main__':
103     main(sys.argv)
104