@@ -129,6 +129,32 @@ def _open_bugzillarc(configpaths=-1):
129129 return cfg
130130
131131
132+ def _save_api_key (url , api_key ):
133+ """
134+ Save the API_KEY in the config file.
135+
136+ If tokenfile and cookiefile are undefined, it means that the
137+ API was called with --no-cache-credentials and no change will be
138+ made
139+ """
140+ config_filename = _default_config_location ('bugzillarc' )
141+ section = _parse_hostname (url )
142+
143+ cfg = ConfigParser ()
144+ cfg .read (config_filename )
145+
146+ if section not in cfg .sections ():
147+ cfg .add_section (section )
148+
149+ cfg [section ]['api_key' ] = api_key .strip ()
150+
151+ with open (config_filename , 'w' ) as configfile :
152+ cfg .write (configfile )
153+
154+ log .info ("API key written to %s" , config_filename )
155+ print ("API key written to %s" % config_filename )
156+
157+
132158class _FieldAlias (object ):
133159 """
134160 Track API attribute names that differ from what we expose in users.
@@ -311,7 +337,8 @@ def __init__(self, url=-1, user=None, password=None, cookiefile=-1,
311337 self ._field_aliases = []
312338 self ._init_field_aliases ()
313339
314- if not use_creds :
340+ self ._use_creds = use_creds
341+ if not self ._use_creds :
315342 cookiefile = None
316343 tokenfile = None
317344 configpaths = []
@@ -646,34 +673,6 @@ def login(self, user=None, password=None, restrict_login=None):
646673 except Fault as e :
647674 raise BugzillaError ("Login failed: %s" % str (e .faultString ))
648675
649- def _save_api_key (self ):
650- """
651- Save the API_KEY in the config file.
652-
653- If toklenfile and cookiefile are undefined, it meas that the
654- API was called with --no-cache-credentials and no change will be
655- made
656- """
657- if self .tokenfile is None and self .cookiefile is None :
658- log .info ("API Key won't be updated" )
659- return
660-
661- config_filename = _default_config_location ('bugzillarc' )
662- section = _parse_hostname (self .url )
663-
664- cfg = ConfigParser ()
665- cfg .read (config_filename )
666-
667- if section not in cfg .sections ():
668- cfg .add_section (section )
669-
670- cfg [section ]['api_key' ] = self .api_key .strip ()
671-
672- with open (config_filename , 'w' ) as configfile :
673- cfg .write (configfile )
674-
675- log .info ("API Key updated in %s" , config_filename )
676-
677676 def interactive_login (self , user = None , password = None , force = False ,
678677 restrict_login = None , use_api_key = False ):
679678 """
@@ -683,7 +682,7 @@ def interactive_login(self, user=None, password=None, force=False,
683682 :param password: bugzilla password. If not specified, prompt for it.
684683 :param force: Unused
685684 :param restrict_login: restricts session to IP address
686- :param use_api_key: True if the login should be done using an api_key
685+ :param use_api_key: If True, prompt for an api_key instead
687686 """
688687 ignore = force
689688 log .debug ('Calling interactive_login' )
@@ -701,9 +700,12 @@ def interactive_login(self, user=None, password=None, force=False,
701700
702701 if not self .logged_in :
703702 raise BugzillaError ("Login with API_KEY failed" )
704- log .info ('API Key acepted ' )
703+ log .info ('API Key accepted ' )
705704
706- self ._save_api_key ()
705+ if self ._use_creds :
706+ _save_api_key (self .url , self .api_key )
707+ else :
708+ log .info ("API Key won't be updated because use_creds=False" )
707709 return
708710
709711 if not user :
0 commit comments