~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2008-08-13 16:07:10 UTC
  • mto: (3606.2.4 1.6)
  • mto: This revision was merged to the branch mainline in revision 3625.
  • Revision ID: john@arbash-meinel.com-20080813160710-ns4og8r4y6xcmp0b
Factor out the common exception handling looking for ENOTDIR and use it
for osutils.walkdirs() and tree transform code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1119
1119
        raise errors.IllegalPath(path)
1120
1120
 
1121
1121
 
 
1122
_WIN32_ERROR_DIRECTORY = 267 # Similar to errno.ENOTDIR
 
1123
 
 
1124
def _is_error_enotdir(e):
 
1125
    """Check if this exception represents ENOTDIR.
 
1126
 
 
1127
    Unfortunately, python is very inconsistent about the exception
 
1128
    here. The cases are:
 
1129
      1) Linux, Mac OSX all versions seem to set errno == ENOTDIR
 
1130
      2) Windows, Python2.4, uses errno == ERROR_DIRECTORY (267)
 
1131
         which is the windows error code.
 
1132
      3) Windows, Python2.5 uses errno == EINVAL and
 
1133
         winerror == ERROR_DIRECTORY
 
1134
 
 
1135
    :param e: An Exception object (expected to be OSError with an errno
 
1136
        attribute, but we should be able to cope with anything)
 
1137
    :return: True if this represents an ENOTDIR error. False otherwise.
 
1138
    """
 
1139
    en = getattr(e, 'errno', None)
 
1140
    if (en == errno.ENOTDIR
 
1141
        or (sys.platform == 'win32'
 
1142
            and (en == _WIN32_ERROR_DIRECTORY
 
1143
                 or (en == errno.EINVAL
 
1144
                     and getattr(e, 'winerror', None) == _WIN32_ERROR_DIRECTORY)
 
1145
        ))):
 
1146
        return True
 
1147
    return False
 
1148
 
 
1149
 
1122
1150
def walkdirs(top, prefix=""):
1123
1151
    """Yield data about all the directories in a tree.
1124
1152
    
1170
1198
        append = dirblock.append
1171
1199
        try:
1172
1200
            names = sorted(_listdir(top))
1173
 
        except EnvironmentError, e:
1174
 
            # Py 2.4 and earlier will set errno to EINVAL to 
1175
 
            # ERROR_DIRECTORY (267).  Later versions set it to
1176
 
            # EINVAL and winerror gets set to ERROR_DIRECTORY.
1177
 
            en = getattr(e, 'errno', None)
1178
 
            if (en == errno.ENOTDIR or
1179
 
                (sys.platform=='win32' and en in (267, errno.EINVAL))):
1180
 
                # We have been asked to examine a file, this is fine.
1181
 
                pass
1182
 
            else:
 
1201
        except OSError, e:
 
1202
            if not _is_error_enotdir(e):
1183
1203
                raise
1184
1204
        else:
1185
1205
            for name in names: