def tryReadLink(path): try: prevPath, initialPath = path, path readlinkCount, maxReadlinkCount = 0, 100 while isLink(path): path = junction.readlink(path) if path == prevPath: # no idea if this can happen, but why risk it break # Prevent infinite loop if circular paths are present (A->B->C->A) readlinkCount = readlinkCount + 1 if readlinkCount == maxReadlinkCount: return initialPath prevPath = path return path except: return None
def test(self): link = 'junction' contents = 'bar' link_file = os.path.join(link, os.path.relpath(self.DIR_FILE, self.DIR)) create(self.DIR, link) self.assertEqual(os.path.abspath(self.DIR), readlink(link)) with open(link_file, 'r') as fd: self.assertEqual(fd.read(), self.DIR_FILE_CONTENTS) with open(self.DIR_FILE, 'w') as fd: fd.write(contents) with open(link_file, 'r') as fd: self.assertEqual(fd.read(), contents) unlink(link) self.assertFalse(os.path.exists(link))
def tryReadLink(path): try: return junction.readlink(path) except: return None
def new_readlink(path): if os.path.isdir(path): return junction.readlink(path) return hardlink.readlink(path)