Skip to content

Commit e97789b

Browse files
abncrobinso
authored andcommitted
Refactor login logic into interactive_login method in BugzillaBase
1 parent 768b529 commit e97789b

2 files changed

Lines changed: 47 additions & 36 deletions

File tree

bin/bugzilla

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
from __future__ import print_function
1515

16-
import getpass
1716
import locale
1817
import logging
1918
import optparse
@@ -1169,7 +1168,9 @@ def main(bzinstance=None):
11691168

11701169
# Handle 'login' action
11711170
is_login_command = (action == 'login')
1172-
if is_login_command or global_opt.login:
1171+
force_login = is_login_command or global_opt.login
1172+
1173+
if force_login:
11731174
if is_login_command:
11741175
if len(args) == 2:
11751176
(global_opt.user, global_opt.password) = args
@@ -1178,42 +1179,15 @@ def main(bzinstance=None):
11781179
elif len(args) > 2:
11791180
parser.error("Too many arguments for login")
11801181

1181-
if not global_opt.user:
1182-
sys.stdout.write('Username: ')
1183-
user = sys.stdin.readline()
1184-
global_opt.user = user.strip()
1185-
if not global_opt.password:
1186-
global_opt.password = getpass.getpass()
1187-
sys.stdout.write('Logging in... ')
1188-
sys.stdout.flush()
1189-
1190-
try:
1191-
bz.login(global_opt.user, global_opt.password)
1192-
print('Authorization cookie received.')
1193-
except bugzilla.BugzillaError:
1194-
print(str(sys.exc_info()[1]))
1195-
sys.exit(1)
1196-
1182+
try:
1183+
if not _is_unittest or force_login:
1184+
bz.interactive_login(
1185+
global_opt.user, global_opt.password, force_login)
11971186
if is_login_command:
11981187
sys.exit(0)
1199-
1200-
# Set up authentication
1201-
if global_opt.user:
1202-
if not global_opt.password:
1203-
global_opt.password = getpass.getpass()
1204-
log.info('Using username/password for authentication')
1205-
bz.login(global_opt.user, global_opt.password)
1206-
elif not _is_unittest:
1207-
if ((bz.cookiefile and os.path.exists(bz.cookiefile)) or
1208-
(bz.tokenfile and os.path.exists(bz.tokenfile))):
1209-
if bz.cookiefile and os.path.exists(bz.cookiefile):
1210-
log.info('Using cookies in %s for authentication',
1211-
bz.cookiefile)
1212-
if bz.tokenfile and os.path.exists(bz.tokenfile):
1213-
log.info('Using token in %s for authentication',
1214-
bz.tokenfile)
1215-
else:
1216-
log.info('No authentication info provided.')
1188+
except bugzilla.BugzillaError:
1189+
print(str(sys.exc_info()[1]))
1190+
sys.exit(1)
12171191

12181192

12191193
###########################

bugzilla/base.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import sys
1616

17+
from getpass import getpass
1718
from io import BytesIO
1819

1920
if hasattr(sys.version_info, "major") and sys.version_info.major >= 3:
@@ -651,6 +652,42 @@ def login(self, user=None, password=None):
651652
e = sys.exc_info()[1]
652653
raise BugzillaError("Login failed: %s" % str(e.faultString))
653654

655+
def interactive_login(self, user=None, password=None, force=False):
656+
"""
657+
Helper method to handle login for this bugzilla instance.
658+
659+
If a 'user' is provided or 'force' is set to True; or no cookie/token
660+
file exists, a username/password authentication is attempted requesting
661+
any information that is not available from the user.
662+
663+
If a cookie/token file exists, the call to the instance login method is
664+
skipped.
665+
"""
666+
if not force and user is None:
667+
auths = {
668+
'cookies': self.cookiefile,
669+
'token': self.tokenfile,
670+
}
671+
for (method, source) in auths.items():
672+
if source and os.path.exists(source):
673+
log.info(
674+
'Using %s in %s for authentication', method, source)
675+
return
676+
elif not force:
677+
log.error('No authentication information provided for login')
678+
679+
log.info('Using username/password for authentication')
680+
681+
if not user:
682+
sys.stdout.write('Bugzilla Username: ')
683+
user = sys.stdin.readline().strip()
684+
if not password:
685+
password = getpass('Bugzilla Password: ')
686+
687+
log.info('Logging in... ')
688+
self.login(user, password)
689+
log.info('Authorization cookie received.')
690+
654691
def logout(self):
655692
'''Log out of bugzilla. Drops server connection and user info, and
656693
destroys authentication cookies.'''

0 commit comments

Comments
 (0)