egencache: --preserve-comments allow missing file
authorZac Medico <zmedico@gentoo.org>
Sun, 13 Feb 2011 14:18:00 +0000 (06:18 -0800)
committerZac Medico <zmedico@gentoo.org>
Sun, 13 Feb 2011 14:18:00 +0000 (06:18 -0800)
This will fix bug #354687.

bin/egencache

index 89d2fdec476148fc11063f3aef5d390f68f03a5a..e5685c4dc88dcdc4a0251c8f20183cc22f2d59ac 100755 (executable)
@@ -396,11 +396,33 @@ class GenUseLocalDesc(object):
                                                mode='w', encoding=_encodings['repo.content'],
                                                errors='replace')
                        except IOError as e:
+                               if not self._preserve_comments or \
+                                       os.path.isfile(desc_path):
+                                       writemsg_level(
+                                               "ERROR: failed to open output file %s: %s\n" \
+                                               % (desc_path, e), level=logging.ERROR, noiselevel=-1)
+                                       self.returncode |= 2
+                                       return
+
+                               # Open in r+b mode failed because the file doesn't
+                               # exist yet. We can probably recover if we disable
+                               # preserve_comments mode now.
                                writemsg_level(
-                                       "ERROR: failed to open output file %s: %s\n" % (desc_path,e,),
-                                       level=logging.ERROR, noiselevel=-1)
-                               self.returncode |= 2
-                               return
+                                       "WARNING: --preserve-comments enabled, but " + \
+                                       "output file not found: %s\n" % (desc_path,),
+                                       level=logging.WARNING, noiselevel=-1)
+                               self._preserve_comments = False
+                               try:
+                                       output = codecs.open(_unicode_encode(desc_path,
+                                               encoding=_encodings['fs'], errors='strict'),
+                                               mode='w', encoding=_encodings['repo.content'],
+                                               errors='replace')
+                               except IOError as e:
+                                       writemsg_level(
+                                               "ERROR: failed to open output file %s: %s\n" \
+                                               % (desc_path, e), level=logging.ERROR, noiselevel=-1)
+                                       self.returncode |= 2
+                                       return
                else:
                        output = sys.stdout