Skip to content

Commit

Permalink
Added "scrapy" command with project settings auto-discovery. Refs scr…
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohoffman committed Aug 17, 2010
1 parent b563e56 commit d17695e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions bin/scrapy
@@ -0,0 +1,4 @@
#!/usr/bin/env python

from scrapy.tool import main
main()
2 changes: 2 additions & 0 deletions scrapy/commands/startproject.py
Expand Up @@ -12,6 +12,7 @@
TEMPLATES_PATH = join(scrapy.__path__[0], 'templates', 'project')

TEMPLATES_TO_RENDER = (
('scrapy.cfg',),
('scrapy-ctl.py',),
('${project_name}', 'settings.py.tmpl'),
('${project_name}', 'items.py.tmpl'),
Expand Down Expand Up @@ -44,6 +45,7 @@ def run(self, args, opts):

moduletpl = join(TEMPLATES_PATH, 'module')
copytree(moduletpl, join(project_name, project_name), ignore=IGNORE)
shutil.copy(join(TEMPLATES_PATH, 'scrapy.cfg'), project_name)
shutil.copy(join(TEMPLATES_PATH, 'scrapy-ctl.py'), project_name)
for paths in TEMPLATES_TO_RENDER:
path = join(*paths)
Expand Down
2 changes: 2 additions & 0 deletions scrapy/templates/project/scrapy.cfg
@@ -0,0 +1,2 @@
[default]
settings = ${project_name}.settings
32 changes: 32 additions & 0 deletions scrapy/tool.py
@@ -0,0 +1,32 @@
"""
Scrapy command-line tool
"""

import sys, os
from ConfigParser import RawConfigParser

def closest_scrapy_cfg(path='.', prevpath=None):
if path == prevpath:
return ''
path = os.path.abspath(path)
cfgfile = os.path.join(path, 'scrapy.cfg')
if os.path.exists(cfgfile):
return cfgfile
return closest_scrapy_cfg(os.path.dirname(path), path)

def main():
scrapy_cfg = closest_scrapy_cfg()
cfg_sources = [scrapy_cfg, os.path.expanduser('~/.scrapy.cfg'), '/etc/scrapy.cfg']
cfg = RawConfigParser()
cfg.read(cfg_sources)
if cfg.has_option('default', 'settings'):
os.environ['SCRAPY_SETTINGS_MODULE'] = cfg.get('default', 'settings')
projdir = os.path.dirname(scrapy_cfg)
if projdir not in sys.path:
sys.path.append(projdir)

from scrapy.cmdline import execute
execute()

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -90,7 +90,7 @@ def is_not_module(filename):
'packages': packages,
'cmdclass': cmdclasses,
'data_files': data_files,
'scripts': ['bin/scrapy-ctl.py', 'bin/scrapy-ws.py', 'bin/scrapy-sqs.py'],
'scripts': ['bin/scrapy', 'bin/scrapy-ctl.py', 'bin/scrapy-ws.py', 'bin/scrapy-sqs.py'],
'classifiers': [
'Programming Language :: Python',
'Programming Language :: Python :: 2.5',
Expand Down

0 comments on commit d17695e

Please sign in to comment.