moved copytree function to scrapy.utils.python (more appropiate location) and fixed minor bug ('Error' not defined). also removed unused var 'project_module_path'

--HG--
extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40861
This commit is contained in:
Pablo Hoffman 2009-02-16 23:43:43 +00:00
parent e6458d057b
commit 489e86bd16
3 changed files with 70 additions and 64 deletions

View File

@ -7,7 +7,7 @@ from optparse import OptionParser
import scrapy
from scrapy.utils.misc import render_templatefile, string_camelcase
from scrapy.utils.misc import ignore_patterns, copytree
from scrapy.utils.python import ignore_patterns, copytree
usage = """
scrapy-admin.py [options] [command]
@ -44,7 +44,6 @@ def main():
if len(args) >= 2:
project_name = args[1]
project_root_path = project_name
project_module_path = '%s/%s' % (project_name, project_name)
roottpl = os.path.join(PROJECT_TEMPLATES_PATH, 'root')
copytree(roottpl, project_name, ignore=IGNORE)

View File

@ -175,65 +175,3 @@ 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
try:
WindowsError
except NameError:
WindowsError = None
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

View File

@ -1,7 +1,14 @@
"""
This module contains essential stuff that should've come with Python itself ;)
It also contains functions (or functionality) which is in Python versions
higher than 2.5 which is the lower version support by Scrapy.
"""
import re
import fnmatch
import os
from shutil import copy2, copystat
from sgmllib import SGMLParser
class FixedSGMLParser(SGMLParser):
@ -165,3 +172,65 @@ def isbinarytext(text):
"""
assert isinstance(text, str), "text must be str, got '%s'" % type(text).__name__
return any(c in _BINARYCHARS for c in text)
# ----- shutil.copytree function from Python 2.6 adds ignore argument ---- #
try:
WindowsError
except NameError:
WindowsError = None
class Error(EnvironmentError):
pass
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
# ----- end of shutil.copytree function from Python 2.6 ---- #