Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

enable genspider command outside projects

Valdir Stumm Junior1779f5fecacb
diff --git a/scrapy/commands/genspider.py b/scrapy/commands/genspider.py
index 58bdb9156..d5498bb5c 100644
--- a/scrapy/commands/genspider.py
+++ b/scrapy/commands/genspider.py
@@ -25,7 +25,7 @@ def sanitize_module_name(module_name):
class Command(ScrapyCommand):
- requires_project = True
+ requires_project = False
default_settings = {'LOG_ENABLED': False}
def syntax(self):
@@ -94,14 +94,19 @@ class Command(ScrapyCommand):
'classname': '%sSpider' % ''.join(s.capitalize() \
for s in module.split('_'))
}
- spiders_module = import_module(self.settings['NEWSPIDER_MODULE'])
- spiders_dir = abspath(dirname(spiders_module.__file__))
+ if self.settings.get('NEWSPIDER_MODULE'):
+ spiders_module = import_module(self.settings['NEWSPIDER_MODULE'])
+ spiders_dir = abspath(dirname(spiders_module.__file__))
+ else:
+ spiders_module = None
+ spiders_dir = "."
spider_file = "%s.py" % join(spiders_dir, module)
shutil.copyfile(template_file, spider_file)
render_templatefile(spider_file, **tvars)
- print("Created spider %r using template %r in module:" % (name, \
- template_name))
- print(" %s.%s" % (spiders_module.__name__, module))
+ print("Created spider %r using template %r " % (name, \
+ template_name), end=('' if spiders_module else '\n'))
+ if spiders_module:
+ print("in module:\n %s.%s" % (spiders_module.__name__, module))
def _find_template(self, template):
template_file = join(self.templates_dir, '%s.tmpl' % template)
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 2e47160d7..cf415a388 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -146,6 +146,13 @@ class GenspiderCommandTest(CommandTest):
assert not exists(join(self.proj_mod_path, 'spiders', '%s.py' % self.project_name))
+class GenspiderStandaloneCommandTest(ProjectTest):
+
+ def test_generate_standalone_spider(self):
+ self.call('genspider', 'example', 'example.com')
+ assert exists(join(self.temp_path, 'example.py'))
+
+
class MiscCommandsTest(CommandTest):
def test_list(self):