def _fileContents(filename=None, path=None, file=None, length=None): assert not (filename and path) if filename: path = filename assert not (path and file) if length: params = (length,) else: params = () if path: path = os.path.normpath(path) #dsunittest.trace(">dsfile.fileContents file '%s'" % (path)) dsthread.blockEnterNamedSection(name="dsfileLock") data = "" try: if not os.path.exists(path): realPath = tempPathForPath(path=path) else: realPath = path f = __builtin__.file(realPath, "rb") data = f.read(*params) f.close() finally: dsthread.leaveNamedSection(name="dsfileLock") #dsunittest.trace("<dsfile.fileContents file '%s', length %u" % (path, len(data))) return data elif file: file.seek(0) return file.read(*params) else: assert 0, "Pass path or file"
def setFileContents(data, path=None, filename=None, file=None): assert not (filename and path) if filename: path = filename assert not (path and file) if path: path = os.path.normpath(path) dsthread.blockEnterNamedSection(name="dsfileLock") try: # first check if the file is actually changed before writing it out again if os.path.exists(path): f = __builtin__.file(path, "rb") readData = f.read() f.close() if readData == data: return #dsunittest.trace(">dsfile.setFileContents file '%s', length %u" % (path, len(data))) #dsunittest.trace("Writing %u bytes to file %s" % (len(data), path)) tempFile = tempPathForPath(path=path) f = __builtin__.file(tempFile, "wb") f.write(data) f.close() if os.path.exists(path): deleteFileOrDirectory(path=path) os.rename(tempFile, path) if os.path.exists(tempFile): deleteFileOrDirectory(path=tempFile) # === Check that the file can be read back correctly. f = __builtin__.file(path, "rb") readData = f.read() f.close() if readData != data: raise "dsfile.setFileContents: error, datas do not match for file '%s'.\ndata = '%s'\nreadData = '%s'" % (path, data, readData) finally: #dsunittest.trace("<dsfile.setFileContents file '%s', length %u" % (path, len(data))) dsthread.leaveNamedSection(name="dsfileLock") elif file: file.seek(0) file.write(data) file.truncate() else: assert 0, "Pass path or file"
def testSections(self): dsthread.assertEnterNamedSection(name="hello") dsthread.leaveNamedSection(name="hello") dsthread.blockEnterNamedSection(name="hello") dsthread.leaveNamedSection(name="hello") dsthread.assertEnterNamedSection(name="hello") self.assertRaises(AssertionError, dsthread.assertEnterNamedSection, name="hello") dsthread.leaveNamedSection(name="hello") self.assertRaises(AssertionError, dsthread.leaveNamedSection, name="hello")
def lf(): self.assertEquals(dsthread.propagatingThreadValue("myKey"), "myValue") dsthread.leaveNamedSection(name="mySection")
def func(): for i in range(100): dsunittest.trace("entering") dsthread.blockEnterNamedSection(name="hello2") dsunittest.trace("leaving") dsthread.leaveNamedSection(name="hello2")