diff --git a/scrapy/contrib/feedexport.py b/scrapy/contrib/feedexport.py index 92664220ccc..bedd839b88e 100644 --- a/scrapy/contrib/feedexport.py +++ b/scrapy/contrib/feedexport.py @@ -34,6 +34,8 @@ def open(spider): def store(file): """Store the given file stream""" + def clean(): + """Clean the storage if no items has been scraped""" @implementer(IFeedStorage) class BlockingFeedStorage(object): @@ -47,6 +49,8 @@ def store(self, file): def _store_in_thread(self, file): raise NotImplementedError + def clean(self): + pass @implementer(IFeedStorage) class StdoutFeedStorage(object): @@ -60,6 +64,8 @@ def open(self, spider): def store(self, file): pass + def clean(self): + pass @implementer(IFeedStorage) class FileFeedStorage(object): @@ -76,6 +82,9 @@ def open(self, spider): def store(self, file): file.close() + def clean(self): + if os.path.isfile(self.path): + os.remove(self.path) class S3FeedStorage(BlockingFeedStorage): @@ -176,6 +185,8 @@ def open_spider(self, spider): def close_spider(self, spider): slot = self.slot if not slot.itemcount and not self.store_empty: + slot.storage.store(slot.file) + slot.storage.clean() return slot.exporter.finish_exporting() logfmt = "%%s %s feed (%d items) in: %s" % (self.format, \ diff --git a/tests/test_contrib_feedexport.py b/tests/test_contrib_feedexport.py index 77eb443d5fd..03423329001 100644 --- a/tests/test_contrib_feedexport.py +++ b/tests/test_contrib_feedexport.py @@ -46,7 +46,8 @@ def _assert_stores(self, storage, path): self.assertTrue(os.path.exists(path)) with open(path, 'rb') as fp: self.assertEqual(fp.read(), b"content") - + yield storage.clean() + self.assertFalse(os.path.exists(path)) class FTPFeedStorageTest(unittest.TestCase):