Source code for utah.group
# Ubuntu Testing Automation Harness
# Copyright 2012 Canonical Ltd.
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Provide unix group checking functionality."""
import grp
import os
import sys
import getpass
from utah.config import config
[docs]def check_user_group(group=config.group):
"""Return whether the user is a member of the given group."""
user_gids = os.getgroups()
gid = grp.getgrnam(group).gr_gid
return gid in user_gids
[docs]def print_group_error_message(script):
"""Print error message to stderr to be used by scripts."""
argv = list(sys.argv)
argv[0] = os.path.abspath(script)
message = ["Error: you are not in the utah group.",
("If you believe you have properly configured "
"your user account for UTAH use, try:"),
' sudo usermod -a -G utah {}'.format(getpass.getuser()),
"Otherwise, please run this script as the utah user, i.e.:",
" sudo su - utah -c '{}'\n".format(' '.join(argv))]
message = '\n'.join(message)
sys.stderr.write(message)