1 |
Author: zmedico |
2 |
Date: 2008-03-01 00:10:37 +0000 (Sat, 01 Mar 2008) |
3 |
New Revision: 9401 |
4 |
|
5 |
Modified: |
6 |
main/branches/2.1.2/pym/portage.py |
7 |
main/branches/2.1.2/pym/portage_util.py |
8 |
Log: |
9 |
Make 'import portage' statements more tolerant to broken source statements |
10 |
in make.conf since exceptions thrown during 'import portage' statements |
11 |
can practically render the api unusable for api consumers. Thanks to lxnay |
12 |
for the suggestion. (trunk r9400) |
13 |
|
14 |
|
15 |
Modified: main/branches/2.1.2/pym/portage.py |
16 |
=================================================================== |
17 |
--- main/branches/2.1.2/pym/portage.py 2008-03-01 00:04:09 UTC (rev 9400) |
18 |
+++ main/branches/2.1.2/pym/portage.py 2008-03-01 00:10:37 UTC (rev 9401) |
19 |
@@ -1104,7 +1104,11 @@ |
20 |
@type local_config: Boolean |
21 |
""" |
22 |
|
23 |
- debug = os.environ.get("PORTAGE_DEBUG") == "1" |
24 |
+ # When initializing the global portage.settings instance, avoid |
25 |
+ # raising exceptions whenever possible since exceptions thrown |
26 |
+ # from 'import portage' or 'import portage.exceptions' statements |
27 |
+ # can practically render the api unusable for api consumers. |
28 |
+ tolerant = "_initializing_globals" in globals() |
29 |
|
30 |
self.already_in_regenerate = 0 |
31 |
|
32 |
@@ -1377,7 +1381,7 @@ |
33 |
|
34 |
self.mygcfg = getconfig( |
35 |
os.path.join(config_root, MAKE_CONF_FILE.lstrip(os.path.sep)), |
36 |
- allow_sourcing=True) |
37 |
+ tolerant=tolerant, allow_sourcing=True) |
38 |
if self.mygcfg is None: |
39 |
self.mygcfg = {} |
40 |
|
41 |
@@ -9997,7 +10001,10 @@ |
42 |
for k, envvar in (("config_root", "PORTAGE_CONFIGROOT"), ("target_root", "ROOT")): |
43 |
kwargs[k] = os.environ.get(envvar, "/") |
44 |
|
45 |
+ global _initializing_globals |
46 |
+ _initializing_globals = True |
47 |
db = create_trees(**kwargs) |
48 |
+ del _initializing_globals |
49 |
|
50 |
settings = db["/"]["vartree"].settings |
51 |
portdb = db["/"]["porttree"].dbapi |
52 |
|
53 |
Modified: main/branches/2.1.2/pym/portage_util.py |
54 |
=================================================================== |
55 |
--- main/branches/2.1.2/pym/portage_util.py 2008-03-01 00:04:09 UTC (rev 9400) |
56 |
+++ main/branches/2.1.2/pym/portage_util.py 2008-03-01 00:10:37 UTC (rev 9401) |
57 |
@@ -16,6 +16,11 @@ |
58 |
if not hasattr(__builtins__, "set"): |
59 |
from sets import Set as set |
60 |
|
61 |
+try: |
62 |
+ import cStringIO as StringIO |
63 |
+except ImportError: |
64 |
+ import StringIO |
65 |
+ |
66 |
noiselimit = 0 |
67 |
|
68 |
def writemsg(mystr,noiselevel=0,fd=None): |
69 |
@@ -291,6 +296,15 @@ |
70 |
return 0 |
71 |
return 1 |
72 |
|
73 |
+class _tolerant_shlex(shlex.shlex): |
74 |
+ def sourcehook(self, newfile): |
75 |
+ try: |
76 |
+ return shlex.shlex.sourcehook(self, newfile) |
77 |
+ except EnvironmentError, e: |
78 |
+ writemsg("!!! Parse error in '%s': source command failed: %s\n" % \ |
79 |
+ (self.infile, str(e)), noiselevel=-1) |
80 |
+ return (newfile, StringIO.StringIO()) |
81 |
+ |
82 |
def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): |
83 |
mykeys={} |
84 |
try: |
85 |
@@ -302,10 +316,14 @@ |
86 |
raise |
87 |
return None |
88 |
try: |
89 |
+ if tolerant: |
90 |
+ shlex_class = _tolerant_shlex |
91 |
+ else: |
92 |
+ shlex_class = shlex.shlex |
93 |
# The default shlex.sourcehook() implementation |
94 |
# only joins relative paths when the infile |
95 |
# attribute is properly set. |
96 |
- lex = shlex.shlex(f, infile=mycfg, posix=True) |
97 |
+ lex = shlex_class(f, infile=mycfg, posix=True) |
98 |
lex.wordchars=string.digits+string.letters+"~!@#$%*_\:;?,./-+{}" |
99 |
lex.quotes="\"'" |
100 |
if allow_sourcing: |
101 |
|
102 |
-- |
103 |
gentoo-commits@l.g.o mailing list |