Skip to content

Commit

Permalink
create output file with unique file name (fixes #84)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Oct 25, 2023
1 parent c93a6cd commit d8a2eb0
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions sphinxcontrib/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import shlex
import shutil
import subprocess
import tempfile
from contextlib import contextmanager

from docutils import nodes
Expand Down Expand Up @@ -362,13 +363,17 @@ def _render_files(self, keys, fileformat):
def render(self, node, fileformat):
key = hash_plantuml_node(node)
outdir = os.path.join(self.cache_dir, key[:2])
outfname = os.path.join(outdir, '%s.%s' % (key, fileformat))
basename = '%s.%s' % (key, fileformat)
outfname = os.path.join(outdir, basename)
if os.path.exists(outfname):
return outfname

ensuredir(outdir)
absincdir = os.path.join(self.builder.srcdir, node['incdir'])
with open(outfname + '.new', 'wb') as f:
# TODO: delete_on_close can be used on Python 3.12+
with tempfile.NamedTemporaryFile(
prefix=basename + '.new', dir=outdir, delete=False
) as f:
try:
p = subprocess.Popen(
generate_plantuml_args(self, node, fileformat),
Expand All @@ -394,7 +399,12 @@ def render(self, node, fileformat):
else:
raise PlantUmlError('error while running plantuml\n\n%s' % serr)

rename(outfname + '.new', outfname)
# inherit dir mode since temp file isn't world-readable by default.
if os.name == 'posix':
os.fchmod(f.fileno(), os.lstat(outdir).st_mode & 0o666)
f.close()
rename(f.name, outfname)

return outfname


Expand Down

0 comments on commit d8a2eb0

Please sign in to comment.