forked from danishprakash/githubfs
/
gfs.py
122 lines (112 loc) · 4.12 KB
/
gfs.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
#!/usr/bin/env python3
import os
import sys
import errno
import getpass
import pwd
from stat import S_IFDIR, S_IFLNK, S_IFREG, S_ISREG
from github import Github
from fuse import FUSE, FuseOSError, Operations
from time import time, mktime, sleep
class gfs(Operations):
def __init__(self, root):
print('[init]: ', root)
self.root = root
self.user = Github(input("Username: "), getpass.getpass("Password: "))
print('[init]: Establishing connection....')
self.repo_list = []
print('[init]: Fetching repositories...')
self.file_content_decoded = dict()
self.file_content_bytes = dict()
for repo in self.user.get_user().get_repos():
self.repo_list.append(repo.name)
files = repo.get_dir_contents('/')
for file_ in files:
if file_.name == repo.name:
continue
elif '.' in file_.name and not(file_.name.startswith('.')):
file_name = file_.name
file_content = repo.get_file_contents(file_name)
self.file_content_bytes[file_.name] = file_content.decoded_content
self.file_content_decoded[file_.name] = file_content.decoded_content.decode('utf-8')
print('Done')
def open(self, path, flags):
if path == '/' or path == '/repos':
pass
else:
path_ele = path.split('/')
file_name = path_ele[-1]
repo_name = path_ele[-2]
new_file = open(file_name, "w")
data = self.file_content_decoded[file_name]
new_file.write(data)
new_file.close
return len(data)
def getattr(self, path, fh=None):
print('[getattr]: ', path)
full_path = self.root + path
properties = dict(
st_mode = S_IFDIR | 755,
st_nlink = 2,
st_ctime=0,
st_mtime=0,
st_atime=0,
st_uid=pwd.getpwuid(os.getuid()).pw_uid,
st_gid=pwd.getpwuid(os.getuid()).pw_gid,
)
path_ele = path.split('/')
if path == '/' or path_ele[-1].startswith('.'):
pass
elif path == '/repos' or path_ele[-1] in self.repo_list:
pass
else:
path_ele = path.split('/')
file_name = path_ele[-1]
repo_name = path_ele[-2]
file_size = 4096
if file_name not in self.file_content_decoded.keys():
pass
else:
if '.' in file_name and not(file_name.startswith('.')):
file_size = len(self.file_content_decoded[file_name])
properties = dict(
st_mode=S_IFREG | 644,
st_size=file_size,
st_nlink=1,
)
return properties
def read(self, path, size, offset, fh=None):
print('[read]: ', path)
file_content = ''
path_ele = path.split('/')
if path == '/' or path == '/repos':
pass
else:
path = path.split('/')
repo_name = path[-2]
file_name = path[-1]
return self.file_content_bytes[file_name]
def readdir(self, path, fh):
print('[readdir]: ', path)
full_path = self.root + path
repo_list = ['.', '..']
path_ele = path.split('/')
if path.startswith('.'):
pass
elif path == '/':
return ['.', '..', 'repos']
elif path == '/repos':
return repo_list + self.repo_list
elif path_ele[-1] in self.repo_list:
repo_name = path_ele[-1]
for item in self.user.get_user().get_repos():
if item.name == repo_name:
files = item.get_dir_contents('/')
break
for item in files:
repo_list.append(item.name)
return repo_list
def main(mountpoint, root):
FUSE(gfs(root), mountpoint, nothreads=True, foreground=True)
if __name__ == '__main__':
main(sys.argv[2], sys.argv[1])