initial commit of fuseosc
[opensuse:osc.git] / fuse / fuseosc
1 #!/usr/bin/python
2
3 import osc
4 import osc.conf
5 import osc.core
6 import sys
7 import fuse
8 from fuse import Fuse
9 import stat
10 import os
11 import errno
12 import tempfile
13
14 fuse.fuse_python_api = (0, 2)
15
16 projects = []
17 files = {}
18 cache = {}
19
20 class EmptyStat(fuse.Stat):
21     def __init__(self):
22         self.st_mode = 0
23         self.st_ino = 0
24         self.st_dev = 0
25         self.st_nlink = 0
26         self.st_uid = 0
27         self.st_gid = 0
28         self.st_size = 0
29         self.st_atime = 0
30         self.st_mtime = 0
31         self.st_ctime = 0
32
33 class oscFS(Fuse):
34
35     def __init__(self, *args, **kw):
36         Fuse.__init__(self, *args, **kw)
37         print 'OK'
38
39     def getattr(self, path):
40         st = EmptyStat()
41         # path is project
42         if path == '/' or path in projects or len(filter(lambda x: x.startswith(path), projects)) > 0:
43             st.st_mode = stat.S_IFDIR | 0555
44             st.st_nlink = 2
45             return st
46         # path is package
47         if os.path.dirname(path) in projects:
48             st.st_mode = stat.S_IFDIR | 0555
49             st.st_nlink = 2
50             return st
51         # path is file
52         file = os.path.basename(path)
53         if files.has_key(file):
54             return files[file]
55         else:
56             return -errno.ENOENT
57
58     def readdir(self, path, offset):
59         yield fuse.Direntry('.')
60         yield fuse.Direntry('..')
61
62         if os.path.dirname(path) in projects: # path is package
63             prj = os.path.dirname(path).replace('/','')
64             pkg = os.path.basename(path)
65             files.clear()
66             for f in osc.core.meta_get_filelist(osc.conf.config['apiurl'], prj, pkg, verbose=True):
67                 st = EmptyStat()
68                 st.st_mode = stat.S_IFREG | 0444
69                 st.st_size = f.size
70                 st.st_atime = f.mtime
71                 st.st_ctime = f.mtime
72                 st.st_mtime = f.mtime
73                 files[f.name] = st
74                 yield fuse.Direntry(f.name)
75             return
76
77         if path in projects: # path is project
78             prj = path.replace('/','')
79             for p in osc.core.meta_get_packagelist(osc.conf.config['apiurl'], prj):
80                 yield fuse.Direntry(p)
81
82         else: # path is project structure
83             if (path != '/'):
84                 path += '/'
85             l = len(path)
86             for d in set( map(lambda x: x[l:].split('/')[0], filter(lambda x: x.startswith(path), projects) ) ) :
87                 yield fuse.Direntry(d)
88
89     def mythread ( self ):
90         print '*** mythread'
91         return -errno.ENOSYS
92
93     def chmod ( self, path, mode ):
94         print '*** chmod', path, oct(mode)
95         return -errno.ENOSYS
96
97     def chown ( self, path, uid, gid ):
98         print '*** chown', path, uid, gid
99         return -errno.ENOSYS
100
101     def fsync ( self, path, isFsyncFile ):
102         print '*** fsync', path, isFsyncFile
103         return -errno.ENOSYS
104
105     def link ( self, targetPath, linkPath ):
106         print '*** link', targetPath, linkPath
107         return -errno.ENOSYS
108
109     def mkdir ( self, path, mode ):
110         print '*** mkdir', path, oct(mode)
111         return -errno.ENOSYS
112
113     def mknod ( self, path, mode, dev ):
114         print '*** mknod', path, oct(mode), dev
115         return -errno.ENOSYS
116
117     def open ( self, path, flags ):
118         file = os.path.basename(path)
119         d = os.path.dirname(path)
120         pkg = os.path.basename(d)
121         prj = os.path.dirname(d).replace('/','')
122         if not cache.has_key(path):
123             tmp = tempfile.mktemp(prefix = 'oscfs_')
124             osc.core.get_source_file(osc.conf.config['apiurl'], prj, pkg, file, tmp)
125             f = open(tmp, 'r')
126             cache[path] = (f, tmp)
127
128     def read ( self, path, length, offset ):
129         if not cache.has_key(path):
130             return -errno.EACCES
131         f = cache[path][0]
132         f.seek(offset)
133         return f.read(length)
134
135     def readlink ( self, path ):
136         print '*** readlink', path
137         return -errno.ENOSYS
138
139     def release ( self, path, flags ):
140         if cache.has_key(path):
141             cache[path][0].close()
142             os.unlink(cache[path][1])
143             del cache[path]
144
145     def rename ( self, oldPath, newPath ):
146         print '*** rename', oldPath, newPath
147         return -errno.ENOSYS
148
149     def rmdir ( self, path ):
150         print '*** rmdir', path
151         return -errno.ENOSYS
152
153     def statfs ( self ):
154         print '*** statfs'
155         return -errno.ENOSYS
156
157     def symlink ( self, targetPath, linkPath ):
158         print '*** symlink', targetPath, linkPath
159         return -errno.ENOSYS
160
161     def truncate ( self, path, size ):
162         print '*** truncate', path, size
163         return -errno.ENOSYS
164
165     def unlink ( self, path ):
166         print '*** unlink', path
167         return -errno.ENOSYS
168
169     def utime ( self, path, times ):
170         print '*** utime', path, times
171         return -errno.ENOSYS
172
173     def write ( self, path, buf, offset ):
174         print '*** write', path, buf, offset
175         return -errno.ENOSYS
176
177 def fill_projects():
178     for prj in osc.core.meta_get_project_list(osc.conf.config['apiurl']):
179         projects.append( '/' + prj.replace(':', ':/') )
180
181 if __name__ == '__main__':
182     print 'Loading config ...',
183     osc.conf.get_config()
184     print 'OK'
185     print 'Getting projects list ...',
186     fill_projects()
187     print 'OK'
188     print 'Starting FUSE ...',
189     oscfs = oscFS( version = '%prog ' + fuse.__version__, usage = '', dash_s_do = 'setsingle')
190     oscfs.flags = 0
191     oscfs.multithreaded = 0
192     oscfs.parse(values = oscfs, errex = 1)
193     oscfs.main()