mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Give utils.copy the ability to ignore files (issue 1692)
This requires a significant overhaul because we want to be able to have IGNORE_FILES apply at every level of a recursively copied directory (e.g. the theme static directory). Since I was overhauling it anyway I changed it to use os.walk, which should be more efficient.
This commit is contained in:
parent
940eb76b7f
commit
0a48371985
1 changed files with 51 additions and 29 deletions
|
|
@ -273,51 +273,73 @@ def slugify(value, substitutions=()):
|
||||||
return value.decode('ascii')
|
return value.decode('ascii')
|
||||||
|
|
||||||
|
|
||||||
def copy(source, destination):
|
def copy(source, destination, ignores=None):
|
||||||
"""Recursively copy source into destination.
|
"""Recursively copy source into destination.
|
||||||
|
|
||||||
If source is a file, destination has to be a file as well.
|
If source is a file, destination has to be a file as well.
|
||||||
|
|
||||||
The function is able to copy either files or directories.
|
The function is able to copy either files or directories.
|
||||||
|
|
||||||
:param source: the source file or directory
|
:param source: the source file or directory
|
||||||
:param destination: the destination file or directory
|
:param destination: the destination file or directory
|
||||||
|
:param ignores: either None, or a list of glob patterns;
|
||||||
|
files matching those patterns will _not_ be copied.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def walk_error(err):
|
||||||
|
logger.warning("While copying %s: %s: %s",
|
||||||
|
source_, err.filename, err.strerror)
|
||||||
|
|
||||||
source_ = os.path.abspath(os.path.expanduser(source))
|
source_ = os.path.abspath(os.path.expanduser(source))
|
||||||
destination_ = os.path.abspath(os.path.expanduser(destination))
|
destination_ = os.path.abspath(os.path.expanduser(destination))
|
||||||
|
|
||||||
if not os.path.exists(destination_) and not os.path.isfile(source_):
|
if ignores is None:
|
||||||
os.makedirs(destination_)
|
ignores = []
|
||||||
|
|
||||||
def recurse(source, destination):
|
if any(fnmatch.fnmatch(os.path.basename(source), ignore)
|
||||||
for entry in os.listdir(source):
|
for ignore in ignores):
|
||||||
entry_path = os.path.join(source, entry)
|
logger.info('Not copying %s due to ignores', source_)
|
||||||
if os.path.isdir(entry_path):
|
return
|
||||||
entry_dest = os.path.join(destination, entry)
|
|
||||||
if os.path.exists(entry_dest):
|
|
||||||
if not os.path.isdir(entry_dest):
|
|
||||||
raise IOError('Failed to copy {0} a directory.'
|
|
||||||
.format(entry_dest))
|
|
||||||
recurse(entry_path, entry_dest)
|
|
||||||
else:
|
|
||||||
shutil.copytree(entry_path, entry_dest)
|
|
||||||
else:
|
|
||||||
shutil.copy2(entry_path, destination)
|
|
||||||
|
|
||||||
|
if os.path.isfile(source_):
|
||||||
if os.path.isdir(source_):
|
dst_dir = os.path.dirname(destination_)
|
||||||
recurse(source_, destination_)
|
if not os.path.exists(dst_dir):
|
||||||
|
logger.info('Creating directory %s', dst_dir)
|
||||||
elif os.path.isfile(source_):
|
os.makedirs(dst_dir)
|
||||||
dest_dir = os.path.dirname(destination_)
|
|
||||||
if not os.path.exists(dest_dir):
|
|
||||||
os.makedirs(dest_dir)
|
|
||||||
shutil.copy2(source_, destination_)
|
|
||||||
logger.info('Copying %s to %s', source_, destination_)
|
logger.info('Copying %s to %s', source_, destination_)
|
||||||
else:
|
shutil.copy2(source_, destination_)
|
||||||
logger.warning('Skipped copy %s to %s', source_, destination_)
|
|
||||||
|
|
||||||
|
elif os.path.isdir(source_):
|
||||||
|
if not os.path.exists(destination_):
|
||||||
|
logger.info('Creating directory %s', destination_)
|
||||||
|
os.makedirs(destination_)
|
||||||
|
if not os.path.isdir(destination_):
|
||||||
|
logger.warning('Cannot copy %s (a directory) to %s (a file)',
|
||||||
|
source_, destination_)
|
||||||
|
return
|
||||||
|
|
||||||
|
for src_dir, subdirs, others in os.walk(source_):
|
||||||
|
dst_dir = os.path.join(destination_,
|
||||||
|
os.path.relpath(src_dir, source_))
|
||||||
|
|
||||||
|
subdirs[:] = (s for s in subdirs if not any(fnmatch.fnmatch(s, i)
|
||||||
|
for i in ignores))
|
||||||
|
others[:] = (o for o in others if not any(fnmatch.fnmatch(o, i)
|
||||||
|
for i in ignores))
|
||||||
|
|
||||||
|
if not os.path.isdir(dst_dir):
|
||||||
|
logger.info('Creating directory %s', dst_dir)
|
||||||
|
# Parent directories are known to exist, so 'mkdir' suffices.
|
||||||
|
os.mkdir(dst_dir)
|
||||||
|
|
||||||
|
for o in others:
|
||||||
|
src_path = os.path.join(src_dir, o)
|
||||||
|
dst_path = os.path.join(dst_dir, o)
|
||||||
|
if os.path.isfile(src_path):
|
||||||
|
logger.info('Copying %s to %s', src_path, dst_path)
|
||||||
|
shutil.copy2(src_path, dst_path)
|
||||||
|
else:
|
||||||
|
logger.warning('Skipped copy %s (not a file or directory) to %s',
|
||||||
|
src_path, dst_path)
|
||||||
|
|
||||||
def clean_output_dir(path, retention):
|
def clean_output_dir(path, retention):
|
||||||
"""Remove all files from output directory except those in retention list"""
|
"""Remove all files from output directory except those in retention list"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue