Skip to content

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

added copytree from python 2.6 to utils.misc and make startproject use it to ignore .svn and .pyc files

Ismael Carnales3e8bcafd8b64
diff --git a/scrapy/trunk/scrapy/bin/scrapy-admin.py b/scrapy/trunk/scrapy/bin/scrapy-admin.py
index a64228008..efbd61c0e 100755
--- a/scrapy/trunk/scrapy/bin/scrapy-admin.py
+++ b/scrapy/trunk/scrapy/bin/scrapy-admin.py
@@ -2,12 +2,12 @@
"""Scrapy admin script is used to create new scrapy projects and similar
tasks"""
import os
-import shutil
import string
from optparse import OptionParser
import scrapy
from scrapy.utils.misc import render_templatefile, string_camelcase
+from scrapy.utils.misc import ignore_patterns, copytree
usage = """
scrapy-admin.py [options] [command]
@@ -29,6 +29,8 @@ TEMPLATES = (
'${project_name}/pipelines.py',
)
+IGNORE = ignore_patterns('*.pyc', '.svn')
+
def main():
parser = OptionParser(usage=usage)
opts, args = parser.parse_args()
@@ -45,10 +47,11 @@ def main():
project_module_path = '%s/%s' % (project_name, project_name)
roottpl = os.path.join(PROJECT_TEMPLATES_PATH, 'root')
- shutil.copytree(roottpl, project_name)
+ copytree(roottpl, project_name, ignore=IGNORE)
moduletpl = os.path.join(PROJECT_TEMPLATES_PATH, 'module')
- shutil.copytree(moduletpl, '%s/%s' % (project_name, project_name))
+ copytree(moduletpl, '%s/%s' % (project_name, project_name),
+ ignore=IGNORE)
for path in TEMPLATES:
tplfile = os.path.join(project_root_path,
diff --git a/scrapy/trunk/scrapy/utils/misc.py b/scrapy/trunk/scrapy/utils/misc.py
index 8886f1afd..4abfc6674 100644
--- a/scrapy/trunk/scrapy/utils/misc.py
+++ b/scrapy/trunk/scrapy/utils/misc.py
@@ -174,3 +174,60 @@ def string_camelcase(string):
"""
return CAMELCASE_INVALID_CHARS.sub('', string.title())
+
+
+# shutil.copytree from Python 2.6 (for backwards compatibility)
+import fnmatch
+import os
+from shutil import copy2, copystat, WindowsError
+
+
+def ignore_patterns(*patterns):
+ def _ignore_patterns(path, names):
+ ignored_names = []
+ for pattern in patterns:
+ ignored_names.extend(fnmatch.filter(names, pattern))
+ return set(ignored_names)
+ return _ignore_patterns
+
+
+def copytree(src, dst, symlinks=False, ignore=None):
+ names = os.listdir(src)
+ if ignore is not None:
+ ignored_names = ignore(src, names)
+ else:
+ ignored_names = set()
+
+ os.makedirs(dst)
+ errors = []
+ for name in names:
+ if name in ignored_names:
+ continue
+ srcname = os.path.join(src, name)
+ dstname = os.path.join(dst, name)
+ try:
+ if symlinks and os.path.islink(srcname):
+ linkto = os.readlink(srcname)
+ os.symlink(linkto, dstname)
+ elif os.path.isdir(srcname):
+ copytree(srcname, dstname, symlinks, ignore)
+ else:
+ copy2(srcname, dstname)
+ # XXX What about devices, sockets etc.?
+ except (IOError, os.error), why:
+ errors.append((srcname, dstname, str(why)))
+ # catch the Error from the recursive copytree so that we can
+ # continue with other files
+ except Error, err:
+ errors.extend(err.args[0])
+ try:
+ copystat(src, dst)
+ except OSError, why:
+ if WindowsError is not None and isinstance(why, WindowsError):
+ # Copying file access times may fail on Windows
+ pass
+ else:
+ errors.extend((src, dst, str(why)))
+ if errors:
+ raise Error, errors
+