Warn in case of multiple spiders - scrapy runspider

This commit is contained in:
harshasrinivas 2017-03-25 05:38:21 +05:30
parent 34f4434b17
commit 5b9304067a
2 changed files with 38 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import sys
import os
import warnings
from importlib import import_module
from scrapy.utils.spider import iter_spider_classes
@ -83,7 +84,16 @@ class Command(ScrapyCommand):
spclasses = list(iter_spider_classes(module))
if not spclasses:
raise UsageError("No spider found in file: %s\n" % filename)
self._check_multiple_spiders(spclasses)
spidercls = spclasses.pop()
self.crawler_process.crawl(spidercls, **opts.spargs)
self.crawler_process.start()
def _check_multiple_spiders(self, spclasses):
if len(spclasses) > 1:
msg = ("\n\nThere are multiple spiders in this file: "
"Only the last-defined spider '{}' will be run.\n".format(spclasses[-1].name))
warnings.warn(msg, UserWarning)

View File

@ -2,6 +2,7 @@ import os
import sys
import subprocess
import tempfile
import warnings
from time import sleep
from os.path import exists, join, abspath
from shutil import rmtree, copytree
@ -193,6 +194,24 @@ class MySpider(scrapy.Spider):
return []
"""
debug_log_multiple_spiders = """
import scrapy
class FirstSpider(scrapy.Spider):
name = 'spider1'
def start_requests(self):
self.logger.debug("spider1 Works!")
return []
class SecondSpider(scrapy.Spider):
name = 'spider2'
def start_requests(self):
self.logger.debug("spider2 Works!")
return []
"""
@contextmanager
def _create_file(self, content, name):
tmpdir = self.mktemp()
@ -267,6 +286,15 @@ class BadSpider(scrapy.Spider):
self.assertIn("start_requests", log)
self.assertIn("badspider.py", log)
def test_runspider_multiple_spiders(self):
log = self.get_log(self.debug_log_multiple_spiders)
self.assertIn("multiple spiders in this file", log)
self.assertIn("DEBUG: spider2 Works!", log)
self.assertIn("INFO: Spider opened", log)
self.assertIn("INFO: Closing spider (finished)", log)
self.assertIn("INFO: Spider closed (finished)", log)
class BenchCommandTest(CommandTest):