Mercurial > public > mercurial-scm > hg
comparison mercurial/win32.py @ 8421:b6d0fa8c7685
posixfile: remove posixfile_nt and fix import bug in windows.py
The posixfile_nt class has been superseded by posixfile in osutils.c,
which works on Windows NT and above. All other systems get the regular
python file class which is assigned to posixfile in posix.py (for POSIX)
and in the pure python version of osutils.py (for Win 9x or Windows NT
in pure mode).
author | Sune Foldager <cryo@cyanite.org> |
---|---|
date | Wed, 13 May 2009 21:36:16 +0200 |
parents | 79a12651d46b |
children | 284fda4cd093 |
comparison
equal
deleted
inserted
replaced
8420:f53bc3e37655 | 8421:b6d0fa8c7685 |
---|---|
262 shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA)) | 262 shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA)) |
263 userdir = os.path.dirname(appdir) | 263 userdir = os.path.dirname(appdir) |
264 return [os.path.join(userdir, 'mercurial.ini'), | 264 return [os.path.join(userdir, 'mercurial.ini'), |
265 os.path.join(userdir, '.hgrc')] | 265 os.path.join(userdir, '.hgrc')] |
266 | 266 |
267 class posixfile_nt(object): | |
268 '''file object with posix-like semantics. on windows, normal | |
269 files can not be deleted or renamed if they are open. must open | |
270 with win32file.FILE_SHARE_DELETE. this flag does not exist on | |
271 windows < nt, so do not use this class there.''' | |
272 | |
273 # ideally, we could use win32file._open_osfhandle and avoid this | |
274 # class entirely, but we would need the win32 _fdopen function, | |
275 # which is not exported by the win32file module. | |
276 | |
277 def __init__(self, name, mode='rb'): | |
278 self.closed = False | |
279 self.name = name | |
280 self.mode = mode | |
281 access = 0 | |
282 if 'r' in mode or '+' in mode: | |
283 access |= win32file.GENERIC_READ | |
284 if 'w' in mode or 'a' in mode or '+' in mode: | |
285 access |= win32file.GENERIC_WRITE | |
286 if 'r' in mode: | |
287 creation = win32file.OPEN_EXISTING | |
288 elif 'a' in mode: | |
289 creation = win32file.OPEN_ALWAYS | |
290 else: | |
291 creation = win32file.CREATE_ALWAYS | |
292 try: | |
293 self.handle = win32file.CreateFile(name, | |
294 access, | |
295 win32file.FILE_SHARE_READ | | |
296 win32file.FILE_SHARE_WRITE | | |
297 win32file.FILE_SHARE_DELETE, | |
298 None, | |
299 creation, | |
300 win32file.FILE_ATTRIBUTE_NORMAL, | |
301 0) | |
302 except pywintypes.error, err: | |
303 raise WinIOError(err, name) | |
304 | |
305 def __iter__(self): | |
306 for line in self.readlines(): | |
307 yield line | |
308 | |
309 def read(self, count=-1): | |
310 try: | |
311 cs = cStringIO.StringIO() | |
312 while count: | |
313 wincount = int(count) | |
314 if wincount == -1: | |
315 wincount = 1048576 | |
316 val, data = win32file.ReadFile(self.handle, wincount) | |
317 if not data: break | |
318 cs.write(data) | |
319 if count != -1: | |
320 count -= len(data) | |
321 return cs.getvalue() | |
322 except pywintypes.error, err: | |
323 raise WinIOError(err) | |
324 | |
325 def readlines(self, sizehint=None): | |
326 # splitlines() splits on single '\r' while readlines() | |
327 # does not. cStringIO has a well behaving readlines() and is fast. | |
328 return cStringIO.StringIO(self.read()).readlines() | |
329 | |
330 def write(self, data): | |
331 try: | |
332 if 'a' in self.mode: | |
333 win32file.SetFilePointer(self.handle, 0, win32file.FILE_END) | |
334 nwrit = 0 | |
335 while nwrit < len(data): | |
336 val, nwrit = win32file.WriteFile(self.handle, data) | |
337 data = data[nwrit:] | |
338 except pywintypes.error, err: | |
339 raise WinIOError(err) | |
340 | |
341 def writelines(self, sequence): | |
342 for s in sequence: | |
343 self.write(s) | |
344 | |
345 def seek(self, pos, whence=0): | |
346 try: | |
347 win32file.SetFilePointer(self.handle, int(pos), whence) | |
348 except pywintypes.error, err: | |
349 raise WinIOError(err) | |
350 | |
351 def tell(self): | |
352 try: | |
353 return win32file.SetFilePointer(self.handle, 0, | |
354 win32file.FILE_CURRENT) | |
355 except pywintypes.error, err: | |
356 raise WinIOError(err) | |
357 | |
358 def close(self): | |
359 if not self.closed: | |
360 self.handle = None | |
361 self.closed = True | |
362 | |
363 def flush(self): | |
364 # we have no application-level buffering | |
365 pass | |
366 | |
367 def truncate(self, pos=0): | |
368 try: | |
369 win32file.SetFilePointer(self.handle, int(pos), | |
370 win32file.FILE_BEGIN) | |
371 win32file.SetEndOfFile(self.handle) | |
372 except pywintypes.error, err: | |
373 raise WinIOError(err) | |
374 | |
375 def getuser(): | 267 def getuser(): |
376 '''return name of current user''' | 268 '''return name of current user''' |
377 return win32api.GetUserName() | 269 return win32api.GetUserName() |
378 | 270 |
379 def set_signal_handler_win32(): | 271 def set_signal_handler_win32(): |