import os # Check for case-sensitive match. full_path is the full path to a file # (or directory), which must exist, albeit perhaps case-insensitively. # On a case-insensitive filesystem, we want to ensure that the basename # matches case-sensitively. Two cases: # # 1. We want a case-sensitive match to the entire basename, like a # directory name. Then pass False for loose_extension_match. # case_ok('/a/b/c/ZODB', False) # This returns True iff directory /a/b/c/ contains a file or # directory with name exactly 'ZODB'. # # 2. We're trying to check a Python file. This is more complicated, # because while Python does case-sensitive imports even on case- # insensitive filesystems, the case-sensitiviy applies only to the # name being imported, not to the extension on the file. For example, # gorp.pyo and gorp.PyO both work on Windows if you want to import gorp. # In this case, pass True for loose_extension_match. For example, # case_ok('/a/b/c/ZODB/__init__.py', True) # This returns True iff directory /a/b/c/ZODB/ contains a file (not # a directory!) with root name exactly '__init__' and an extension # that is a case-insensitive match to .py. def case_ok(full_path, loose_extension_match): assert os.path.exists(full_path) parentdir, basename = os.path.split(full_path) all_files = os.listdir(parentdir) if not loose_extension_match: return basename in all_files basename_name, basename_ext = os.path.splitext(basename) name_len = len(basename_name) basename_ext = basename_ext.lower() for fname in all_files: if fname[:name_len] != basename_name: continue # We have a case-sensitive match to basename_name on a prefix # of fname. root, ext = os.path.splitext(fname) if (len(root) == name_len and ext.lower() == basename_ext and os.path.isfile(os.path.join(parentdir, fname))): return True return False