Skip to content

Commit

Permalink
del empty file created when no items scraped scrapy#872
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-yin committed Oct 8, 2014
1 parent 7be3479 commit aea50f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions scrapy/contrib/feedexport.py
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -60,6 +64,8 @@ def open(self, spider):
def store(self, file):
pass

def clean(self):
pass

@implementer(IFeedStorage)
class FileFeedStorage(object):
Expand All @@ -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):

Expand Down Expand Up @@ -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, \
Expand Down
3 changes: 2 additions & 1 deletion tests/test_contrib_feedexport.py
Expand Up @@ -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):

Expand Down

0 comments on commit aea50f2

Please sign in to comment.