2008-07-27 02:23:14 +00:00
|
|
|
#!/usr/bin/env python
|
2008-10-05 07:37:21 +00:00
|
|
|
"""Scrapy admin script is used to create new scrapy projects and similar
|
|
|
|
tasks"""
|
|
|
|
import os
|
2009-02-05 12:12:15 +00:00
|
|
|
import string
|
2008-07-27 02:23:14 +00:00
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
import scrapy
|
2009-02-05 12:12:15 +00:00
|
|
|
from scrapy.utils.misc import render_templatefile, string_camelcase
|
2009-02-16 23:43:43 +00:00
|
|
|
from scrapy.utils.python import ignore_patterns, copytree
|
2008-07-27 02:23:14 +00:00
|
|
|
|
|
|
|
usage = """
|
2008-10-05 07:37:21 +00:00
|
|
|
scrapy-admin.py [options] [command]
|
2008-07-27 02:23:14 +00:00
|
|
|
|
|
|
|
Available commands:
|
|
|
|
|
|
|
|
startproject <project_name>
|
|
|
|
Starts a new project with name 'project_name'
|
|
|
|
"""
|
|
|
|
|
2009-02-05 12:12:15 +00:00
|
|
|
PROJECT_TEMPLATES_PATH = os.path.join(scrapy.__path__[0], 'templates/project')
|
|
|
|
|
|
|
|
# This is the list of templatefile's path that are rendered *after copying* to
|
|
|
|
# project directory.
|
2008-11-17 10:33:15 +00:00
|
|
|
TEMPLATES = (
|
2009-02-05 12:12:15 +00:00
|
|
|
'scrapy-ctl.py',
|
2009-04-10 08:06:44 +00:00
|
|
|
'${project_name}/settings.py.tmpl',
|
|
|
|
'${project_name}/items.py.tmpl',
|
|
|
|
'${project_name}/pipelines.py.tmpl',
|
2008-11-17 10:33:15 +00:00
|
|
|
)
|
|
|
|
|
2009-02-16 12:50:00 +00:00
|
|
|
IGNORE = ignore_patterns('*.pyc', '.svn')
|
|
|
|
|
2008-07-27 02:23:14 +00:00
|
|
|
def main():
|
2008-10-05 07:37:21 +00:00
|
|
|
parser = OptionParser(usage=usage)
|
2008-07-27 02:23:14 +00:00
|
|
|
opts, args = parser.parse_args()
|
2009-02-05 12:12:15 +00:00
|
|
|
|
2008-10-05 07:37:21 +00:00
|
|
|
if not args:
|
|
|
|
parser.print_help()
|
2008-11-17 10:33:15 +00:00
|
|
|
return
|
2008-10-05 07:37:21 +00:00
|
|
|
|
|
|
|
cmd = args[0]
|
|
|
|
if cmd == "startproject":
|
|
|
|
if len(args) >= 2:
|
|
|
|
project_name = args[1]
|
2009-02-05 12:12:15 +00:00
|
|
|
project_root_path = project_name
|
|
|
|
|
|
|
|
roottpl = os.path.join(PROJECT_TEMPLATES_PATH, 'root')
|
2009-02-16 12:50:00 +00:00
|
|
|
copytree(roottpl, project_name, ignore=IGNORE)
|
2009-02-05 12:12:15 +00:00
|
|
|
|
|
|
|
moduletpl = os.path.join(PROJECT_TEMPLATES_PATH, 'module')
|
2009-02-16 12:50:00 +00:00
|
|
|
copytree(moduletpl, '%s/%s' % (project_name, project_name),
|
|
|
|
ignore=IGNORE)
|
2009-02-05 12:12:15 +00:00
|
|
|
|
2008-11-17 10:33:15 +00:00
|
|
|
for path in TEMPLATES:
|
2009-02-05 12:12:15 +00:00
|
|
|
tplfile = os.path.join(project_root_path,
|
|
|
|
string.Template(path).substitute(project_name=project_name))
|
|
|
|
render_templatefile(tplfile, project_name=project_name,
|
|
|
|
ProjectName=string_camelcase(project_name))
|
2008-10-05 07:37:21 +00:00
|
|
|
else:
|
|
|
|
print "scrapy-admin.py: missing project name"
|
|
|
|
else:
|
|
|
|
print "scrapy-admin.py: unknown command: %s" % cmd
|
2008-07-27 02:23:14 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2008-10-05 07:37:21 +00:00
|
|
|
main()
|