[web] Fix bug with usage of os.rename under windows environment 3.22
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 19 Jul 2016 15:53:58 +0200
branch3.22
changeset 11434 f6ba947c11ee
parent 11431 bc473cddba5e
child 11435 42578bc0b5ef
[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
web/propertysheet.py
--- 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