mirror of
https://github.com/scrapy/scrapy.git
synced 2025-02-26 08:03:54 +00:00
Merge pull request #1575 from palego/startproject-templates-override
[MRG+1] Startproject templates override
This commit is contained in:
commit
3881eaff45
@ -1046,7 +1046,12 @@ TEMPLATES_DIR
|
|||||||
Default: ``templates`` dir inside scrapy module
|
Default: ``templates`` dir inside scrapy module
|
||||||
|
|
||||||
The directory where to look for templates when creating new projects with
|
The directory where to look for templates when creating new projects with
|
||||||
:command:`startproject` command.
|
:command:`startproject` command and new spiders with :command:`genspider`
|
||||||
|
command.
|
||||||
|
|
||||||
|
The project name must not conflict with the name of custom files or directories
|
||||||
|
in the ``project`` subdirectory.
|
||||||
|
|
||||||
|
|
||||||
.. setting:: URLLENGTH_LIMIT
|
.. setting:: URLLENGTH_LIMIT
|
||||||
|
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import re
|
import re
|
||||||
import shutil
|
|
||||||
import string
|
import string
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from os.path import join, exists, abspath
|
from os.path import join, exists, abspath
|
||||||
from shutil import copytree, ignore_patterns
|
from shutil import copytree, ignore_patterns, move
|
||||||
|
|
||||||
import scrapy
|
import scrapy
|
||||||
from scrapy.commands import ScrapyCommand
|
from scrapy.commands import ScrapyCommand
|
||||||
@ -12,8 +11,6 @@ from scrapy.utils.template import render_templatefile, string_camelcase
|
|||||||
from scrapy.exceptions import UsageError
|
from scrapy.exceptions import UsageError
|
||||||
|
|
||||||
|
|
||||||
TEMPLATES_PATH = join(scrapy.__path__[0], 'templates', 'project')
|
|
||||||
|
|
||||||
TEMPLATES_TO_RENDER = (
|
TEMPLATES_TO_RENDER = (
|
||||||
('scrapy.cfg',),
|
('scrapy.cfg',),
|
||||||
('${project_name}', 'settings.py.tmpl'),
|
('${project_name}', 'settings.py.tmpl'),
|
||||||
@ -63,17 +60,24 @@ class Command(ScrapyCommand):
|
|||||||
self.exitcode = 1
|
self.exitcode = 1
|
||||||
return
|
return
|
||||||
|
|
||||||
moduletpl = join(TEMPLATES_PATH, 'module')
|
copytree(self.templates_dir, project_name, ignore=IGNORE)
|
||||||
copytree(moduletpl, join(project_name, project_name), ignore=IGNORE)
|
move(join(project_name, 'module'), join(project_name, project_name))
|
||||||
shutil.copy(join(TEMPLATES_PATH, 'scrapy.cfg'), project_name)
|
|
||||||
for paths in TEMPLATES_TO_RENDER:
|
for paths in TEMPLATES_TO_RENDER:
|
||||||
path = join(*paths)
|
path = join(*paths)
|
||||||
tplfile = join(project_name,
|
tplfile = join(project_name,
|
||||||
string.Template(path).substitute(project_name=project_name))
|
string.Template(path).substitute(project_name=project_name))
|
||||||
render_templatefile(tplfile, project_name=project_name,
|
render_templatefile(tplfile, project_name=project_name,
|
||||||
ProjectName=string_camelcase(project_name))
|
ProjectName=string_camelcase(project_name))
|
||||||
print("New Scrapy project %r created in:" % project_name)
|
print("New Scrapy project %r, using template directory %r, created in:" % \
|
||||||
|
(project_name, self.templates_dir))
|
||||||
print(" %s\n" % abspath(project_name))
|
print(" %s\n" % abspath(project_name))
|
||||||
print("You can start your first spider with:")
|
print("You can start your first spider with:")
|
||||||
print(" cd %s" % project_name)
|
print(" cd %s" % project_name)
|
||||||
print(" scrapy genspider example example.com")
|
print(" scrapy genspider example example.com")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def templates_dir(self):
|
||||||
|
_templates_base_dir = self.settings['TEMPLATES_DIR'] or \
|
||||||
|
join(scrapy.__path__[0], 'templates')
|
||||||
|
return join(_templates_base_dir, 'project')
|
||||||
|
|
@ -4,13 +4,14 @@ import subprocess
|
|||||||
import tempfile
|
import tempfile
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from os.path import exists, join, abspath
|
from os.path import exists, join, abspath
|
||||||
from shutil import rmtree
|
from shutil import rmtree, copytree
|
||||||
from tempfile import mkdtemp
|
from tempfile import mkdtemp
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from twisted.trial import unittest
|
from twisted.trial import unittest
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
|
import scrapy
|
||||||
from scrapy.utils.python import to_native_str
|
from scrapy.utils.python import to_native_str
|
||||||
from scrapy.utils.python import retry_on_eintr
|
from scrapy.utils.python import retry_on_eintr
|
||||||
from scrapy.utils.test import get_testenv
|
from scrapy.utils.test import get_testenv
|
||||||
@ -73,6 +74,26 @@ class StartprojectTest(ProjectTest):
|
|||||||
self.assertEqual(1, self.call('startproject', 'sys'))
|
self.assertEqual(1, self.call('startproject', 'sys'))
|
||||||
|
|
||||||
|
|
||||||
|
class StartprojectTemplatesTest(ProjectTest):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(StartprojectTemplatesTest, self).setUp()
|
||||||
|
self.tmpl = join(self.temp_path, 'templates')
|
||||||
|
self.tmpl_proj = join(self.tmpl, 'project')
|
||||||
|
|
||||||
|
def test_startproject_template_override(self):
|
||||||
|
copytree(join(scrapy.__path__[0], 'templates'), self.tmpl)
|
||||||
|
os.mknod(join(self.tmpl_proj, 'root_template'))
|
||||||
|
assert exists(join(self.tmpl_proj, 'root_template'))
|
||||||
|
|
||||||
|
args = ['--set', 'TEMPLATES_DIR=%s' % self.tmpl]
|
||||||
|
p = self.proc('startproject', self.project_name, *args)
|
||||||
|
out = to_native_str(retry_on_eintr(p.stdout.read))
|
||||||
|
self.assertIn("New Scrapy project %r, using template directory %r, created in:" % \
|
||||||
|
(self.project_name, join(self.tmpl, 'project')), out)
|
||||||
|
assert exists(join(self.proj_path, 'root_template'))
|
||||||
|
|
||||||
|
|
||||||
class CommandTest(ProjectTest):
|
class CommandTest(ProjectTest):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user