[PATCH] Refactor login logic into interactive_login method in BugzillaBase
by Arun Babu Neelicattu
From: Arun Babu Neelicattu <abn(a)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.'''
--
1.9.3
9 years, 6 months
Refactoring login logic
by Arun Babu Neelicattu
Hi,
One of the annoyances I have had using python-bugzilla in projects has
been the need to copy-paste the login logic w/ token/cookie awareness.
To address this, I'd like to refactor the login logic from the cli
code into bugzilla.util, for which changes might look something like
[1]. This might not be the final version, I am open to suggestions.
Is this something that would be considered useful for other users?
-arun
[1] https://github.com/abn/python-bugzilla/commit/67ec0c76f4e9ad18b5b596be187...
9 years, 7 months
[PATCH] Handle requirements in setup.py
by Arun Babu Neelicattu
From: Arun Babu Neelicattu <abn(a)redhat.com>
---
setup.py | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)
diff --git a/setup.py b/setup.py
index c548fb5..be0ba59 100755
--- a/setup.py
+++ b/setup.py
@@ -166,6 +166,25 @@ class RPMCommand(Command):
os.system("mv /tmp/python-bugzilla.spec .")
+requirements = {}
+
+try:
+ from pip.req import parse_requirements
+
+ requirement_files = {
+ 'install_requires': 'requirements.txt',
+ 'tests_require': 'test-requirements.txt',
+ }
+
+ for name, fname in requirement_files.items():
+ requirements[name] = [
+ str(ir.req) for ir in parse_requirements(fname)
+ ]
+except ImportError:
+ # ignore as this is not useful without pip anyway
+ pass
+
+
setup(name='python-bugzilla',
version=get_version(),
description='Bugzilla XMLRPC access module',
@@ -181,5 +200,6 @@ setup(name='python-bugzilla',
"pylint" : PylintCommand,
"rpm" : RPMCommand,
"test" : TestCommand,
- }
+ },
+ **requirements
)
--
1.9.3
9 years, 7 months
requests dependency missing from setup.py/setup.cfg; bugzilla cli/modules fail to import on machines without requests package
by Garrett Cooper
Hello!
I recently did a pip install of python-bugzilla, which installed
1.1.0, and when I tried running the bugzilla command it failed to
execute because the requests module wasn't installed on my machine.
Replacing requirements.txt with the following lines in setup.py should work:
install_requires=[
'requests',
]
Also, if python-bugzilla depends on certain API versions of
requests, it might be a good idea to do some sort of version checking;
I know that the latest version of the requests package is not
backwards compatible in many ways with the new one.
Thanks!
-Garrett
9 years, 7 months
New bug using custom fields
by Jaime Gómez del Pulgar Ruano
Hello everyone!
First of all, I apologize for my English level
I'm using python-bugzilla to create bugs.It has been working properly, but now I have to add a custom field named "hostname" to the Bugzilla server and I don't achieve that python-bugzilla stores data in that field.
I always get this message:
File "/usr/bin/bugzilla", line 332, in <module>
main()
File "/usr/bin/bugzilla", line 328, in main
buglist = _do_new(bz, opt)
File "/usr/bin/bugzilla", line 231, in _do_new
sub_component=opt.sub_component or None,
TypeError: build_createbug() got an unexpected keyword argument 'cf_hostname'
What is the best way to modify the program and add my request?
Thank you
9 years, 7 months