def testLocalFilesystemCDM(self): d = HTTPDelivery() rootDir = TemporaryDirectory() d.serverURI = 'http://localhost/content/' d.repositoryPath = rootDir.name # d.repositoryPath = '/var/www/repository' # ioc.Initializer.initialize(d) cdm = LocalFileSystemCDM() cdm.delivery = d # test publish from a file from the file system try: srcTmpFile = NamedTemporaryFile(delete=False) srcTmpFile.close() dstFile = join('testdir1', 'tempfile.txt') cdm.publishFromFile(dstFile, srcTmpFile.name) dstFilePath = join(d.getRepositoryPath(), dstFile) self.assertTrue(isfile(dstFilePath)) self.assertEqual(datetime.fromtimestamp(stat(dstFilePath).st_mtime), cdm.getTimestamp('testdir1/tempfile.txt')) finally: rmtree(dirname(dstFilePath)) remove(srcTmpFile.name) # test publish from a file from a zip archive try: inFileName = join('dir1', 'subdir2', 'file1.txt') dstFile = join('testdir2', 'tempfile2.txt') cdm.publishFromFile(dstFile, join(dirname(__file__), 'test.zip', inFileName)) dstFilePath = join(d.getRepositoryPath(), dstFile) self.assertTrue(isfile(dstFilePath)) finally: rmtree(dirname(dstFilePath)) # test publish from a directory from the file system srcTmpDir = TemporaryDirectory() dirs = ('test1/subdir1', 'test2/subdir1') for dir in dirs: fullPath = join(srcTmpDir.name, dir) makedirs(fullPath) with open(join(fullPath, 'text.html'), 'w') as _f: pass try: cdm.publishFromDir('testdir3', srcTmpDir.name) dstDirPath = join(d.getRepositoryPath(), 'testdir3') for dir in dirs: dstFilePath = join(dstDirPath, dir, 'text.html') self.assertTrue(isfile(dstFilePath)) self.assertEqual(datetime.fromtimestamp(stat(dstFilePath).st_mtime), cdm.getTimestamp(join('testdir3', dir, 'text.html'))) # test remove path filePath = 'testdir3/test1/subdir1/text.html' self.assertTrue(isfile(join(d.getRepositoryPath(), filePath))) cdm.removePath(filePath) self.assertFalse(isfile(join(d.getRepositoryPath(), filePath))) dirPath = 'testdir3/test2' self.assertTrue(isdir(join(d.getRepositoryPath(), dirPath))) cdm.removePath(dirPath) self.assertFalse(isdir(join(d.getRepositoryPath(), dirPath))) finally: rmtree(dstDirPath) # test publish from a directory from a zip file try: cdm.publishFromDir('testdir4', join(dirname(__file__), 'test.zip', 'dir1')) dstDirPath = join(d.getRepositoryPath(), 'testdir4') dstFilePath = join(dstDirPath, 'subdir1', 'file1.txt') self.assertTrue(isfile(dstFilePath)) dstFilePath = join(dstDirPath, 'subdir2', 'file2.txt') self.assertTrue(isfile(dstFilePath)) # Test whether republishing the same directory checks the last modified date # The file created manually in the repository should not be deleted because # the zip archive was not modified from the last publish dstFilePath = join(dstDirPath, 'sometestfile.txt') with open(dstFilePath, 'w') as _f: pass cdm.publishFromDir('testdir4', join(dirname(__file__), 'test.zip', 'dir1')) self.assertTrue(isfile(dstFilePath)) finally: rmtree(dstDirPath) # test publish from a string try: path = join('testdir5', 'somecontent.txt') cdm.publishContent(path, 'test') dstFilePath = join(d.getRepositoryPath(), path) self.assertTrue(isfile(dstFilePath)) finally: rmtree(join(d.getRepositoryPath(), dirname(path))) # test publish from a file object try: path = join('testdir6', 'somecontent2.txt') cdm.publishFromFile(path, BytesIO(b'test 2')) dstFilePath = join(d.getRepositoryPath(), path) self.assertTrue(isfile(dstFilePath)) finally: rmtree(join(d.getRepositoryPath(), dirname(path)))