def _global_init(p4):
    """Create global Git Fusion Perforce data:
    * user git-fusion-user
    * depot //.git-fusion
    * group git-fusion-pull
    * group git-fusion-push
    * protects entries
    """

    #
    # The global initialization process below must be idempotent in the sense
    # that it is safe to perform more than once. As such, there are checks to
    # determine if work is needed or not, and if that work results in an
    # error, log and carry on with the rest of the steps, with the assumption
    # that a previous attempt had failed in the middle (or possibly that
    # another instance of Git Fusion has started at nearly the same time as
    # this one).
    #

    with p4gf_group.PermErrorOK(p4):
        p4gf_util.ensure_user_gf(p4)

    with p4gf_group.PermErrorOK(p4):
        p4gf_util.ensure_depot_gf(p4)

    p4gf_group.create_global_perm(p4, p4gf_group.PERM_PULL)
    p4gf_group.create_global_perm(p4, p4gf_group.PERM_PUSH)
    p4gf_group.create_default_perm(p4)

    ### ONCE ADMIN works, downgrade our auto-generated Protections
    ### table to git-fusion-user=admin, not super, and user * = write.

    # Require that single git-fusion-user have admin privileges
    # over the //.git-fusion/ depot
    is_protects_empty = False
    try:
        ### ONCE ADMIN works, remove the use of -u option
        p4.run('protects', '-u', p4gf_const.P4GF_USER, '-m',
               '//{depot}/...'.format(depot=p4gf_const.P4GF_DEPOT))
    except P4.P4Exception:
        if p4gf_p4msg.find_msgid(p4, p4gf_p4msgid.MsgDm_ProtectsEmpty):
            is_protects_empty = True
        # All other errors are fatal, propagated.

    if is_protects_empty:
        ### ONCE ADMIN works, modify the protects table as follows
        # - order the lines in increasing permission
        # - end with at least one user (even a not-yet-created user) with super
        #     write user * * //...
        #     admin user git-fusion-user * //...
        #     super user super * //...
        p4gf_util.set_spec(p4, 'protect', values={
            'Protections': ["super user * * //...",
                            "super user {user} * //...".format(user=p4gf_const.P4GF_USER),
                            "admin user {user} * //{depot}/..."
                            .format(user=p4gf_const.P4GF_USER, depot=p4gf_const.P4GF_DEPOT)]})
def ensure_user():
    """Create Perforce user git-fusion-user if not already exists."""
    created = p4gf_util.ensure_user_gf(p4)
    if created:
        report(INFO, "User {} created.".format(p4gf_const.P4GF_USER))
    else:
        report(INFO, "User {} already exists. Not creating."
                     .format(p4gf_const.P4GF_USER))
    return created
def ensure_user():
    """Create Perforce user git-fusion-user if not already exists."""
    created = p4gf_util.ensure_user_gf(p4)
    if created:
        report(INFO, "User {} created.".format(p4gf_const.P4GF_USER))
    else:
        report(
            INFO, "User {} already exists. Not creating.".format(
                p4gf_const.P4GF_USER))
    return created
def ensure_users():
    """Create Perforce user git-fusion-user, and reviews users if not already extant."""
    created = p4gf_util.ensure_user_gf(p4)
    log_info(created, p4gf_const.P4GF_USER)
    set_user_passwd_if_created(created, p4gf_const.P4GF_USER)

    created = p4gf_util.ensure_user_reviews(p4)
    log_info(created, p4gf_util.gf_reviews_user_name())
    set_user_passwd_if_created(created, p4gf_util.gf_reviews_user_name())

    created = p4gf_util.ensure_user_reviews_non_gf(p4)
    log_info(created, p4gf_const.P4GF_REVIEWS__NON_GF)
    set_user_passwd_if_created(created, p4gf_const.P4GF_REVIEWS__NON_GF)

    created = p4gf_util.ensure_user_reviews_all_gf(p4)
    log_info(created, p4gf_const.P4GF_REVIEWS__ALL_GF)
    set_user_passwd_if_created(created, p4gf_const.P4GF_REVIEWS__ALL_GF)

    # Report whether 'unknown_git' exists. Do not create.
    e = p4gf_util.service_user_exists(p4, p4gf_const.P4GF_UNKNOWN_USER)
    _exists = ( _("Git Fusion user '{0}' does not exist.")
              , _("Git Fusion user '{0}' exists."))
    Verbosity.report(Verbosity.INFO, _exists[e].format(p4gf_const.P4GF_UNKNOWN_USER))
def ensure_users():
    """Create Perforce user git-fusion-user, and reviews users if not already extant."""
    created = p4gf_util.ensure_user_gf(p4)
    log_info(created, p4gf_const.P4GF_USER)
    set_user_passwd_if_created(created, p4gf_const.P4GF_USER)

    created = p4gf_util.ensure_user_reviews(p4)
    log_info(created, p4gf_util.gf_reviews_user_name())
    set_user_passwd_if_created(created, p4gf_util.gf_reviews_user_name())

    created = p4gf_util.ensure_user_reviews_non_gf(p4)
    log_info(created, p4gf_const.P4GF_REVIEWS__NON_GF)
    set_user_passwd_if_created(created, p4gf_const.P4GF_REVIEWS__NON_GF)

    created = p4gf_util.ensure_user_reviews_all_gf(p4)
    log_info(created, p4gf_const.P4GF_REVIEWS__ALL_GF)
    set_user_passwd_if_created(created, p4gf_const.P4GF_REVIEWS__ALL_GF)

    # Report whether 'unknown_git' exists. Do not create.
    e = p4gf_util.service_user_exists(p4, p4gf_const.P4GF_UNKNOWN_USER)
    _exists = (_("Git Fusion user '{0}' does not exist."),
               _("Git Fusion user '{0}' exists."))
    Verbosity.report(Verbosity.INFO,
                     _exists[e].format(p4gf_const.P4GF_UNKNOWN_USER))