def getbugzillaauth()

in pylib/mozhg/mozhg/auth.py [0:0]


def getbugzillaauth(ui, require=False, profile=None):
    """Obtain Bugzilla authentication credentials from any possible source.

    This returns a BugzillaAuth instance on success or None on failure.

    If ``require`` is True, we abort if Bugzilla credentials could not be
    found.

    If ``profile`` is defined, we will only consult the profile having this
    name. The default behavior is to examine all available profiles.

    The order of preference for Bugzilla credentials is as follows:

      1) bugzilla.username and bugzilla.apikey from hgrc
      2) bugzilla.userid and bugzilla.cookie from hgrc
      3) bugzilla.username and bugzilla.password from hgrc
      4) login cookies from Firefox profiles
      5) prompt the user

    The ``bugzilla.firefoxprofile`` option is interpreted as a list of Firefox
    profiles from which data should be read. This overrides the default sort
    order.
    """

    username = ui.config(b"bugzilla", b"username", None)
    apikey = ui.config(b"bugzilla", b"apikey", None)
    password = ui.config(b"bugzilla", b"password", None)
    userid = ui.config(b"bugzilla", b"userid", None)
    cookie = ui.config(b"bugzilla", b"cookie", None)
    profileorder = ui.configlist(b"bugzilla", b"firefoxprofile", [])

    if username and apikey:
        return BugzillaAuth(username=username, apikey=apikey)

    if userid and cookie:
        return BugzillaAuth(userid=userid, cookie=cookie)

    if username and password:
        return BugzillaAuth(username=username, password=password)

    ui.debug(b"searching for Bugzilla cookies in Firefox profile\n")
    url = ui.config(b"bugzilla", b"url", b"https://bugzilla.mozilla.org/")
    profilesdir = find_profiles_path()
    profiles = get_profiles(profilesdir)

    # If the list of profiles is explicitly defined, filter out unknown
    # profiles and sort by order.
    if profileorder:
        profiles = [p for p in profiles if p[b"name"] in profileorder]
        profiles = sorted(profiles, key=lambda p: profileorder.index(p[b"name"]))

    for p in profiles:
        if profile and p[b"name"] != profile:
            continue

        try:
            userid, cookie = get_bugzilla_login_cookie_from_profile(p[b"path"], url)

            if userid and cookie:
                return BugzillaAuth(userid=userid, cookie=cookie)
        except NoSQLiteError:
            ui.warn(b"SQLite unavailable. Unable to look for Bugzilla cookie.\n")
            break

    if not username:
        username = ui.prompt(_(b"bugzilla username:"), b"")

    if not password:
        password = ui.getpass(_(b"bugzilla password: "), b"")

    if username and password:
        return BugzillaAuth(username=username, password=password)

    if require:
        raise error.Abort(_(b"unable to obtain Bugzilla authentication."))

    return None