27
from breezy.lazy_import import lazy_import
27
from .lazy_import import lazy_import
28
28
lazy_import(globals(), """
29
29
from datetime import datetime
30
30
from datetime import timedelta
106
112
"""Make a filename read-only."""
107
113
mod = os.lstat(filename).st_mode
108
114
if not stat.S_ISLNK(mod):
110
116
chmod_if_possible(filename, mod)
113
119
def make_writable(filename):
114
120
mod = os.lstat(filename).st_mode
115
121
if not stat.S_ISLNK(mod):
117
123
chmod_if_possible(filename, mod)
125
131
# It is probably faster to just do the chmod, rather than
126
132
# doing a stat, and then trying to compare
127
133
os.chmod(filename, mode)
128
except (IOError, OSError),e:
134
except (IOError, OSError) as e:
129
135
# Permission/access denied seems to commonly happen on smbfs; there's
130
136
# probably no point warning about it.
131
137
# <https://bugs.launchpad.net/bzr/+bug/606537>
248
254
file_existed = False
250
256
rename_func(new, tmp_name)
251
except (errors.NoSuchFile,), e:
257
except (errors.NoSuchFile,) as e:
254
260
# RBC 20060103 abstraction leakage: the paramiko SFTP clients rename
255
261
# function raises an IOError with errno is None when a rename fails.
256
262
# This then gets caught here.
257
263
if e.errno not in (None, errno.ENOENT, errno.ENOTDIR):
265
except Exception as e:
260
266
if (getattr(e, 'errno', None) is None
261
267
or e.errno not in (errno.ENOENT, errno.ENOTDIR)):
264
270
file_existed = True
270
# This may throw an exception, in which case success will
272
rename_func(old, new)
274
except (IOError, OSError), e:
275
# source and target may be aliases of each other (e.g. on a
276
# case-insensitive filesystem), so we may have accidentally renamed
277
# source by when we tried to rename target
278
failure_exc = sys.exc_info()
279
if (file_existed and e.errno in (None, errno.ENOENT)
280
and old.lower() == new.lower()):
281
# source and target are the same file on a case-insensitive
282
# filesystem, so we don't generate an exception
274
# This may throw an exception, in which case success will
276
rename_func(old, new)
278
except (IOError, OSError) as e:
279
# source and target may be aliases of each other (e.g. on a
280
# case-insensitive filesystem), so we may have accidentally renamed
281
# source by when we tried to rename target
282
if (file_existed and e.errno in (None, errno.ENOENT)
283
and old.lower() == new.lower()):
284
# source and target are the same file on a case-insensitive
285
# filesystem, so we don't generate an exception
286
291
# If the file used to exist, rename it back into place
289
294
unlink_func(tmp_name)
291
296
rename_func(tmp_name, new)
292
if failure_exc is not None:
294
raise failure_exc[0], failure_exc[1], failure_exc[2]
299
299
# In Python 2.4.2 and older, os.path.abspath and os.path.realpath
397
399
# check for absolute path
398
400
drive = ntpath.splitdrive(path)[0]
399
401
if drive == '' and path[:2] not in('//','\\\\'):
401
403
# we cannot simply os.path.join cwd and path
402
404
# because os.path.join('C:','/path') produce '/path'
403
405
# and this is incorrect
439
441
fancy_rename(old, new, rename_func=os.rename, unlink_func=os.unlink)
441
443
if e.errno in (errno.EPERM, errno.EACCES, errno.EBUSY, errno.EINVAL):
442
444
# If we try to rename a non-existant file onto cwd, we get
443
445
# EPERM or EACCES instead of ENOENT, this will raise ENOENT
483
492
path_from_environ = _posix_path_from_environ
484
493
_get_home_dir = _posix_get_home_dir
485
494
getuser_unicode = _posix_getuser_unicode
487
496
dirname = os.path.dirname
488
497
basename = os.path.basename
489
498
split = os.path.split
567
576
:param trace: If True trace the selected encoding via mutter().
569
from breezy.trace import mutter
578
from .trace import mutter
570
579
output_encoding = getattr(sys.stdout, 'encoding', None)
571
580
if not output_encoding:
572
581
input_encoding = getattr(sys.stdin, 'encoding', None)
908
917
(date_fmt, tt, offset_str) = \
909
918
_format_date(t, offset, timezone, date_fmt, show_offset)
910
919
date_str = time.strftime(date_fmt, tt)
911
if not isinstance(date_str, unicode):
920
if not isinstance(date_str, text_type):
912
921
date_str = date_str.decode(get_user_encoding(), 'replace')
913
922
return date_str + offset_str
1102
1111
if config.GlobalStack().get('ignore_missing_extensions'):
1104
1113
# the warnings framework should by default show this only once
1105
from breezy.trace import warning
1114
from .trace import warning
1107
1116
"brz: warning: some compiled extensions could not be loaded; "
1108
1117
"see <https://answers.launchpad.net/bzr/+faq/703>")
1115
from breezy._chunks_to_lines_pyx import chunks_to_lines
1116
except ImportError, e:
1124
from ._chunks_to_lines_pyx import chunks_to_lines
1125
except ImportError as e:
1117
1126
failed_to_load_extension(e)
1118
from breezy._chunks_to_lines_py import chunks_to_lines
1127
from ._chunks_to_lines_py import chunks_to_lines
1121
1130
def split_lines(s):
1154
1163
os.link(src, dest)
1155
except (OSError, IOError), e:
1164
except (OSError, IOError) as e:
1156
1165
if e.errno != errno.EXDEV:
1158
1167
shutil.copyfile(src, dest)
1167
1176
_delete_file_or_dir(path)
1168
except (OSError, IOError), e:
1177
except (OSError, IOError) as e:
1169
1178
if e.errno in (errno.EPERM, errno.EACCES):
1170
1179
# make writable and try again
1363
1372
Otherwise it is decoded from the the filesystem's encoding. If decoding
1364
1373
fails, a errors.BadFilenameEncoding exception is raised.
1366
if type(filename) is unicode:
1375
if isinstance(filename, text_type):
1367
1376
return filename
1369
1378
return filename.decode(_fs_enc)
1378
1387
Otherwise it is decoded from utf-8. If decoding fails, the exception is
1379
1388
wrapped in a BzrBadParameterNotUnicode exception.
1381
if isinstance(unicode_or_utf8_string, unicode):
1390
if isinstance(unicode_or_utf8_string, text_type):
1382
1391
return unicode_or_utf8_string
1384
1393
return unicode_or_utf8_string.decode('utf8')
1690
1699
if orig_val is not None:
1691
1700
del os.environ[env_variable]
1693
if isinstance(value, unicode):
1702
if not PY3 and isinstance(value, text_type):
1694
1703
value = value.encode(get_user_encoding())
1695
1704
os.environ[env_variable] = value
1696
1705
return orig_val
1855
1864
# but that gets a bit tricky, and requires custom compiling
1856
1865
# for win98 anyway.
1858
from breezy._walkdirs_win32 import Win32ReadDir
1867
from ._walkdirs_win32 import Win32ReadDir
1859
1868
_selected_dir_reader = Win32ReadDir()
1860
1869
except ImportError:
1862
1871
elif _fs_enc in ('utf-8', 'ascii'):
1864
from breezy._readdir_pyx import UTF8DirReader
1873
from ._readdir_pyx import UTF8DirReader
1865
1874
_selected_dir_reader = UTF8DirReader()
1866
except ImportError, e:
1875
except ImportError as e:
1867
1876
failed_to_load_extension(e)
2000
2009
s = os.stat(src)
2001
2010
chown(dst, s.st_uid, s.st_gid)
2011
except OSError as e:
2004
2013
'Unable to copy ownership from "%s" to "%s". '
2005
2014
'You may want to set it manually.', src, dst)
2113
2122
empty string rather than raise an error), and repeats the recv if
2114
2123
interrupted by a signal.
2118
2127
bytes = sock.recv(max_read_size)
2119
except socket.error, e:
2128
except socket.error as e:
2120
2129
eno = e.args[0]
2121
2130
if eno in _end_of_stream_errors:
2122
2131
# The connection was closed by the other side. Callers expect
2169
2178
while sent_total < byte_count:
2171
2180
sent = sock.send(buffer(bytes, sent_total, MAX_SOCKET_CHUNK))
2172
except (socket.error, IOError), e:
2181
except (socket.error, IOError) as e:
2173
2182
if e.args[0] in _end_of_stream_errors:
2174
2183
raise errors.ConnectionReset(
2175
2184
"Error trying to write to socket", e)
2261
2270
global file_kind_from_stat_mode
2262
2271
if file_kind_from_stat_mode is file_kind_from_stat_mode_thunk:
2264
from breezy._readdir_pyx import UTF8DirReader
2273
from ._readdir_pyx import UTF8DirReader
2265
2274
file_kind_from_stat_mode = UTF8DirReader().kind_from_mode
2266
except ImportError, e:
2275
except ImportError as e:
2267
2276
# This is one time where we won't warn that an extension failed to
2268
2277
# load. The extension is never available on Windows anyway.
2269
from breezy._readdir_py import (
2278
from ._readdir_py import (
2270
2279
_kind_from_mode as file_kind_from_stat_mode
2272
2281
return file_kind_from_stat_mode(mode)
2324
2333
re_obj = re.compile(re_string, flags)
2325
2334
re_obj.search("")
2327
except errors.InvalidPattern, e:
2336
except errors.InvalidPattern as e:
2329
2338
where = ' in ' + where
2330
2339
# despite the name 'error' is a type
2580
2589
if fn is not None:
2592
except IOError as e:
2584
2593
# See bug #1075108, on some platforms fdatasync exists, but can
2585
2594
# raise ENOTSUP. However, we are calling fdatasync to be helpful
2586
2595
# and reduce the chance of corruption-on-powerloss situations. It