Python pwd.struct_passwd() Examples

The following are 11 code examples of pwd.struct_passwd(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pwd , or try the search function .
Example #1
Source File: test_util.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_check_environ_getpwuid(self):
        util._environ_checked = 0
        os.environ.pop('HOME', None)

        import pwd

        # only set pw_dir field, other fields are not used
        def mock_getpwuid(uid):
            return pwd.struct_passwd((None, None, None, None, None,
                                      '/home/distutils', None))

        with swap_attr(pwd, 'getpwuid', mock_getpwuid):
            check_environ()
            self.assertEqual(os.environ['HOME'], '/home/distutils')

        util._environ_checked = 0
        os.environ.pop('HOME', None)

        # bpo-10496: Catch pwd.getpwuid() error
        def getpwuid_err(uid):
            raise KeyError
        with swap_attr(pwd, 'getpwuid', getpwuid_err):
            check_environ()
            self.assertNotIn('HOME', os.environ) 
Example #2
Source File: test_util.py    From setuptools with MIT License 6 votes vote down vote up
def test_check_environ_getpwuid(self):
        util._environ_checked = 0
        os.environ.pop('HOME', None)

        import pwd

        # only set pw_dir field, other fields are not used
        result = pwd.struct_passwd((None, None, None, None, None,
                                    '/home/distutils', None))
        with mock.patch.object(pwd, 'getpwuid', return_value=result):
            check_environ()
            self.assertEqual(os.environ['HOME'], '/home/distutils')

        util._environ_checked = 0
        os.environ.pop('HOME', None)

        # bpo-10496: Catch pwd.getpwuid() error
        with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError):
            check_environ()
            self.assertNotIn('HOME', os.environ) 
Example #3
Source File: test_util.py    From android_universal with MIT License 6 votes vote down vote up
def test_check_environ_getpwuid(self):
        util._environ_checked = 0
        os.environ.pop('HOME', None)

        import pwd

        # only set pw_dir field, other fields are not used
        result = pwd.struct_passwd((None, None, None, None, None,
                                    '/home/distutils', None))
        with mock.patch.object(pwd, 'getpwuid', return_value=result):
            check_environ()
            self.assertEqual(os.environ['HOME'], '/home/distutils')

        util._environ_checked = 0
        os.environ.pop('HOME', None)

        # bpo-10496: Catch pwd.getpwuid() error
        with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError):
            check_environ()
            self.assertNotIn('HOME', os.environ) 
Example #4
Source File: test_fakepwd.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_addUser(self):
        """
        L{UserDatabase.addUser} accepts seven arguments, one for each field of
        a L{pwd.struct_passwd}, and makes the new record available via
        L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
        L{UserDatabase.getpwall}.
        """
        username = 'alice'
        password = 'secr3t'
        uid = 123
        gid = 456
        gecos = 'Alice,,,'
        home = '/users/alice'
        shell = '/usr/bin/foosh'

        db = self.database
        db.addUser(username, password, uid, gid, gecos, home, shell)

        for [entry] in [[db.getpwuid(uid)], [db.getpwnam(username)],
                        db.getpwall()]:
            self.assertEqual(entry.pw_name, username)
            self.assertEqual(entry.pw_passwd, password)
            self.assertEqual(entry.pw_uid, uid)
            self.assertEqual(entry.pw_gid, gid)
            self.assertEqual(entry.pw_gecos, gecos)
            self.assertEqual(entry.pw_dir, home)
            self.assertEqual(entry.pw_shell, shell) 
Example #5
Source File: test_fakepwd.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_addUser(self):
        """
        L{UserDatabase.addUser} accepts seven arguments, one for each field of
        a L{pwd.struct_passwd}, and makes the new record available via
        L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
        L{UserDatabase.getpwall}.
        """
        username = 'alice'
        password = 'secr3t'
        lastChange = 17
        min = 42
        max = 105
        warn = 12
        inact = 3
        expire = 400
        flag = 3

        db = self.database
        db.addUser(username, password, lastChange, min, max, warn, inact,
                   expire, flag)

        for [entry] in [[db.getspnam(username)], db.getspall()]:
            self.assertEqual(entry.sp_nam, username)
            self.assertEqual(entry.sp_pwd, password)
            self.assertEqual(entry.sp_lstchg, lastChange)
            self.assertEqual(entry.sp_min, min)
            self.assertEqual(entry.sp_max, max)
            self.assertEqual(entry.sp_warn, warn)
            self.assertEqual(entry.sp_inact, inact)
            self.assertEqual(entry.sp_expire, expire)
            self.assertEqual(entry.sp_flag, flag) 
Example #6
Source File: user.py    From docker_interface with Apache License 2.0 5 votes vote down vote up
def get_user_group(self, user=None, group=None):
        """
        Get the user and group information.

        Parameters
        ----------
        user : str
            User name or user id (default is the `os.getuid()`).
        group : str
            Group name or group id (default is the group of `user`).

        Returns
        -------
        user : pwd.struct_passwd
            User object.
        group : grp.struct_group
            Group object.
        """
        user = user or os.getuid()
        # Convert the information we have obtained to a user object
        try:
            try:
                user = pwd.getpwuid(int(user))
            except ValueError:
                user = pwd.getpwnam(user)
        except KeyError as ex:  # pragma: no cover
            self.logger.fatal("could not resolve user: %s", ex)
            raise

        # Get the group
        group = group or user.pw_gid
        try:
            try:
                group = grp.getgrgid(int(group))
            except ValueError:
                group = grp.getgrnam(group)
        except KeyError as ex:  # pragma: no cover
            self.logger.fatal("could not resolve group:%s", ex)
            raise

        return user, group 
Example #7
Source File: docs.py    From dffml with MIT License 5 votes vote down vote up
def fake_getpwuid(uid):
    return pwd.struct_passwd(
        ("user", "x", uid, uid, "", "/home/user", "/bin/bash")
    ) 
Example #8
Source File: test_fakepwd.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_addUser(self):
        """
        L{UserDatabase.addUser} accepts seven arguments, one for each field of
        a L{pwd.struct_passwd}, and makes the new record available via
        L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
        L{UserDatabase.getpwall}.
        """
        username = 'alice'
        password = 'secr3t'
        uid = 123
        gid = 456
        gecos = 'Alice,,,'
        home = '/users/alice'
        shell = '/usr/bin/foosh'

        db = self.database
        db.addUser(username, password, uid, gid, gecos, home, shell)

        for [entry] in [[db.getpwuid(uid)], [db.getpwnam(username)],
                        db.getpwall()]:
            self.assertEqual(entry.pw_name, username)
            self.assertEqual(entry.pw_passwd, password)
            self.assertEqual(entry.pw_uid, uid)
            self.assertEqual(entry.pw_gid, gid)
            self.assertEqual(entry.pw_gecos, gecos)
            self.assertEqual(entry.pw_dir, home)
            self.assertEqual(entry.pw_shell, shell) 
Example #9
Source File: test_fakepwd.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_addUser(self):
        """
        L{UserDatabase.addUser} accepts seven arguments, one for each field of
        a L{pwd.struct_passwd}, and makes the new record available via
        L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
        L{UserDatabase.getpwall}.
        """
        username = 'alice'
        password = 'secr3t'
        lastChange = 17
        min = 42
        max = 105
        warn = 12
        inact = 3
        expire = 400
        flag = 3

        db = self.database
        db.addUser(username, password, lastChange, min, max, warn, inact,
                   expire, flag)

        for [entry] in [[db.getspnam(username)], db.getspall()]:
            self.assertEqual(entry.sp_nam, username)
            self.assertEqual(entry.sp_pwd, password)
            self.assertEqual(entry.sp_lstchg, lastChange)
            self.assertEqual(entry.sp_min, min)
            self.assertEqual(entry.sp_max, max)
            self.assertEqual(entry.sp_warn, warn)
            self.assertEqual(entry.sp_inact, inact)
            self.assertEqual(entry.sp_expire, expire)
            self.assertEqual(entry.sp_flag, flag) 
Example #10
Source File: provider.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def get_users(self):
        """Returns a list of all local users on the computer.

        Each user is represented as a dict with the keys: C{username},
        C{name}, C{uid}, C{enabled}, C{location}, C{work-phone} and
        C{home-phone}.
        """
        users = []
        found_usernames = set()
        for user in self.get_user_data():
            if not isinstance(user, struct_passwd):
                user = struct_passwd(user)
            if user.pw_name in found_usernames:
                continue
            gecos_data = [x or None for x in user.pw_gecos.split(",")[:4]]
            while len(gecos_data) < 4:
                gecos_data.append(None)
            name, location, work_phone, home_phone = tuple(gecos_data)
            enabled = user.pw_name not in self.locked_users
            users.append({"username": user.pw_name, "name": name,
                          "uid": user.pw_uid, "enabled": enabled,
                          "location": location, "work-phone": work_phone,
                          "home-phone": home_phone,
                          "primary-gid": user.pw_gid})
            found_usernames.add(user.pw_name)
        return users 
Example #11
Source File: test_fakepwd.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_addUser(self):
        """
        L{UserDatabase.addUser} accepts seven arguments, one for each field of
        a L{pwd.struct_passwd}, and makes the new record available via
        L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
        L{UserDatabase.getpwall}.
        """
        username = 'alice'
        password = 'secr3t'
        uid = 123
        gid = 456
        gecos = 'Alice,,,'
        home = '/users/alice'
        shell = '/usr/bin/foosh'

        db = self.database
        db.addUser(username, password, uid, gid, gecos, home, shell)

        for entry in [db.getpwuid(uid), db.getpwnam(username)]:
            self.assertEquals(entry.pw_name, username)
            self.assertEquals(entry.pw_passwd, password)
            self.assertEquals(entry.pw_uid, uid)
            self.assertEquals(entry.pw_gid, gid)
            self.assertEquals(entry.pw_gecos, gecos)
            self.assertEquals(entry.pw_dir, home)
            self.assertEquals(entry.pw_shell, shell)

        [entry] = db.getpwall()
        self.assertEquals(entry.pw_name, username)
        self.assertEquals(entry.pw_passwd, password)
        self.assertEquals(entry.pw_uid, uid)
        self.assertEquals(entry.pw_gid, gid)
        self.assertEquals(entry.pw_gecos, gecos)
        self.assertEquals(entry.pw_dir, home)
        self.assertEquals(entry.pw_shell, shell)