~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Aaron Bentley
  • Date: 2006-06-03 16:23:09 UTC
  • mfrom: (1736 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: aaron.bentley@utoronto.ca-20060603162309-c975ca9ea9fea344
Merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import re
26
26
import sha
27
27
import shutil
 
28
import stat
28
29
import string
29
30
import sys
30
31
import time
38
39
                           PathNotChild,
39
40
                           IllegalPath,
40
41
                           )
 
42
from bzrlib.symbol_versioning import *
41
43
from bzrlib.trace import mutter
42
44
import bzrlib.win32console
43
45
 
74
76
        return f
75
77
 
76
78
 
77
 
def file_kind(f):
78
 
    mode = os.lstat(f)[ST_MODE]
79
 
    if S_ISREG(mode):
80
 
        return 'file'
81
 
    elif S_ISDIR(mode):
82
 
        return 'directory'
83
 
    elif S_ISLNK(mode):
84
 
        return 'symlink'
85
 
    elif S_ISCHR(mode):
86
 
        return 'chardev'
87
 
    elif S_ISBLK(mode):
88
 
        return 'block'
89
 
    elif S_ISFIFO(mode):
90
 
        return 'fifo'
91
 
    elif S_ISSOCK(mode):
92
 
        return 'socket'
93
 
    else:
94
 
        return 'unknown'
 
79
_formats = {
 
80
    stat.S_IFDIR:'directory',
 
81
    stat.S_IFCHR:'chardev',
 
82
    stat.S_IFBLK:'block',
 
83
    stat.S_IFREG:'file',
 
84
    stat.S_IFIFO:'fifo',
 
85
    stat.S_IFLNK:'symlink',
 
86
    stat.S_IFSOCK:'socket',
 
87
}
 
88
def file_kind(f, _formats=_formats, _unknown='unknown', _lstat=os.lstat):
 
89
    try:
 
90
        return _formats[_lstat(f).st_mode & 0170000]
 
91
    except KeyError:
 
92
        return _unknown
95
93
 
96
94
 
97
95
def kind_marker(kind):
104
102
    else:
105
103
        raise BzrError('invalid file kind %r' % kind)
106
104
 
107
 
def lexists(f):
108
 
    if hasattr(os.path, 'lexists'):
109
 
        return os.path.lexists(f)
110
 
    try:
111
 
        if hasattr(os, 'lstat'):
112
 
            os.lstat(f)
113
 
        else:
114
 
            os.stat(f)
115
 
        return True
116
 
    except OSError,e:
117
 
        if e.errno == errno.ENOENT:
118
 
            return False;
119
 
        else:
120
 
            raise BzrError("lstat/stat of (%r): %r" % (f, e))
 
105
lexists = getattr(os.path, 'lexists', None)
 
106
if lexists is None:
 
107
    def lexists(f):
 
108
        try:
 
109
            if hasattr(os, 'lstat'):
 
110
                os.lstat(f)
 
111
            else:
 
112
                os.stat(f)
 
113
            return True
 
114
        except OSError,e:
 
115
            if e.errno == errno.ENOENT:
 
116
                return False;
 
117
            else:
 
118
                raise BzrError("lstat/stat of (%r): %r" % (f, e))
 
119
 
121
120
 
122
121
def fancy_rename(old, new, rename_func, unlink_func):
123
122
    """A fancy rename, when you don't have atomic rename.
547
546
    return pathjoin(*p)
548
547
 
549
548
 
 
549
@deprecated_function(zero_nine)
550
550
def appendpath(p1, p2):
551
551
    if p1 == '':
552
552
        return p2