Skip to content

Commit

Permalink
ref: cleanup temporary directories
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Apr 17, 2024
1 parent 617c053 commit d5ed1aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.9.1
- ref: do not use tempfile.TemporaryDirectory
- ref: create /tmp/matplotlib before importing matplotlib
0.9.0
- ref: don't create any preview files locally, upload directly to S3
- ref: only serve preview images from S3
Expand Down
16 changes: 11 additions & 5 deletions ckanext/dc_view/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from dcor_shared import DC_MIME_TYPES, s3cc, get_dc_instance, wait_for_resource
import numpy as np

# Create a temporary matplotlib config directory which is removed on exit
mpldir = tempfile.mkdtemp(prefix="ckan_dcor_dc_view_")
atexit.register(shutil.rmtree, mpldir)
# Create a matplotlib config directory, so we can import and use matplotlib
mpldir = "/tmp/matplotlib"
pathlib.Path(mpldir).mkdir(exist_ok=True)
os.environ['MPLCONFIGDIR'] = mpldir

from matplotlib.gridspec import GridSpec # noqa: E402
Expand All @@ -36,7 +36,8 @@ def create_preview_job(resource, override=False):
or not s3cc.artifact_exists(resource_id=rid,
artifact="preview"))):
# Create the preview in a temporary location
with tempfile.TemporaryDirectory() as ttd_name:
ttd_name = tempfile.mkdtemp(prefix="ckanext-dc_view_")
try:
path_preview = pathlib.Path(ttd_name) / "preview.jpg"
with get_dc_instance(rid) as ds:
fig = overview_plot(rtdc_ds=ds)
Expand All @@ -47,7 +48,12 @@ def create_preview_job(resource, override=False):
path_artifact=path_preview,
artifact="preview",
override=True)
return True
except BaseException:
pass
else:
return True
finally:
shutil.rmtree(ttd_name, ignore_errors=True)
return False


Expand Down

0 comments on commit d5ed1aa

Please sign in to comment.