1
# Bazaar-NG -- distributed version control
3
# Copyright (C) 2005 by Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
from shutil import copyfile
20
from stat import (S_ISREG, S_ISDIR, S_ISLNK, ST_MODE, ST_SIZE,
21
S_ISCHR, S_ISBLK, S_ISFIFO, S_ISSOCK)
22
from cStringIO import StringIO
36
from bzrlib.errors import (BzrError,
37
BzrBadParameterNotUnicode,
43
from bzrlib.trace import mutter
46
def make_readonly(filename):
47
"""Make a filename read-only."""
48
mod = os.stat(filename).st_mode
50
os.chmod(filename, mod)
53
def make_writable(filename):
54
mod = os.stat(filename).st_mode
56
os.chmod(filename, mod)
63
"""Return a quoted filename filename
65
This previously used backslash quoting, but that works poorly on
67
# TODO: I'm not really sure this is the best format either.x
70
_QUOTE_RE = re.compile(r'([^a-zA-Z0-9.,:/\\_~-])')
72
if _QUOTE_RE.search(f):
79
mode = os.lstat(f)[ST_MODE]
98
def kind_marker(kind):
101
elif kind == 'directory':
103
elif kind == 'symlink':
106
raise BzrError('invalid file kind %r' % kind)
109
if hasattr(os.path, 'lexists'):
110
return os.path.lexists(f)
112
if hasattr(os, 'lstat'):
118
if e.errno == errno.ENOENT:
121
raise BzrError("lstat/stat of (%r): %r" % (f, e))
123
def fancy_rename(old, new, rename_func, unlink_func):
124
"""A fancy rename, when you don't have atomic rename.
126
:param old: The old path, to rename from
127
:param new: The new path, to rename to
128
:param rename_func: The potentially non-atomic rename function
129
:param unlink_func: A way to delete the target file if the full rename succeeds
132
# sftp rename doesn't allow overwriting, so play tricks:
134
base = os.path.basename(new)
135
dirname = os.path.dirname(new)
136
tmp_name = u'tmp.%s.%.9f.%d.%s' % (base, time.time(), os.getpid(), rand_chars(10))
137
tmp_name = pathjoin(dirname, tmp_name)
139
# Rename the file out of the way, but keep track if it didn't exist
140
# We don't want to grab just any exception
141
# something like EACCES should prevent us from continuing
142
# The downside is that the rename_func has to throw an exception
143
# with an errno = ENOENT, or NoSuchFile
146
rename_func(new, tmp_name)
147
except (NoSuchFile,), e:
150
# RBC 20060103 abstraction leakage: the paramiko SFTP clients rename
151
# function raises an IOError with errno == None when a rename fails.
152
# This then gets caught here.
153
if e.errno not in (None, errno.ENOENT, errno.ENOTDIR):
156
if (not hasattr(e, 'errno')
157
or e.errno not in (errno.ENOENT, errno.ENOTDIR)):
164
# This may throw an exception, in which case success will
166
rename_func(old, new)
170
# If the file used to exist, rename it back into place
171
# otherwise just delete it from the tmp location
173
unlink_func(tmp_name)
175
rename_func(tmp_name, new)
178
def urlescape(relpath):
179
"""Escape relpath to be a valid url."""
180
if isinstance(relpath, unicode):
181
relpath = relpath.encode('utf-8')
182
return urllib.quote(relpath)
185
def urlunescape(url):
186
"""Unescape relpath from url format.
188
This returns a Unicode path from a URL
190
unquoted = urllib.unquote(url)
192
unicode_path = unquoted.decode('utf-8')
193
except UnicodeError, e:
194
raise InvalidURL(url, e)
198
def posix_local_path_to_url(path):
199
"""Convert a local path like ./foo into a URL like file:///path/to/foo
201
This also handles transforming escaping unicode characters, etc.
203
# importing directly from posixpath allows us to test this
204
# on non-posix platforms
205
from posixpath import abspath, normpath
206
return 'file://' + urlescape(normpath(abspath(path)))
209
def posix_local_path_from_url(url):
210
"""Convert a url like file:///path/to/foo into /path/to/foo"""
211
if not url.startswith('file:///'):
212
raise InvalidURL(url, 'local urls must start with file:///')
213
# We only strip off 2 slashes
214
return urlunescape(url[len('file://'):])
217
def win32_local_path_to_url(path):
218
"""Convert a local path like ./foo into a URL like file:///C|/path/to/foo
220
This also handles transforming escaping unicode characters, etc.
222
# importing directly from ntpath allows us to test this
223
# on non-win32 platforms
224
# TODO: jam 20060426 consider moving this import outside of the function
225
from ntpath import normpath, abspath
226
win32_path = normpath(abspath(path)).replace('\\', '/')
227
return 'file:///' + win32_path[0] + '|' + urlescape(win32_path[2:])
230
def win32_local_path_from_url(url):
231
"""Convert a url like file:///C|/path/to/foo into C:/path/to/foo"""
232
if not url.startswith('file:///'):
233
raise InvalidURL(url, 'local urls must start with file:///')
234
# We strip off all 3 slashes
235
win32_url = url[len('file:///'):]
236
if (win32_url[0] not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
237
or win32_url[1] != '|'
238
or win32_url[2] != '/'):
239
raise InvalidURL(url, 'Win32 file urls start with file:///X|/, where X is a valid drive letter')
240
# TODO: jam 20060426, we could .upper() or .lower() the drive letter
241
# for better consistency.
242
return win32_url[0] + u':' + urlunescape(win32_url[2:])
245
# Default is to just use the python builtins
246
abspath = os.path.abspath
247
realpath = os.path.realpath
248
pathjoin = os.path.join
249
normpath = os.path.normpath
251
mkdtemp = tempfile.mkdtemp
253
dirname = os.path.dirname
254
basename = os.path.basename
255
local_path_to_url = posix_local_path_to_url
256
local_path_from_url = posix_local_path_from_url
258
MIN_ABS_PATHLENGTH = 1
259
MIN_ABS_URLPATHLENGTH = len('file:///')
262
if os.name == "posix":
263
# In Python 2.4.2 and older, os.path.abspath and os.path.realpath
264
# choke on a Unicode string containing a relative path if
265
# os.getcwd() returns a non-sys.getdefaultencoding()-encoded
267
_fs_enc = sys.getfilesystemencoding()
269
return os.path.abspath(path.encode(_fs_enc)).decode(_fs_enc)
272
return os.path.realpath(path.encode(_fs_enc)).decode(_fs_enc)
274
if sys.platform == 'win32':
275
# We need to use the Unicode-aware os.path.abspath and
276
# os.path.realpath on Windows systems.
278
return os.path.abspath(path).replace('\\', '/')
281
return os.path.realpath(path).replace('\\', '/')
284
return os.path.join(*args).replace('\\', '/')
287
return os.path.normpath(path).replace('\\', '/')
290
return os.getcwdu().replace('\\', '/')
292
def mkdtemp(*args, **kwargs):
293
return tempfile.mkdtemp(*args, **kwargs).replace('\\', '/')
295
def rename(old, new):
296
fancy_rename(old, new, rename_func=os.rename, unlink_func=os.unlink)
298
local_path_to_url = win32_local_path_to_url
299
local_path_from_url = win32_local_path_from_url
301
MIN_ABS_PATHLENGTH = 3
302
MIN_ABS_URLPATHLENGTH = len('file:///C|/')
304
def normalizepath(f):
305
if hasattr(os.path, 'realpath'):
309
[p,e] = os.path.split(f)
310
if e == "" or e == "." or e == "..":
313
return pathjoin(F(p), e)
317
"""Copy a file to a backup.
319
Backups are named in GNU-style, with a ~ suffix.
321
If the file is already a backup, it's not copied.
327
if has_symlinks() and os.path.islink(fn):
328
target = os.readlink(fn)
329
os.symlink(target, bfn)
337
outf = file(bfn, 'wb')
345
"""True if f is an accessible directory."""
347
return S_ISDIR(os.lstat(f)[ST_MODE])
353
"""True if f is a regular file."""
355
return S_ISREG(os.lstat(f)[ST_MODE])
360
"""True if f is a symlink."""
362
return S_ISLNK(os.lstat(f)[ST_MODE])
366
def is_inside(dir, fname):
367
"""True if fname is inside dir.
369
The parameters should typically be passed to osutils.normpath first, so
370
that . and .. and repeated slashes are eliminated, and the separators
371
are canonical for the platform.
373
The empty string as a dir name is taken as top-of-tree and matches
376
>>> is_inside('src', pathjoin('src', 'foo.c'))
378
>>> is_inside('src', 'srccontrol')
380
>>> is_inside('src', pathjoin('src', 'a', 'a', 'a', 'foo.c'))
382
>>> is_inside('foo.c', 'foo.c')
384
>>> is_inside('foo.c', '')
386
>>> is_inside('', 'foo.c')
389
# XXX: Most callers of this can actually do something smarter by
390
# looking at the inventory
400
return fname.startswith(dir)
403
def is_inside_any(dir_list, fname):
404
"""True if fname is inside any of given dirs."""
405
for dirname in dir_list:
406
if is_inside(dirname, fname):
412
def pumpfile(fromfile, tofile):
413
"""Copy contents of one file to another."""
416
b = fromfile.read(BUFSIZE)
422
def file_iterator(input_file, readsize=32768):
424
b = input_file.read(readsize)
431
if hasattr(f, 'tell'):
444
def sha_strings(strings):
445
"""Return the sha-1 of concatenation of strings"""
447
map(s.update, strings)
457
def fingerprint_file(f):
462
return {'size': size,
463
'sha1': s.hexdigest()}
466
def compare_files(a, b):
467
"""Returns true if equal in contents"""
478
def local_time_offset(t=None):
479
"""Return offset of local zone from GMT, either at present or at time t."""
480
# python2.3 localtime() can't take None
484
if time.localtime(t).tm_isdst and time.daylight:
487
return -time.timezone
490
def format_date(t, offset=0, timezone='original', date_fmt=None,
492
## TODO: Perhaps a global option to use either universal or local time?
493
## Or perhaps just let people set $TZ?
494
assert isinstance(t, float)
496
if timezone == 'utc':
499
elif timezone == 'original':
502
tt = time.gmtime(t + offset)
503
elif timezone == 'local':
504
tt = time.localtime(t)
505
offset = local_time_offset(t)
507
raise BzrError("unsupported timezone format %r" % timezone,
508
['options are "utc", "original", "local"'])
510
date_fmt = "%a %Y-%m-%d %H:%M:%S"
512
offset_str = ' %+03d%02d' % (offset / 3600, (offset / 60) % 60)
515
return (time.strftime(date_fmt, tt) + offset_str)
518
def compact_date(when):
519
return time.strftime('%Y%m%d%H%M%S', time.gmtime(when))
524
"""Return size of given open file."""
525
return os.fstat(f.fileno())[ST_SIZE]
528
# Define rand_bytes based on platform.
530
# Python 2.4 and later have os.urandom,
531
# but it doesn't work on some arches
533
rand_bytes = os.urandom
534
except (NotImplementedError, AttributeError):
535
# If python doesn't have os.urandom, or it doesn't work,
536
# then try to first pull random data from /dev/urandom
537
if os.path.exists("/dev/urandom"):
538
rand_bytes = file('/dev/urandom', 'rb').read
539
# Otherwise, use this hack as a last resort
541
# not well seeded, but better than nothing
546
s += chr(random.randint(0, 255))
551
ALNUM = '0123456789abcdefghijklmnopqrstuvwxyz'
553
"""Return a random string of num alphanumeric characters
555
The result only contains lowercase chars because it may be used on
556
case-insensitive filesystems.
559
for raw_byte in rand_bytes(num):
560
s += ALNUM[ord(raw_byte) % 36]
564
## TODO: We could later have path objects that remember their list
565
## decomposition (might be too tricksy though.)
568
"""Turn string into list of parts.
574
>>> splitpath('a/./b')
576
>>> splitpath('a/.b')
578
>>> splitpath('a/../b')
579
Traceback (most recent call last):
581
BzrError: sorry, '..' not allowed in path
583
assert isinstance(p, types.StringTypes)
585
# split on either delimiter because people might use either on
587
ps = re.split(r'[\\/]', p)
592
raise BzrError("sorry, %r not allowed in path" % f)
593
elif (f == '.') or (f == ''):
600
assert isinstance(p, list)
602
if (f == '..') or (f == None) or (f == ''):
603
raise BzrError("sorry, %r not allowed in path" % f)
607
def appendpath(p1, p2):
611
return pathjoin(p1, p2)
615
"""Split s into lines, but without removing the newline characters."""
616
lines = s.split('\n')
617
result = [line + '\n' for line in lines[:-1]]
619
result.append(lines[-1])
623
def hardlinks_good():
624
return sys.platform not in ('win32', 'cygwin', 'darwin')
627
def link_or_copy(src, dest):
628
"""Hardlink a file, or copy it if it can't be hardlinked."""
629
if not hardlinks_good():
634
except (OSError, IOError), e:
635
if e.errno != errno.EXDEV:
639
def delete_any(full_path):
640
"""Delete a file or directory."""
644
# We may be renaming a dangling inventory id
645
if e.errno not in (errno.EISDIR, errno.EACCES, errno.EPERM):
651
if hasattr(os, 'symlink'):
657
def contains_whitespace(s):
658
"""True if there are any whitespace characters in s."""
659
for ch in string.whitespace:
666
def contains_linebreaks(s):
667
"""True if there is any vertical whitespace in s."""
675
def relpath(base, path):
676
"""Return path relative to base, or raise exception.
678
The path may be either an absolute path or a path relative to the
679
current working directory.
681
os.path.commonprefix (python2.4) has a bad bug that it works just
682
on string prefixes, assuming that '/u' is a prefix of '/u2'. This
686
assert len(base) >= MIN_ABS_PATHLENGTH, ('Length of base must be equal or'
687
' exceed the platform minimum length (which is %d)' %
694
while len(head) >= len(base):
697
head, tail = os.path.split(head)
701
raise PathNotChild(rp, base)
709
def urlrelpath(base, path):
710
"""Compute just the relative sub-portion of a url
712
This assumes that both paths are already fully specified URLs.
714
assert len(base) >= MIN_ABS_URLPATHLENGTH, ('Length of base must be equal or'
715
' exceed the platform minimum url length (which is %d)' %
716
MIN_ABS_URLPATHLENGTH)
718
base = local_path_from_url(base)
719
path = local_path_from_url(path)
720
return relpath(base, path)
723
def safe_unicode(unicode_or_utf8_string):
724
"""Coerce unicode_or_utf8_string into unicode.
726
If it is unicode, it is returned.
727
Otherwise it is decoded from utf-8. If a decoding error
728
occurs, it is wrapped as a If the decoding fails, the exception is wrapped
729
as a BzrBadParameter exception.
731
if isinstance(unicode_or_utf8_string, unicode):
732
return unicode_or_utf8_string
734
return unicode_or_utf8_string.decode('utf8')
735
except UnicodeDecodeError:
736
raise BzrBadParameterNotUnicode(unicode_or_utf8_string)
739
_platform_normalizes_filenames = False
740
if sys.platform == 'darwin':
741
_platform_normalizes_filenames = True
744
def normalizes_filenames():
745
"""Return True if this platform normalizes unicode filenames.
747
Mac OSX does, Windows/Linux do not.
749
return _platform_normalizes_filenames
752
if _platform_normalizes_filenames:
753
def unicode_filename(path):
754
"""Make sure 'path' is a properly normalized filename.
756
On platforms where the system normalizes filenames (Mac OSX),
757
you can access a file by any path which will normalize
759
Internally, bzr only supports NFC/NFKC normalization, since
760
that is the standard for XML documents.
761
So we return an normalized path, and indicate this has been
764
:return: (path, is_normalized) Return a path which can
765
access the file, and whether or not this path is
768
return unicodedata.normalize('NFKC', path), True
770
def unicode_filename(path):
771
"""Make sure 'path' is a properly normalized filename.
773
On platforms where the system does not normalize filenames
774
(Windows, Linux), you have to access a file by its exact path.
775
Internally, bzr only supports NFC/NFKC normalization, since
776
that is the standard for XML documents.
777
So we return the original path, and indicate if this is
780
:return: (path, is_normalized) Return a path which can
781
access the file, and whether or not this path is
784
return path, unicodedata.normalize('NFKC', path) == path
787
def terminal_width():
788
"""Return estimated terminal width."""
790
# TODO: Do something smart on Windows?
792
# TODO: Is there anything that gets a better update when the window
793
# is resized while the program is running? We could use the Python termcap
796
return int(os.environ['COLUMNS'])
797
except (IndexError, KeyError, ValueError):
800
def supports_executable():
801
return sys.platform != "win32"
804
def strip_url_trailing_slash(path):
805
"""Strip trailing slash, except for root paths.
806
The definition of 'root path' is platform-dependent.
808
assert path.startswith('file:///'), \
809
'strip_url_trailing_slash expects file:// urls (%s)' % path
810
if len(path) != MIN_ABS_URLPATHLENGTH and path[-1] == '/':
816
_validWin32PathRE = re.compile(r'^([A-Za-z]:[/\\])?[^:<>*"?\|]*$')
819
def check_legal_path(path):
820
"""Check whether the supplied path is legal.
821
This is only required on Windows, so we don't test on other platforms
824
if sys.platform != "win32":
826
if _validWin32PathRE.match(path) is None:
827
raise IllegalPath(path)