# Avoid instantiating portage.settings when the desired
# variable is set in os.environ.
- elif k == '_portage_grpname':
+ elif k in ('_portage_grpname', '_portage_username'):
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':
- v = None
- if 'PORTAGE_USERNAME' in os.environ:
- v = os.environ['PORTAGE_USERNAME']
+ if k == '_portage_grpname':
+ env_key = 'PORTAGE_GRPNAME'
+ else:
+ env_key = 'PORTAGE_USERNAME'
+
+ if env_key in os.environ:
+ v = os.environ[env_key]
elif hasattr(portage, 'settings'):
- v = portage.settings.get('PORTAGE_USERNAME')
+ v = portage.settings.get(env_key)
+ elif portage.const.EPREFIX:
+ # For prefix environments, default to the UID and GID of
+ # the top-level EROOT directory. The config class has
+ # equivalent code, but we also need to do it here if
+ # _disable_legacy_globals() has been called.
+ eroot = os.path.join(os.environ.get('ROOT', os.sep),
+ portage.const.EPREFIX.lstrip(os.sep))
+ try:
+ eroot_st = os.stat(eroot)
+ except OSError:
+ pass
+ else:
+ if k == '_portage_grpname':
+ try:
+ grp_struct = grp.getgrgid(eroot_st.st_gid)
+ except KeyError:
+ pass
+ else:
+ v = grp_struct.gr_name
+ else:
+ try:
+ pwd_struct = pwd.getpwuid(eroot_st.st_uid)
+ except KeyError:
+ pass
+ else:
+ v = pwd_struct.pw_name
+
if v is None:
v = 'portage'
else: