summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2008-07-24 11:40:04 +0000
committerZac Medico <zmedico@gentoo.org>2008-07-24 11:40:04 +0000
commit3b91a546c90f20a6099f211f198584003a34ae7b (patch)
tree5ba67e2cadb4df8f1b6985e90afe26e52cf25212
parenteae644d55d854520281720532b41317ced5a6af2 (diff)
downloadportage-3b91a546c90f20a6099f211f198584003a34ae7b.tar.gz
portage-3b91a546c90f20a6099f211f198584003a34ae7b.tar.bz2
portage-3b91a546c90f20a6099f211f198584003a34ae7b.zip
Bug #228117 - Insert a trailing newline in the getconfig() input file as
necessary to avoid a silent error in shlex that is triggered by a source statement at the end of the file without a trailing newline after the source statement. To solve this problem, derive a special file class that inserts a newline just before eof when necessary. svn path=/main/trunk/; revision=11181
-rw-r--r--pym/portage/util.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/pym/portage/util.py b/pym/portage/util.py
index 9ad93b5c3..d7aa7e949 100644
--- a/pym/portage/util.py
+++ b/pym/portage/util.py
@@ -339,6 +339,44 @@ class _tolerant_shlex(shlex.shlex):
(self.infile, str(e)), noiselevel=-1)
return (newfile, StringIO.StringIO())
+class _insert_newline_eof(file):
+ """
+ Read functions insert anywhere from 0 and 2 newlines just before eof.
+ This is useful as a workaround for avoiding a silent error in shlex that
+ is triggered by a source statement at the end of the file without a
+ trailing newline after the source statement.
+ """
+
+ def read(self, *args):
+ if hasattr(self, "_got_eof"):
+ return ""
+ rval = file.read(self, *args)
+ if rval and not args and rval[-1:] != "\n":
+ rval += "\n"
+ if not rval:
+ self._got_eof = True
+ return "\n"
+ return rval
+
+ def readline(self, *args):
+ if hasattr(self, "_got_eof"):
+ return ""
+ rval = file.readline(self, *args)
+ if rval and rval[-1:] != "\n":
+ rval += "\n"
+ if not rval:
+ self._got_eof = True
+ rval = "\n"
+ return rval
+
+ def readlines(self, *args):
+ if hasattr(self, "_got_eof"):
+ return []
+ lines = file.readlines(self, *args)
+ if lines and lines[-1][-1:] != "\n":
+ lines[-1] += "\n"
+ return lines
+
def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True):
if isinstance(expand, dict):
# Some existing variable definitions have been
@@ -349,7 +387,7 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True):
expand_map = {}
mykeys = {}
try:
- f=open(mycfg,'r')
+ f = _insert_newline_eof(mycfg, 'rb')
except IOError, e:
if e.errno == PermissionDenied.errno:
raise PermissionDenied(mycfg)