1
0
Fork 0
forked from github/pelican

Add a feature to copy files from src to dest.

Fixes #86
This commit is contained in:
Alexis Metaireau 2011-05-07 22:46:56 +01:00
commit 04da794b6b
6 changed files with 51 additions and 18 deletions

View file

@ -11,7 +11,7 @@ import random
from jinja2 import Environment, FileSystemLoader
from jinja2.exceptions import TemplateNotFound
from pelican.utils import copytree, get_relative_path, process_translations, open
from pelican.utils import copy, get_relative_path, process_translations, open
from pelican.contents import Article, Page, is_valid_content
from pelican.readers import read_file
from pelican.log import *
@ -298,9 +298,10 @@ class StaticGenerator(Generator):
def _copy_paths(self, paths, source, destination, output_path,
final_path=None):
"""Copy all the paths from source to destination"""
for path in paths:
copytree(path, source, os.path.join(output_path, destination),
final_path)
copy(path, source, os.path.join(output_path, destination), final_path,
overwrite=True)
def generate_output(self, writer):
self._copy_paths(self.settings['STATIC_PATHS'], self.path,
@ -308,6 +309,10 @@ class StaticGenerator(Generator):
self._copy_paths(self.settings['THEME_STATIC_PATHS'], self.theme,
'theme', self.output_path, '.')
# copy all the files needed
for source, destination in self.settings['FILES_TO_COPY']:
copy(source, self.path, self.output_path, destination, overwrite=True)
class PdfGenerator(Generator):
"""Generate PDFs on the output dir, for all articles and pages coming from

View file

@ -38,6 +38,7 @@ _DEFAULT_CONFIG = {'PATH': None,
'DEFAULT_PAGINATION': 5,
'DEFAULT_ORPHANS': 0,
'DEFAULT_METADATA': (),
'FILES_TO_COPY': (),
}
def read_settings(filename):

View file

@ -42,20 +42,38 @@ def slugify(value):
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
return re.sub('[-\s]+', '-', value)
def copytree(path, origin, destination, topath=None):
"""Copy path from origin to destination, silent any errors"""
if not topath:
topath = path
try:
fromp = os.path.expanduser(os.path.join(origin, path))
to = os.path.expanduser(os.path.join(destination, topath))
shutil.copytree(fromp, to)
info('copying %s to %s' % (fromp, to))
def copy(path, source, destination, destination_path=None, overwrite=False):
"""Copy path from origin to destination.
except OSError:
pass
The function is able to copy either files or directories.
:param path: the path to be copied from the source to the destination
:param source: the source dir
:param destination: the destination dir
:param destination_path: the destination path (optional)
:param overwrite: wether to overwrite the destination if already exists or not
"""
if not destination_path:
destination_path = path
source_ = os.path.abspath(os.path.expanduser(os.path.join(source, path)))
destination_ = os.path.abspath(
os.path.expanduser(os.path.join(destination, destination_path)))
if os.path.isdir(source_):
try:
shutil.copytree(source_, destination_)
info('copying %s to %s' % (source_, destination_))
except OSError:
if overwrite:
shutil.rmtree(destination_)
shutil.copytree(source_, destination_)
info('replacement of %s with %s' % (source_, destination_))
elif os.path.isfile(source_):
shutil.copy(source_, destination_)
info('copying %s to %s' % (source_, destination_))
def clean_output_dir(path):
"""Remove all the files from the output directory"""