Skip to content

Commit

Permalink
On Windows, the target of os.rename() may not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
birkenfeld committed Jan 13, 2009
1 parent 0da1788 commit 677d096
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 3 additions & 2 deletions sphinx/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
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
Expand Down Expand Up @@ -783,7 +784,7 @@ def handle_finish(self):
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)
Expand Down
4 changes: 2 additions & 2 deletions sphinx/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
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 = {
Expand Down Expand Up @@ -217,7 +217,7 @@ def topickle(self, filename):
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)
Expand Down
10 changes: 10 additions & 0 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 677d096

Please sign in to comment.