data.py: avoid portage.settings when possible
authorZac Medico <zmedico@gentoo.org>
Sat, 10 Dec 2011 23:49:21 +0000 (15:49 -0800)
committerZac Medico <zmedico@gentoo.org>
Sat, 10 Dec 2011 23:49:21 +0000 (15:49 -0800)
pym/portage/data.py

index f17fd7326f7f4db2c4db469fc203c093bdf0d7a2..a25b282ab715b55d64c0cbda8b81b82e19362e16 100644 (file)
@@ -134,12 +134,24 @@ def _get_global(k):
                                                pass
                                v = sorted(set(v))
 
+       # Avoid instantiating portage.settings when the desired
+       # variable is set in os.environ.
        elif k == '_portage_grpname':
-               env = getattr(portage, 'settings', os.environ)
-               v = env.get('PORTAGE_GRPNAME', 'portage')
+               v = None
+               if 'PORTAGE_GRPNAME' in os.environ:
+                       v = os.environ['PORTAGE_GRPNAME']
+               elif hasattr(portage, 'settings'):
+                       v = portage.settings.get('PORTAGE_GRPNAME')
+               if v is None:
+                       v = 'portage'
        elif k == '_portage_username':
-               env = getattr(portage, 'settings', os.environ)
-               v = env.get('PORTAGE_USERNAME', 'portage')
+               v = None
+               if 'PORTAGE_USERNAME' in os.environ:
+                       v = os.environ['PORTAGE_USERNAME']
+               elif hasattr(portage, 'settings'):
+                       v = portage.settings.get('PORTAGE_USERNAME')
+               if v is None:
+                       v = 'portage'
        else:
                raise AssertionError('unknown name: %s' % k)