From 677d096393bca948eea8fad252bd859ed8142309 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 13 Jan 2009 23:55:24 +0100 Subject: [PATCH] On Windows, the target of os.rename() may not exist. --- sphinx/builder.py | 5 +++-- sphinx/environment.py | 4 ++-- sphinx/util/__init__.py | 10 ++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/sphinx/builder.py b/sphinx/builder.py index 33322d5818..7b45f973ea 100644 --- a/sphinx/builder.py +++ b/sphinx/builder.py @@ -26,7 +26,8 @@ from docutils.frontend import OptionParser from docutils.readers.doctree import Reader as DoctreeReader from sphinx import addnodes, locale, __version__ -from sphinx.util import ensuredir, relative_uri, SEP, os_path, texescape, ustrftime +from sphinx.util import movefile, ensuredir, relative_uri, SEP, os_path, texescape, \ + ustrftime from sphinx.htmlhelp import build_hhx from sphinx.htmlwriter import HTMLWriter, HTMLTranslator, SmartyPantsHTMLTranslator from sphinx.textwriter import TextWriter @@ -783,7 +784,7 @@ class StandaloneHTMLBuilder(Builder): self.indexer.dump(f, self.indexer_format) finally: f.close() - os.rename(searchindexfn + '.tmp', searchindexfn) + movefile(searchindexfn + '.tmp', searchindexfn) self.info('done') self.info(bold('dumping object inventory... '), nonl=True) diff --git a/sphinx/environment.py b/sphinx/environment.py index ab6b1f0bb7..a0270d33bd 100644 --- a/sphinx/environment.py +++ b/sphinx/environment.py @@ -42,7 +42,7 @@ from docutils.transforms import Transform from docutils.transforms.parts import ContentsFilter from sphinx import addnodes -from sphinx.util import get_matching_docs, SEP, ustrftime +from sphinx.util import movefile, get_matching_docs, SEP, ustrftime from sphinx.directives import additional_xref_types default_settings = { @@ -217,7 +217,7 @@ class BuildEnvironment: pickle.dump(self, picklefile, pickle.HIGHEST_PROTOCOL) finally: picklefile.close() - os.rename(filename + '.tmp', filename) + movefile(filename + '.tmp', filename) # reset attributes self.config.values = values self.set_warnfunc(warnfunc) diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py index e427310665..3e315e612c 100644 --- a/sphinx/util/__init__.py +++ b/sphinx/util/__init__.py @@ -282,3 +282,13 @@ def nested_parse_with_titles(state, content, node): def ustrftime(format, *args): # strftime for unicode strings return time.strftime(unicode(format).encode('utf-8'), *args).decode('utf-8') + + +def movefile(source, dest): + # move a file, removing the destination if it exists + if os.path.exists(dest): + try: + os.unlink(dest) + except OSError: + pass + os.rename(source, dest)