diff --git a/github_backup/github_backup.py b/github_backup/github_backup.py index 03f5a65..ff6a377 100755 --- a/github_backup/github_backup.py +++ b/github_backup/github_backup.py @@ -71,14 +71,13 @@ def main(): args.backupdir = args.backupdir.rstrip("/") # Make the connection to Github here. - config = {} + config = {'login_or_token': args.login_or_token} if args.password == False: # no password option given, continue unauthenticated # unauthenticated users can only use http git method args.type = 'http' elif args.password == None: # password option given, but no password value given - config = {'login_or_token': args.login_or_token} if os.path.isfile(CONFFILE): cfg = ConfigParser() cfg.read(CONFFILE) @@ -91,7 +90,6 @@ def main(): if password: config['password'] = password else: - config = {'login_or_token': args.login_or_token} config['password'] = args.password LOGGER.debug("Github config: %r", config) @@ -101,28 +99,33 @@ def main(): if not os.path.exists(args.backupdir): mkdir_p(args.backupdir) - if args.organization: - if args.password: - account = gh.get_organization(args.organization) - else: - account = gh.get_organization(args.login_or_token) - else: - if args.username: - account = gh.get_user(args.username) - elif config.get('password', None): - account = gh.get_user() - else: - account = gh.get_user(args.login_or_token) + while True: + try: + if args.organization: + account = gh.get_organization(args.organization) + else: + if args.username: + account = gh.get_user(args.username) + elif config.get('password', None): + account = gh.get_user() + else: + account = gh.get_user(args.login_or_token) - IS_AUTHORIZED = isinstance(account, github.AuthenticatedUser.AuthenticatedUser) - assert not (bool(config.get('password', None)) ^ IS_AUTHORIZED), account + break + except github.BadCredentialsException: + LOGGER.info("Given login_or_token is not authenticated, ignoring...") + # Reset gh client, try without auth + gh = github.Github() + + IS_AUTHORIZED = isinstance(account, (github.AuthenticatedUser.AuthenticatedUser, github.Organization.Organization)) + assert (bool(config.get('password', None)) or IS_AUTHORIZED), account if args.include_keys and not IS_AUTHORIZED: LOGGER.info("Cannot backup keys with unauthenticated account, ignoring...") args.include_keys = False filters = {} - if IS_AUTHORIZED: + if isinstance(account, github.AuthenticatedUser.AuthenticatedUser): # Get all repos filters = { 'affiliation': ','.join(args.affiliation), @@ -329,12 +332,12 @@ class RepositoryBackup(object): if self.is_gist: url = repo.git_pull_url - elif args.type == 'http' or not IS_AUTHORIZED: - url = repo.clone_url elif args.type == 'ssh': url = repo.ssh_url elif args.type == 'git': url = repo.git_url + elif args.type == 'http' or not IS_AUTHORIZED: + url = repo.clone_url self.url = url self.wiki_url = None