<commit_before>#!/usr/bin/env python3
"""Delete all repos in the Github <user>-shadow org."""
import textwrap
import argparse
import os
from time import sleep
import progressbar
from .. import codetools


def parse_args():
    """Parse command-line arguments"""
    parser = argparse.ArgumentParser(
        prog='github-delete-shadow',
        description=textwrap.dedent("""Delete all repos in the GitHub
            <username>-shadow org"""),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='Part of codekit: https://github.com/lsst-sqre/sqre-codekit')
    parser.add_argument(
        '-u', '--user',
        help='GitHub username',
        dest='user',
        required=True)
    parser.add_argument(
        '--token-path',
        default='~/.sq_github_token_delete',
        help='Use a token (made with github-auth) in a non-standard loction')
    parser.add_argument(
        '--token',
        default=None,
        help='Literal github personal access token string')
    parser.add_argument(
        '-d', '--debug',
        action='store_true',
        default=os.getenv('DM_SQUARE_DEBUG'),
        help='Debug mode')
    parser.add_argument('-v', '--version', action=codetools.ScmVersionAction)
    return parser.parse_args()


def countdown_timer():
    """Show countdown bar"""
    widgets = ['Pause for panic: ', progressbar.ETA(), ' ', progressbar.Bar()]
    pbar = progressbar.ProgressBar(widgets=widgets, max_value=200).start()
    for i in range(200):
        pbar.update(i)
        sleep(0.1)
    pbar.finish()


def main():
    """Delete user shadow org"""
    work = 0
    nowork = 0

    args = parse_args()
    # Deliberately hardcoding the -shadow part due to cowardice
    orgname = '{user}-shadow'.format(user=args.user)

    if args.debug:
        print('org:', orgname)

    ghb = codetools.login_github(token_path=args.token_path, token=args.token)

    # get the organization object
    organization = ghb.organization(orgname)

    # get all the repos
    repos = [g for g in organization.repositories()]

    print('Deleting all repos in', orgname)
    print('Now is the time to panic and Ctrl-C')

    countdown_timer()

    print('Here goes:')

    if args.debug:
        delay = 5
        print(delay, 'second gap between deletions')
        work = 0
        nowork = 0

    for repo in repos:

        if args.debug:
            print('Next deleting:', repo.name, '...',)
            sleep(delay)

        status = repo.delete()

        if status:
            print('ok')
            work += 1
        else:
            print('FAILED - does your token have delete_repo scope?')
            nowork += 1

    print('Done - Succeed:', work, 'Failed:', nowork)
    if work:
        print('Consider deleting your privileged auth token', args.token_path)


if __name__ == '__main__':
    main()<commit_msg>switch github-delete-shadow from github3.py -> pygithub<commit_after>#!/usr/bin/env python3
"""Delete all repos in the Github <user>-shadow org."""

from codekit.codetools import error
from codekit import codetools
from codekit import pygithub
from time import sleep
import argparse
import github
import logging
import os
import progressbar
import sys
import textwrap

logging.basicConfig()
logger = logging.getLogger('codekit')


def parse_args():
    """Parse command-line arguments"""
    parser = argparse.ArgumentParser(
        prog='github-delete-shadow',
        description=textwrap.dedent("""Delete all repos in the GitHub
            <username>-shadow org"""),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog='Part of codekit: https://github.com/lsst-sqre/sqre-codekit')
    parser.add_argument(
        '-u', '--user',
        help='GitHub username',
        dest='user',
        required=True)
    parser.add_argument(
        '--token-path',
        default='~/.sq_github_token_delete',
        help='Use a token (made with github-auth) in a non-standard loction')
    parser.add_argument(
        '--token',
        default=None,
        help='Literal github personal access token string')
    parser.add_argument(
        '-d', '--debug',
        action='store_true',
        default=os.getenv('DM_SQUARE_DEBUG'),
        help='Debug mode')
    parser.add_argument('-v', '--version', action=codetools.ScmVersionAction)
    return parser.parse_args()


def countdown_timer():
    """Show countdown bar"""
    widgets = ['Pause for panic: ', progressbar.ETA(), ' ', progressbar.Bar()]
    pbar = progressbar.ProgressBar(widgets=widgets, max_value=200).start()
    for i in range(200):
        pbar.update(i)
        sleep(0.1)
    pbar.finish()


def main():
    """Delete Github shadow org"""
    args = parse_args()
    # Deliberately hardcoding the -shadow part due to cowardice
    orgname = '{user}-shadow'.format(user=args.user)

    if args.debug:
        print('org:', orgname)

    g = pygithub.login_github(token_path=args.token_path, token=args.token)
    org = g.get_organization(orgname)
    # get all the repos
    repos = list(org.get_repos())

    print('Deleting all repos in', orgname)
    print('Now is the time to panic and Ctrl-C')

    countdown_timer()

    print('Here goes:')

    if args.debug:
        delay = 5
        print(delay, 'second gap between deletions')

    work = 0
    nowork = 0
    problems = []
    for r in repos:
        print('Next deleting:', r.full_name, '...',)

        if args.debug:
            sleep(delay)

        try:
            r.delete()
            work += 1
            print('ok')
        except github.GithubException as e:
            yikes = pygithub.CaughtGitError(r, e)
            problems.append(yikes)
            nowork += 1
            print('FAILED - does your token have delete_repo scope?')

    print('Done - Succeed:', work, 'Failed:', nowork)
    if problems:
        error("ERROR: {n} failures".format(n=str(len(problems))))

        for e in problems:
            error(e)

        sys.exit(1)

    if work:
        print('Consider deleting your privileged auth token', args.token_path)


if __name__ == '__main__':
    main()
{'fraction_non_alphanumeric': 0.126327831344991, 'fraction_numerical': 0.0052296126818107535, 'mean_word_length': 2.585237258347979, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 2, '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': '2345940', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1809, 'n_words': 480}