-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgitfs.py
More file actions
executable file
·212 lines (169 loc) · 6.35 KB
/
gitfs.py
File metadata and controls
executable file
·212 lines (169 loc) · 6.35 KB
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
#!/usr/bin/env python
#
# Use Git as a Storage Filesystem
#
# Copyright (c) 2011 Sreejith K <sreejitemk@gmail.com>
# Licensed under FreeBSD License. Refer COPYING for more information
#
# http://foobarnbaz.com
# Created on 11th Jan 2011
from __future__ import with_statement
from errno import EACCES
from sys import argv, exit
from time import time
import logging
import datetime
import os
from threading import Lock, Condition, Thread
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
class GitStatus(object):
def __init__(self, path):
if not os.getcwd() == path:
os.chdir(path)
self.status = {}
def update(self):
self.clear()
logging.debug('getting status')
for line in os.popen('git status').readlines():
line = line.strip()
if line.startswith('#\t'):
try:
status, file = [l.strip() for l in line[2:].split(':')]
if self.status.has_key(file):
self.status[status].append(file)
else:
self.status[status] = [file]
except ValueError:
if self.status.has_key('untracked'):
self.status['untracked'].append( line[2:].strip() )
else:
self.status['untracked'] = [ line[2:].strip() ]
logging.debug('current status: %r' %self.status)
return self.status
def stagedFiles(self):
self.update()
return self.status.get('renamed', []) + \
self.status.get('modified', []) + \
self.status.get('new file', []) + \
self.status.get('deleted', [])
def unstagedFiles(self):
self.update()
return self.status.get('untracked', [])
def clear(self):
self.status.clear()
class GitRepo(object):
def __init__(self, path, origin, branch, sync=False):
self.path = path
self.origin = origin
self.branch = branch
self.status = GitStatus(path)
# sync all the files with git
if sync:
self.synchronize()
def synchronize(self):
logging.debug('syncing')
if self.syncNeeded():
unstaged = self.status.unstagedFiles()
for file in unstaged:
self.stage(file)
self.commit('syncing files @ %s' %datetime.datetime.now())
self.push()
def syncNeeded(self):
return (self.status.stagedFiles() + self.status.unstagedFiles() and True or False)
def stage(self, file):
logging.debug('staging file %s' %file)
os.system('git add %s' %file)
def commit(self, msg):
logging.debug('commiting file %s' %file)
os.system('git commit -am \"%s\"' %msg)
def push(self):
logging.debug('pushing')
os.system('git push origin %s' %self.branch)
self.status.clear()
class GitFS(LoggingMixIn, Operations):
"""A simple filesystem using Git and FUSE.
"""
def __init__(self, origin, branch='master', path='.'):
self.origin = origin
self.branch = branch
self.root = os.path.realpath(path)
self.repo = GitRepo(path, origin, branch, sync=True)
self.halt = False
self.rwlock = Lock()
self.sync_c = Condition()
self.sync_thread = Thread(target=self._sync, args=())
self.sync_thread.start()
def _sync(self):
while True:
self.sync_c.acquire()
if not self.halt:
# wait till a sync request comes
self.sync_c.wait()
self.repo.synchronize()
self.sync_c.release()
else:
self.sync_c.release()
break
def destroy(self, path):
# stop sync thread
self.sync_c.acquire()
self.halt = True
self.sync_c.notifyAll()
self.sync_c.release()
def __call__(self, op, path, *args):
return super(GitFS, self).__call__(op, self.root + path, *args)
def access(self, path, mode):
if not os.access(path, mode):
raise FuseOSError(EACCES)
chmod = os.chmod
chown = os.chown
def create(self, path, mode):
return os.open(path, os.O_WRONLY | os.O_CREAT, mode)
def flush(self, path, fh):
return os.fsync(fh)
def fsync(self, path, datasync, fh):
return os.fsync(fh)
def getattr(self, path, fh=None):
st = os.lstat(path)
return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
getxattr = None
def link(self, target, source):
return os.link(source, target)
listxattr = None
mkdir = os.mkdir
mknod = os.mknod
open = os.open
def read(self, path, size, offset, fh):
with self.rwlock:
os.lseek(fh, offset, 0)
return os.read(fh, size)
def readdir(self, path, fh):
return ['.', '..'] + os.listdir(path)
readlink = os.readlink
def release(self, path, fh):
return os.close(fh)
def rename(self, old, new):
return os.rename(old, self.root + new)
rmdir = os.rmdir
def statfs(self, path):
stv = os.statvfs(path)
return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
'f_frsize', 'f_namemax'))
def symlink(self, target, source):
return os.symlink(source, target)
def truncate(self, path, length, fh=None):
with open(path, 'r+') as f:
f.truncate(length)
unlink = os.unlink
utimens = os.utime
def write(self, path, data, offset, fh):
with self.rwlock:
os.lseek(fh, offset, 0)
return os.write(fh, data)
if __name__ == "__main__":
if len(argv) != 5:
print 'usage: %s <origin> <branch> <local_repo> <mount_point>' % argv[0]
exit(1)
fuse = FUSE(GitFS(argv[1], argv[2], argv[3]), argv[4], foreground=True)