def _ParseSpec(self, contents, db=None):
        """Return a AccountDatabase that has read a file with |contents|.

    Args:
      contents: desired contents of accounts database to parse.
      db: existing account db to override with new definitions.

    Returns:
      an instance of AccountDatabase.
    """
        if db is None:
            db = accounts_lib.AccountDatabase()
        with self.PatchObject(osutils, 'ReadFile', return_value=contents):
            db.AddAccountsFromDatabase('ignored')
        return db
    def _ParseSpecs(self, specs):
        """Return a AccountDatabase based on the account database stack in |specs|.

    Args:
      specs: list of json fragments (encoded as strings) to compose into a
          consistent account database.  This list is assumed to be in
          increasing priority order so that later entries override earlier
          entries.

    Returns:
      an instance of AccountDatabase.
    """
        db = accounts_lib.AccountDatabase()
        for spec in specs:
            self._ParseSpec(spec, db=db)
        return db
def main(argv):
    cros_build_lib.AssertInsideChroot()
    options = GetOptions(argv)

    if options.action == ACTION_GET_ENTRY:
        db = user_db.UserDB(options.sysroot)
        if options.database == USER_DB:
            print(db.GetUserEntry(options.name, skip_lock=options.nolock))
        else:
            print(db.GetGroupEntry(options.name, skip_lock=options.nolock))
        return 0

    overlays = sysroot_lib.Sysroot(options.sysroot).GetStandardField(
        sysroot_lib.STANDARD_FIELD_PORTDIR_OVERLAY).split()

    # TODO(wiley) This process could be optimized to avoid reparsing these
    #             overlay databases each time.
    account_db = accounts_lib.AccountDatabase()
    for overlay_path in overlays:
        database_path = os.path.join(overlay_path, ACCOUNT_DB_FILENAME)
        if os.path.exists(database_path):
            account_db.AddAccountsFromDatabase(database_path)

    installed_users = user_db.UserDB(options.sysroot)

    if options.action == ACTION_INSTALL_USER:
        account_db.InstallUser(options.name,
                               installed_users,
                               uid=options.uid,
                               shell=options.shell,
                               homedir=options.home,
                               primary_group=options.primary_group)

        homedir = account_db.users[options.name].home
        homedir_path = os.path.join(options.sysroot, homedir)

        if homedir != '/dev/null' and not os.path.exists(homedir_path):
            osutils.SafeMakedirs(homedir_path, sudo=True)
            uid = account_db.users[options.name].uid
            cros_build_lib.sudo_run(
                ['chown', '%d:%d' % (uid, uid), homedir_path], print_cmd=False)

    elif options.action == ACTION_INSTALL_GROUP:
        account_db.InstallGroup(options.name, installed_users, gid=options.gid)
    else:
        cros_build_lib.Die('Unsupported account type: %s' %
                           options.account_type)