mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Check URL overwrite. Fixes #446.
Check that template-generated files don't overwrite each other. Log a critical message otherwise, and exit with non-zero status.
This commit is contained in:
parent
73ca6a1316
commit
ff7410ce2a
1 changed files with 13 additions and 2 deletions
|
|
@ -21,6 +21,7 @@ class Writer(object):
|
|||
self.output_path = output_path
|
||||
self.reminder = dict()
|
||||
self.settings = settings or {}
|
||||
self._written_files = set()
|
||||
|
||||
def _create_new_feed(self, feed_type, context):
|
||||
feed_class = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed
|
||||
|
|
@ -46,6 +47,16 @@ class Writer(object):
|
|||
pubdate=set_date_tzinfo(item.date,
|
||||
self.settings.get('TIMEZONE', None)))
|
||||
|
||||
def _open_w(self, filename, encoding):
|
||||
"""Open a file to write some content to it.
|
||||
|
||||
Exit if we have already written to that file.
|
||||
"""
|
||||
if filename in self._written_files:
|
||||
raise IOError('File %s is to be overwritten' % filename)
|
||||
self._written_files.add(filename)
|
||||
return open(filename, 'w', encoding=encoding)
|
||||
|
||||
def write_feed(self, elements, context, path=None, feed_type='atom'):
|
||||
"""Generate a feed with the list of articles provided
|
||||
|
||||
|
|
@ -82,7 +93,7 @@ class Writer(object):
|
|||
pass
|
||||
|
||||
encoding = 'utf-8' if six.PY3 else None
|
||||
with open(complete_path, 'w', encoding=encoding) as fp:
|
||||
with self._open_w(complete_path, encoding) as fp:
|
||||
feed.write(fp, 'utf-8')
|
||||
logger.info('writing %s' % complete_path)
|
||||
return feed
|
||||
|
|
@ -121,7 +132,7 @@ class Writer(object):
|
|||
os.makedirs(os.path.dirname(path))
|
||||
except Exception:
|
||||
pass
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
with self._open_w(path, 'utf-8') as f:
|
||||
f.write(output)
|
||||
logger.info('writing {}'.format(path))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue