161
161
req.headers_out['X-IVLE-Return'] = 'Dir'
162
162
# Start by trying to do an SVN status, so we can report file version
165
166
status_list = svnclient.status(path, recurse=False, get_all=True,
167
list = filter(lambda x: x != None,
168
map(PysvnStatus_to_fileinfo(path), status_list))
168
for status in status_list:
169
filename, attrs = PysvnStatus_to_fileinfo(path, status)
170
listing[filename] = attrs
169
171
except pysvn.ClientError:
170
172
# Presumably the directory is not under version control.
171
173
# Fallback to just an OS file listing.
172
filenames = os.listdir(path)
173
list = map(file_to_fileinfo(path), filenames)
174
for filename in os.listdir(path):
175
listing[filename] = file_to_fileinfo(path, filename)
174
176
# The subversion one includes "." while the OS one does not.
175
177
# Add "." to the output, so the caller can see we are
177
list.append({"filename" : ".", "isdir" : True,
178
"mtime" : time.ctime(os.path.getmtime(path))})
179
listing["."] = {"isdir" : True,
180
"mtime" : time.ctime(os.path.getmtime(path))}
180
req.write(cjson.encode(list))
182
req.write(cjson.encode(listing))
182
184
# It's a file. Return the file contents.
183
185
# First get the mime type of this file
191
193
req.sendfile(path)
193
def file_to_fileinfo(path):
195
def file_to_fileinfo(path, filename):
194
196
"""Given a filename (relative to a given path), gets all the info "ls"
195
needs to display about the filename. Returns a dict mapping a number of
196
fields which are returned."""
197
# Note: curried so it can be used with map
199
fullpath = os.path.join(path, filename)
200
d = {"filename" : filename}
197
needs to display about the filename. Returns a dict containing a number
198
of fields related to the file (excluding the filename itself)."""
199
fullpath = os.path.join(path, filename)
201
file_stat = os.stat(fullpath)
202
if stat.S_ISDIR(file_stat.st_mode):
206
d["size"] = file_stat.st_size
207
(type, _) = mimetypes.guess_type(filename)
209
type = conf.mimetypes.default_mimetype
211
d["mtime"] = time.ctime(file_stat.st_mtime)
214
def PysvnStatus_to_fileinfo(path, status):
215
"""Given a PysvnStatus object, gets all the info "ls"
216
needs to display about the filename. Returns a pair mapping filename to
217
a dict containing a number of other fields."""
218
path = os.path.normcase(path)
219
fullpath = status.path
220
# If this is "." (the directory itself)
221
if path == os.path.normcase(fullpath):
222
# If this directory is unversioned, then we aren't
223
# looking at any interesting files, so throw
224
# an exception and default to normal OS-based listing.
225
if status.text_status == pysvn.wc_status_kind.unversioned:
226
raise pysvn.ClientError
227
# We actually want to return "." because we want its
231
filename = os.path.basename(fullpath)
233
text_status = status.text_status
234
d["svnstatus"] = str(text_status)
201
236
file_stat = os.stat(fullpath)
202
237
if stat.S_ISDIR(file_stat.st_mode):
203
238
d["isdir"] = True
205
240
d["isdir"] = False
206
241
d["size"] = file_stat.st_size
207
(type, _) = mimetypes.guess_type(filename)
242
(type, _) = mimetypes.guess_type(fullpath)
209
244
type = conf.mimetypes.default_mimetype
211
246
d["mtime"] = time.ctime(file_stat.st_mtime)
215
def PysvnStatus_to_fileinfo(path):
216
"""Given a PysvnStatus object, gets all the info "ls"
217
needs to display about the filename. Returns a dict mapping a number of
218
fields which are returned.
220
# Note: curried so it can be used with map
221
path = os.path.normcase(path)
223
fullpath = status.path
224
# If this is "." (the directory itself)
225
if path == os.path.normcase(fullpath):
226
# If this directory is unversioned, then we aren't
227
# looking at any interesting files, so throw
228
# an exception and default to normal OS-based listing.
229
if status.text_status == pysvn.wc_status_kind.unversioned:
230
raise pysvn.ClientError
231
# We actually want to return "." because we want its
235
filename = os.path.basename(fullpath)
236
d = {"filename" : filename}
237
text_status = status.text_status
238
d["svnstatus"] = str(text_status)
240
file_stat = os.stat(fullpath)
241
if stat.S_ISDIR(file_stat.st_mode):
245
d["size"] = file_stat.st_size
246
(type, _) = mimetypes.guess_type(fullpath)
248
type = conf.mimetypes.default_mimetype
250
d["mtime"] = time.ctime(file_stat.st_mtime)
252
# Here if, eg, the file is missing.
253
# Can't get any more information so just return d
248
# Here if, eg, the file is missing.
249
# Can't get any more information so just return d