##################################################################################
#                                                                                #
# PkgTk: Toolkit for packaging                                                   #
#                                                                                #
# Homepage: pkgtk.readthedocs.io                                                 #
#                                                                                #
# This software is distributed under the CeCILL-C license. You should have       #
# received a copy of the legalcode along with this work. If not, see             #
# <http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html>.                 #
#                                                                                #
# File authors: Pierre Fernique <pfernique@gmail.com> (2)                        #
#                                                                                #
##################################################################################

import os
import git
import github3
import parse
import re
import time

from getpass import getpass
from distutils.version import LooseVersion

from .vcs import get_vcs
from .about import About

GITHUB_USERNAME = None
GITHUB_PASSWORD = None

def load_about(repository, config):
    global GITHUB_USERNAME, GITHUB_PASSWORD
    vcs = get_vcs(repository)
    if vcs == 'git':
        about = config.get('about', dict())
        if 'remote' not in about:
            repo = git.Repo(repository)
            result = None
            for remote in repo.remotes:
                for url in remote.urls:
                    result = parse.parse('{protocol}://{host}/{owner}/{repository}.git', url)
                    if result and result['host'] == 'github.com':
                        break
                    else:
                        result = None
                if result:
                    break
        else:
            remote = about['remote']
            result = parse.parse('{protocol}://{host}/{owner}/{repository}.git', remote)
        if not result or not result['host'] == 'github.com':
            raise Exception("")
        about = config.get('about', dict())
        if any(field not in about for field in ['name', 'brief', 'homepage', 'version', 'authors', 'email']):
            import pdb
            pdb.set_trace()
            session = github3.GitHub()
            if session.ratelimit_remaining == 0:
                delay = int(session.rate_limit()['rate']['reset'] - time.time()) + 1
                print 'Waiting for ' + str(delay) + 's'
                time.sleep(delay)
                session = github3.GitHub()
                # if not GITHUB_USERNAME:
                #     GITHUB_USERNAME = raw_input("Username for 'https://github.com': ")
                # if not GITHUB_PASSWORD:
                #     GITHUB_PASSWORD = getpass("Password for 'https://" + GITHUB_USERNAME + "@github.com': ")
                # session = github3.GitHub(GITHUB_USERNAME, GITHUB_PASSWORD)
            owner = session.organization(result['owner'])
            if isinstance(owner, github3.null.NullObject):
                owner = session.user(result['owner'])
                owner.authors = [owner]
            else:
                owner.authors = list(owner.members())
            version = [tag.name for tag in git.Repo(repository).tags if  re.match('[A-Za-z]*(|-|_)[0-9]*\.[0-9]*(|\..*)', tag.name)]
            if version:
                version = max(version, key = lambda version: LooseVersion(version))
            else:
                version = None
            repository = session.repository(result['owner'], result['repository'])
            about = About(name = about.get('name', repository.name),
                          brief = about.get('brief', repository.description),
                          homepage = about.get('homepage', repository.homepage),
                          version = about.get('version', version),
                          authors = about.get('authors', [author.name for author in owner.authors]),
                          email = about.get('email', owner.email))
        else:
            about = About(name = about['name'],
                          brief = about['brief'],
                          homepage = about['homepage'],
                          version = about['version'],
                          authors = about['authors'],
                          email = about['email'])
        return about
    else:
        raise Exception()


@@ -1272,55 +1272,8 @@
 ut%0A%0A
-GITHUB_USERNAME = None%0AGITHUB_PASSWORD = None%0A%0A
 def 
@@ -2678,369 +2678,8 @@
 b()%0A
-                # if not GITHUB_USERNAME:%0A                #     GITHUB_USERNAME = raw_input(%22Username for 'https://github.com': %22)%0A                # if not GITHUB_PASSWORD:%0A                #     GITHUB_PASSWORD = getpass(%22Password for 'https://%22 + GITHUB_USERNAME + %22@github.com': %22)%0A                # session = github3.GitHub(GITHUB_USERNAME, GITHUB_PASSWORD)%0A
     

Update .pkgtk.yml