[web] Fix bug with usage of os.rename under windows environment
In 7c386161ebd6 we removed cache handling from property sheet and introduced
usage of a tempfile + os.rename to get atomic generation of files. The pb is
that this is not portable, since under windows os.rename will raise an exception
if the file already exists (because there is no way to write a file atomatically
in such case).
This kind of thing should be out of the CW scope anyway, so implements a quick &
dirty fix in the mean time.
Closes #14214794
--- a/web/propertysheet.py Wed Jul 20 09:40:04 2016 +0200
+++ b/web/propertysheet.py Tue Jul 19 15:53:58 2016 +0200
@@ -1,4 +1,4 @@
-# copyright 2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2010-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
@@ -29,10 +29,12 @@
('STYLESHEETS_IE', list), ('STYLESHEETS_PRINT', list),
]
+
class lazystr(object):
def __init__(self, string, context):
self.string = string
self.context = context
+
def __str__(self):
return self.string % self.context
@@ -105,7 +107,12 @@
tmpfd, tmpfile = tempfile.mkstemp(dir=rcachedir, prefix=osp.basename(cachefile))
with os.fdopen(tmpfd, 'w') as stream:
stream.write(content)
- os.rename(tmpfile, cachefile)
+ try:
+ os.rename(tmpfile, cachefile)
+ except IOError:
+ # Under windows, os.rename won't overwrite an existing file
+ os.unlink(cachefile)
+ os.rename(tmpfile, cachefile)
adirectory = self._cache_directory
return adirectory