~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2006-05-27 05:54:50 UTC
  • mto: (1711.2.26 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1734.
  • Revision ID: john@arbash-meinel.com-20060527055450-acbbfbf55c3c4aa5
Updated version of file_kind. Rather than multiple function calls, one mask + dictionary lookup

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
75
76
        return f
76
77
 
77
78
 
 
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
}
78
88
def file_kind(f):
79
 
    mode = os.lstat(f)[ST_MODE]
80
 
    if S_ISREG(mode):
81
 
        return 'file'
82
 
    elif S_ISDIR(mode):
83
 
        return 'directory'
84
 
    elif S_ISLNK(mode):
85
 
        return 'symlink'
86
 
    elif S_ISCHR(mode):
87
 
        return 'chardev'
88
 
    elif S_ISBLK(mode):
89
 
        return 'block'
90
 
    elif S_ISFIFO(mode):
91
 
        return 'fifo'
92
 
    elif S_ISSOCK(mode):
93
 
        return 'socket'
94
 
    else:
 
89
    fmt = stat.S_IFMT(os.lstat(f).st_mode)
 
90
    try:
 
91
        return _formats[fmt]
 
92
    except KeyError:
95
93
        return 'unknown'
96
94
 
97
95