Simplified copytree function

This commit is contained in:
Felipe Ruhland 2016-05-24 11:57:56 -03:00
parent 24a45cc6e6
commit fc9a45ee91
No known key found for this signature in database
GPG Key ID: 6708F893426891AF
1 changed files with 9 additions and 31 deletions

View File

@ -52,47 +52,25 @@ class Command(ScrapyCommand):
return True
return False
def _copytree(self, src, dst, symlinks=False, ignore=None):
def _copytree(self, src, dst):
ignore = IGNORE
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
ignored_names = ignore(src, names)
if not os.path.exists(dst):
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):
self._copytree(srcname, dstname, symlinks, ignore)
else:
# Will raise a SpecialFileError for unsupported file types
copy2(srcname, dstname)
# catch the Error from the recursive copytree so that we can
# continue with other files
except EnvironmentError as err:
errors.extend(err.args[0])
except EnvironmentError as why:
errors.append((srcname, dstname, str(why)))
try:
copystat(src, dst)
except OSError as why:
if WindowsError is not None and isinstance(why, WindowsError):
# Copying file access times may fail on Windows
pass
if os.path.isdir(srcname):
self._copytree(srcname, dstname)
else:
errors.append((src, dst, str(why)))
if errors:
raise EnvironmentError(errors)
copy2(srcname, dstname)
copystat(src, dst)
def run(self, args, opts):
if len(args) not in (1, 2):
@ -112,7 +90,7 @@ class Command(ScrapyCommand):
self.exitcode = 1
return
self._copytree(self.templates_dir, abspath(project_dir), ignore=IGNORE)
self._copytree(self.templates_dir, abspath(project_dir))
move(join(project_dir, 'module'), join(project_dir, project_name))
for paths in TEMPLATES_TO_RENDER:
path = join(*paths)