-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_fuse.py
executable file
·264 lines (203 loc) · 6.88 KB
/
git_fuse.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env python
###############################################################################
##
## digger - Digging into some data mines
## Copyright (C) 2006 Andrew Straw <[email protected]>
## Copyright (C) 2010 Thammi
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU Affero General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Affero General Public License for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
###############################################################################
# structure taken from:
# http://sourceforge.net/apps/mediawiki/fuse/index.php?title=FUSE_Python_tutorial
import os, stat, errno
import fuse
from fuse import Fuse
from StringIO import StringIO
from datetime import datetime
from datehelper import iso_to_gregorian
from git_stats import *
from graphs import *
if not hasattr(fuse, '__version__'):
raise RuntimeError, \
"your fuse-py doesn't know of fuse.__version__, probably it's too old."
fuse.fuse_python_api = (0, 2)
hello_path = '/hello'
hello_str = 'Hello World!\n'
class MyStat(fuse.Stat):
def __init__(self):
self.st_mode = 0
self.st_ino = 0
self.st_dev = 0
self.st_nlink = 0
self.st_uid = 0
self.st_gid = 0
self.st_size = 0
self.st_atime = 0
self.st_mtime = 0
self.st_ctime = 0
def exec_svg(agg):
keys = agg.keys()
data = [(key, agg[key]) for key in keys]
buf = StringIO()
punch_svg(data, buf)
return buf.getvalue()
def exec_curve(agg):
keys = agg.keys()
keys.sort()
values = [agg[key] for key in keys]
data = (keys, values)
buf = StringIO()
line_plot(data, buf)
return buf.getvalue()
def curve_day(blob):
commits = blob.commits()
agg = aggre_count(commits, lambda c: date2num(datetime(*c['date'][:3])))
return exec_curve(agg)
def curve_week(blob):
def commit_to_week_num(commit):
week = date_to_week(commit['date'])
greg_date = iso_to_gregorian(*week)
return date2num(greg_date)
commits = blob.commits()
agg = aggre_count(commits, commit_to_week_num)
return exec_curve(agg)
def week_punchcard(blob):
commits = blob.commits()
agg = aggre_count(commits, lambda c: (c['date'][3], date_to_weekday(c['date'])))
return exec_svg(agg)
def single_week_punchcard(blob):
commits = blob.commits()
# find hours/days
day_agg = aggre_count(commits, lambda c: (c['date'][3], tuple(c['date'][:3])))
# transform to hours/weekday
week_agg = aggre_count(day_agg, lambda (h, d): (h, date_to_weekday(d)))
return exec_svg(week_agg)
def roll(blob):
dates = [datetime(*c['date'][:6]) for c in blob.commits()]
buf = StringIO()
roll_date_time(dates, buf)
return buf.getvalue()
class Cache:
def __init__(self):
self.data = {}
def contains(self, path):
data = self.data
return path in data and data[path][1]
def insert(self, data, path):
self.data[path] = [len(data), data, None]
def get_size(self, path):
return self.data[path][0]
def get_payload(self, path):
return self.data[path][1]
def sweep(self):
# TODO: sweep old items!
pass
class ProjectFS(Fuse):
ACTION_HOOKS = {
'punch_week.svg': week_punchcard,
'punch_week_single.svg': single_week_punchcard,
'curve_day.png': curve_day,
'curve_week.png': curve_week,
'roll.png': roll,
}
def __init__(self, base, *args, **kw):
self.base = base
Fuse.__init__(self, *args, **kw)
# elephant-caching
self.cache = Cache()
def _create_data(self, path):
blob_path, action_path = path.rsplit("/", 1)
blob = self._get_blob(blob_path)
data = self.ACTION_HOOKS[action_path](blob)
self.cache.insert(data, path)
return data
def _get_blob(self, path):
cur = self.base
for element in path[1:].split('/'):
if element:
subs = cur.subs()
if element in subs:
cur = subs[element]
else:
return None
return cur
def getattr(self, path):
st = MyStat()
if path == '/':
st.st_mode = stat.S_IFDIR | 0755
st.st_nlink = 2
elif path.split('/')[-1] in self.ACTION_HOOKS:
cache = self.cache
if not cache.contains(path):
data = self._create_data(path)
dlen = len(data)
else:
dlen = cache.get_size(path)
st.st_mode = stat.S_IFREG | 0444
st.st_nlink = 1
st.st_size = dlen
else:
if self._get_blob(path):
st.st_mode = stat.S_IFDIR | 0755
st.st_nlink = 1
else:
return -errno.ENOENT
return st
def readdir(self, path, offset):
for item in ['.', '..']:
yield fuse.Direntry(item)
blob = self._get_blob(path)
if blob:
for sub in blob.subs():
yield fuse.Direntry(str(sub))
for action in self.ACTION_HOOKS:
yield fuse.Direntry(action)
def open(self, path, flags):
blob_path, action_path = path.rsplit("/", 1)
if action_path not in self.ACTION_HOOKS or self._get_blob(blob_path) == None:
return -errno.ENOENT
accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
if (flags & accmode) != os.O_RDONLY:
return -errno.EACCES
def read(self, path, size, offset):
blob_path, action_path = path.rsplit("/", 1)
if action_path not in self.ACTION_HOOKS or self._get_blob(blob_path) == None:
return -errno.ENOENT
cache = self.cache
if not cache.contains(path):
data = self._create_data(path)
else:
data = cache.get_payload(path)
slen = len(data)
if offset < slen:
if offset + size > slen:
size = slen - offset
buf = data[offset:offset+size]
else:
buf = ''
return buf
def main():
usage="""
Showing off with some nice graphics
""" + Fuse.fusage
base = Base()
server = ProjectFS(base,
version="%prog " + fuse.__version__,
usage=usage,
dash_s_do='setsingle')
server.parse(errex=1)
server.main()
if __name__ == '__main__':
main()