From: Arun Babu Neelicattu abn@redhat.com
--- bin/bugzilla | 46 ++++++++++------------------------------------ bugzilla/base.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 36 deletions(-)
diff --git a/bin/bugzilla b/bin/bugzilla index 14ed6b6..59f65a5 100755 --- a/bin/bugzilla +++ b/bin/bugzilla @@ -13,7 +13,6 @@
from __future__ import print_function
-import getpass import locale import logging import optparse @@ -1169,7 +1168,9 @@ def main(bzinstance=None):
# Handle 'login' action is_login_command = (action == 'login') - if is_login_command or global_opt.login: + force_login = is_login_command or global_opt.login + + if force_login: if is_login_command: if len(args) == 2: (global_opt.user, global_opt.password) = args @@ -1178,42 +1179,15 @@ def main(bzinstance=None): elif len(args) > 2: parser.error("Too many arguments for login")
- if not global_opt.user: - sys.stdout.write('Username: ') - user = sys.stdin.readline() - global_opt.user = user.strip() - if not global_opt.password: - global_opt.password = getpass.getpass() - sys.stdout.write('Logging in... ') - sys.stdout.flush() - - try: - bz.login(global_opt.user, global_opt.password) - print('Authorization cookie received.') - except bugzilla.BugzillaError: - print(str(sys.exc_info()[1])) - sys.exit(1) - + try: + if not _is_unittest or force_login: + bz.interactive_login( + global_opt.user, global_opt.password, force_login) if is_login_command: sys.exit(0) - - # Set up authentication - if global_opt.user: - if not global_opt.password: - global_opt.password = getpass.getpass() - log.info('Using username/password for authentication') - bz.login(global_opt.user, global_opt.password) - elif not _is_unittest: - if ((bz.cookiefile and os.path.exists(bz.cookiefile)) or - (bz.tokenfile and os.path.exists(bz.tokenfile))): - if bz.cookiefile and os.path.exists(bz.cookiefile): - log.info('Using cookies in %s for authentication', - bz.cookiefile) - if bz.tokenfile and os.path.exists(bz.tokenfile): - log.info('Using token in %s for authentication', - bz.tokenfile) - else: - log.info('No authentication info provided.') + except bugzilla.BugzillaError: + print(str(sys.exc_info()[1])) + sys.exit(1)
########################### diff --git a/bugzilla/base.py b/bugzilla/base.py index 4c1630c..e06223f 100644 --- a/bugzilla/base.py +++ b/bugzilla/base.py @@ -14,6 +14,7 @@ from logging import getLogger import os import sys
+from getpass import getpass from io import BytesIO
if hasattr(sys.version_info, "major") and sys.version_info.major >= 3: @@ -651,6 +652,42 @@ class BugzillaBase(object): e = sys.exc_info()[1] raise BugzillaError("Login failed: %s" % str(e.faultString))
+ def interactive_login(self, user=None, password=None, force=False): + """ + Helper method to handle login for this bugzilla instance. + + If a 'user' is provided or 'force' is set to True; or no cookie/token + file exists, a username/password authentication is attempted requesting + any information that is not available from the user. + + If a cookie/token file exists, the call to the instance login method is + skipped. + """ + if not force and user is None: + auths = { + 'cookies': self.cookiefile, + 'token': self.tokenfile, + } + for (method, source) in auths.items(): + if source and os.path.exists(source): + log.info( + 'Using %s in %s for authentication', method, source) + return + elif not force: + log.error('No authentication information provided for login') + + log.info('Using username/password for authentication') + + if not user: + sys.stdout.write('Bugzilla Username: ') + user = sys.stdin.readline().strip() + if not password: + password = getpass('Bugzilla Password: ') + + log.info('Logging in... ') + self.login(user, password) + log.info('Authorization cookie received.') + def logout(self): '''Log out of bugzilla. Drops server connection and user info, and destroys authentication cookies.'''
On 09/27/2014 10:27 AM, abn@redhat.com wrote:
From: Arun Babu Neelicattu abn@redhat.com
Thanks Arun, pushed now.
- Cole
bin/bugzilla | 46 ++++++++++------------------------------------ bugzilla/base.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 36 deletions(-)
diff --git a/bin/bugzilla b/bin/bugzilla index 14ed6b6..59f65a5 100755 --- a/bin/bugzilla +++ b/bin/bugzilla @@ -13,7 +13,6 @@
from __future__ import print_function
-import getpass import locale import logging import optparse @@ -1169,7 +1168,9 @@ def main(bzinstance=None):
# Handle 'login' action is_login_command = (action == 'login')
- if is_login_command or global_opt.login:
- force_login = is_login_command or global_opt.login
- if force_login: if is_login_command: if len(args) == 2: (global_opt.user, global_opt.password) = args
@@ -1178,42 +1179,15 @@ def main(bzinstance=None): elif len(args) > 2: parser.error("Too many arguments for login")
if not global_opt.user:
sys.stdout.write('Username: ')
user = sys.stdin.readline()
global_opt.user = user.strip()
if not global_opt.password:
global_opt.password = getpass.getpass()
sys.stdout.write('Logging in... ')
sys.stdout.flush()
try:
bz.login(global_opt.user, global_opt.password)
print('Authorization cookie received.')
except bugzilla.BugzillaError:
print(str(sys.exc_info()[1]))
sys.exit(1)
- try:
if not _is_unittest or force_login:
bz.interactive_login(
global_opt.user, global_opt.password, force_login) if is_login_command: sys.exit(0)
- # Set up authentication
- if global_opt.user:
if not global_opt.password:
global_opt.password = getpass.getpass()
log.info('Using username/password for authentication')
bz.login(global_opt.user, global_opt.password)
- elif not _is_unittest:
if ((bz.cookiefile and os.path.exists(bz.cookiefile)) or
(bz.tokenfile and os.path.exists(bz.tokenfile))):
if bz.cookiefile and os.path.exists(bz.cookiefile):
log.info('Using cookies in %s for authentication',
bz.cookiefile)
if bz.tokenfile and os.path.exists(bz.tokenfile):
log.info('Using token in %s for authentication',
bz.tokenfile)
else:
log.info('No authentication info provided.')
except bugzilla.BugzillaError:
print(str(sys.exc_info()[1]))
sys.exit(1)
###########################
diff --git a/bugzilla/base.py b/bugzilla/base.py index 4c1630c..e06223f 100644 --- a/bugzilla/base.py +++ b/bugzilla/base.py @@ -14,6 +14,7 @@ from logging import getLogger import os import sys
+from getpass import getpass from io import BytesIO
if hasattr(sys.version_info, "major") and sys.version_info.major >= 3: @@ -651,6 +652,42 @@ class BugzillaBase(object): e = sys.exc_info()[1] raise BugzillaError("Login failed: %s" % str(e.faultString))
- def interactive_login(self, user=None, password=None, force=False):
"""
Helper method to handle login for this bugzilla instance.
If a 'user' is provided or 'force' is set to True; or no cookie/token
file exists, a username/password authentication is attempted requesting
any information that is not available from the user.
If a cookie/token file exists, the call to the instance login method is
skipped.
"""
if not force and user is None:
auths = {
'cookies': self.cookiefile,
'token': self.tokenfile,
}
for (method, source) in auths.items():
if source and os.path.exists(source):
log.info(
'Using %s in %s for authentication', method, source)
return
elif not force:
log.error('No authentication information provided for login')
log.info('Using username/password for authentication')
if not user:
sys.stdout.write('Bugzilla Username: ')
user = sys.stdin.readline().strip()
if not password:
password = getpass('Bugzilla Password: ')
log.info('Logging in... ')
self.login(user, password)
log.info('Authorization cookie received.')
- def logout(self): '''Log out of bugzilla. Drops server connection and user info, and destroys authentication cookies.'''
python-bugzilla@lists.stg.fedorahosted.org