Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create temporary directory in a more safe manner. #88

Merged
merged 3 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,15 @@ for developing your own pandocfilters.
package is available. Also assumes that ImageMagick's convert is in
the path. Images are put in the ``tikz-images`` directory.

Cleanup after run
-----------------

By default most filters create a directory ``...-images`` to save temporary
files. This directory doesn't get removed as it can be used as a cache so that
later pandoc runs don't have to recreate files if they already exist. The
directory is generated in the current directory.

If you prefer to have a clean directory after the run of the pandoc filter, you
can set an environment variable ``PANDOCFILTER_CLEANUP`` to the value ``true``
which forces the code to create a real temporary directory that will be removed
after the filter has finished.
30 changes: 23 additions & 7 deletions pandocfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import json
import os
import sys
import atexit
import shutil
import tempfile


# some utility-functions: make it easier to create your own filters
Expand All @@ -22,18 +25,31 @@ def get_filename4code(module, content, ext=None):
"""Generate filename based on content

The function ensures that the (temporary) directory exists, so that the
file can be written.
file can be written. In the usual case the directory doesn't get removed
afterwards, so a plugin can use the directory for caching purposes and
decide not to recreate an already existing file.

In case the user preferres the files to be actual temporary files, he can
set an environment variable `PANDOCFILTER_CLEANUP` to the value `true` to
make sure the directory is created in a temporary location and removed
after finishing the filter. In this case no caching whatsoever can be
done.

Example:
filename = get_filename4code("myfilter", code)
"""
imagedir = module + "-images"
if os.getenv('PANDOCFILTER_CLEANUP') == 'true':
imagedir = tempfile.mkdtemp(prefix=module)
atexit.register(lambda: shutil.rmtree(imagedir))
else:
imagedir = module + "-images"
try:
os.mkdir(imagedir)
sys.stderr.write('Created directory "' + imagedir + '"\n')
except OSError:
sys.stderr.write('Could not create directory "' + imagedir + '"\n')
raise
fn = hashlib.sha1(content.encode(sys.getfilesystemencoding())).hexdigest()
try:
os.mkdir(imagedir)
sys.stderr.write('Created directory ' + imagedir + '\n')
except OSError:
pass
if ext:
fn += "." + ext
return os.path.join(imagedir, fn)
Expand Down