1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import os
18 import os.path
19 import stat
20 import time
21 import pwd
22 import grp
23
24 from libxyz.exceptions import VFSError
25 from libxyz.exceptions import XYZRuntimeError
26 from libxyz.vfs import vfsobj
27 from libxyz.vfs import types
28 from libxyz.vfs import util
29 from libxyz.vfs import mode
30 from libxyz.core.utils import ustring
31
33 """
34 Local VFS object is used to access local filesystem
35 """
36
37 - def walk(self, top=None):
38 """
39 Directory tree walker
40 @param top: Top directory or self.path unless provided
41 @return: tuple (parent, dir, dirs, files) where:
42 parent - parent dir LocalVFSFile instance
43 dir - current dir LocalVFSFile instance
44 dirs - list of LocalVFSFile objects of directories
45 files - list of LocalVFSFile objects of files
46 """
47
48 top = top or self.path
49
50 try:
51 _dir, _dirs, _files = os.walk(top).next()
52 except StopIteration:
53 raise XYZRuntimeError(_(u"Unable to walk on %s") % ustring(top))
54
55 _abstop = os.path.abspath(top)
56
57 _dirs.sort()
58 _files.sort()
59
60 _parent = LocalVFSFile(os.path.dirname(_dir), self.enc)
61 _parent.name = u".."
62
63 if not isinstance(_parent.ftype, types.VFSTypeLink):
64 _parent.visual = u"/.."
65
66 return [
67 _parent,
68 LocalVFSFile(_dir, self.enc),
69 [LocalVFSFile(os.path.join(_abstop, x), self.enc)
70 for x in _dirs],
71 [LocalVFSFile(os.path.join(_abstop, x), self.enc)
72 for x in _files],
73 ]
74
75
76
78 """
79 Local file object
80 """
81
83 def _uid(uid):
84 try:
85 _name = pwd.getpwuid(uid).pw_name
86 except (KeyError, TypeError):
87 _name = None
88
89 if _name is not None:
90 return u"%s (%s)" % (ustring(uid), _name)
91 else:
92 return ustring(uid)
93
94
95
96 def _gid(gid):
97 try:
98 _name = grp.getgrgid(gid).gr_name
99 except (KeyError, TypeError):
100 _name = None
101
102 if _name is not None:
103 return u"%s (%s)" % (ustring(gid), _name)
104 else:
105 return ustring(gid)
106
107
108
109 super(LocalVFSFile, self).__init__(path, enc)
110
111 self.ftype = self._find_type(path)
112 self.vtype = self.ftype.vtype
113
114 self._set_attributes()
115
116 _time = lambda x: ustring(time.ctime(x))
117
118 self.attributes = (
119 (_(u"Name"), self.name),
120 (_(u"Type"), self.ftype),
121 (_(u"Access time"), _time(self.atime)),
122 (_(u"Modification time"), _time(self.mtime)),
123 (_(u"Change time"), _time(self.ctime)),
124 (_(u"Size in bytes"), ustring(self.size)),
125 (_(u"Owner"), _uid(self.uid)),
126 (_(u"Group"), _gid(self.gid)),
127 (_(u"Access mode"), ustring(self.mode)),
128 (_(u"Inode"), ustring(self.inode)),
129 (_(u"Type-specific data"), self.data),
130 )
131
132
133
135 return "<LocalVFSFile object: %s>" % self.path
136
137
138
141
142
143
145 """
146 Find out file type
147 """
148
149 try:
150 self._stat = os.lstat(path)
151 except OSError, e:
152 raise VFSError(_(u"Unable to stat file %s: %s" % (path, str(e))))
153
154 return util.get_file_type(self._stat.st_mode)
155
156
157
159 """
160 Set file attibutes
161 """
162
163 def set_link_attributes():
164 """
165 Set appropriate soft link attibutes
166 """
167
168 _realpath = os.readlink(self.path)
169 _fullpath = os.path.realpath(self.path)
170
171 if not os.path.exists(_fullpath):
172 self.vtype = u"!"
173 else:
174 try:
175 self.data = LocalVFSFile(_fullpath, self.enc)
176 except VFSError, e:
177 xyzlog.error(_(u"Error creating VFS object: %s") %
178 ustring(str(e)))
179 else:
180 if isinstance(self.data.ftype, types.VFSTypeDir):
181 self.vtype = u"~"
182
183 self.info = u""
184 self.visual = u"-> %s" % ustring(_realpath, self.enc)
185
186
187
188 def set_char_attributes():
189 """
190 Set appropriate character device attibutes
191 """
192
193 _dev = self._stat.st_rdev
194 self.info = u"%s, %s %s" % (os.major(_dev), os.minor(_dev),
195 self.mode)
196
197
198
199 self.atime = self._stat.st_atime
200 self.mtime = self._stat.st_mtime
201 self.ctime = self._stat.st_ctime
202 self.size = self._stat.st_size
203 self.uid = self._stat.st_uid
204 self.gid = self._stat.st_gid
205 self.inode = self._stat.st_ino
206 self.mode = mode.Mode(self._stat.st_mode)
207 self.visual = u"%s%s" % (self.vtype, self.name)
208 self.info = u"%s %s" % (util.format_size(self.size), self.mode)
209
210 if isinstance(self.ftype, types.VFSTypeLink):
211 set_link_attributes()
212 elif isinstance(self.ftype, types.VFSTypeChar):
213 set_char_attributes()
214 elif isinstance(self.ftype, types.VFSTypeFile):
215 _mode = stat.S_IMODE(self.mode.raw)
216
217
218 if _mode & 0111:
219 self.vtype = u"*"
220 self.visual = u"*%s" % self.name
221