<commit_before>"""Assorted codetools utility functions."""

# technical debt
# --------------
# - package
# - exception rather than sysexit
# - check explictly for github3 version

import os
import shutil
import sys
import tempfile
import urllib3
import re
from github3 import login
import gitconfig


__all__ = ['login_github', 'eups2git_ref', 'repos_for_team',
           'github_2fa_callback', 'TempDir', 'gitusername', 'gituseremail',
           'get_team_id_by_name']


def login_github(token_path=None, token=None):
    """Log into GitHub using an existing token.

    Parameters
    ----------
    token_path : str, optional
        Path to the token file. The default token is used otherwise.

    token: str, optional
        Literial token string. If specifified, this value is used instead of
        reading from the token_path file.

    Returns
    -------
    gh : :class:`github3.GitHub` instance
        A GitHub login instance.
    """
    if token is None:
        if token_path is None:
            # Try the default token
            token_path = '~/.sq_github_token'
        token_path = os.path.expandvars(os.path.expanduser(token_path))

        if not os.path.isfile(token_path):
            print "You don't have a token in {0} ".format(token_path)
            print "Have you run github-auth"
            sys.exit(1)

        with open(token_path, 'r') as fd:
            token = fd.readline().strip()

    gh = login(token=token, two_factor_callback=github_2fa_callback)

    return gh


def gitusername():
    """
    Returns the user's name from .gitconfig if available
    """

    try:
        mygitconfig = gitconfig.GitConfig()
        return mygitconfig['user.name']
    except:
        return None<commit_msg>Convert sys.exit() to an exception.<commit_after>"""Assorted codetools utility functions."""

# technical debt
# --------------
# - package
# - check explictly for github3 version

import os
import shutil
import sys
import tempfile
import urllib3
import re
from github3 import login
import gitconfig


__all__ = ['login_github', 'eups2git_ref', 'repos_for_team',
           'github_2fa_callback', 'TempDir', 'gitusername', 'gituseremail',
           'get_team_id_by_name']


def login_github(token_path=None, token=None):
    """Log into GitHub using an existing token.

    Parameters
    ----------
    token_path : str, optional
        Path to the token file. The default token is used otherwise.

    token: str, optional
        Literial token string. If specifified, this value is used instead of
        reading from the token_path file.

    Returns
    -------
    gh : :class:`github3.GitHub` instance
        A GitHub login instance.
    """
    if token is None:
        if token_path is None:
            # Try the default token
            token_path = '~/.sq_github_token'
        token_path = os.path.expandvars(os.path.expanduser(token_path))

        if not os.path.isfile(token_path):
            print "You don't have a token in {0} ".format(token_path)
            print "Have you run github-auth?"
            raise EnvironmentError("No token in %v" % token_path)

        with open(token_path, 'r') as fd:
            token = fd.readline().strip()

    gh = login(token=token, two_factor_callback=github_2fa_callback)

    return gh


def gitusername():
    """
    Returns the user's name from .gitconfig if available
    """

    try:
        mygitconfig = gitconfig.GitConfig()
        return mygitconfig['user.name']
    except:
        return None
{'fraction_non_alphanumeric': 0.10508378301618858, 'fraction_numerical': 0.004828173814257313, 'mean_word_length': 2.469950738916256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2055875', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1005, 'n_words': 322}