77import socket
88from xml .etree import ElementTree
99
10+ try :
11+ from ConfigParser import ConfigParser , NoSectionError
12+ except ImportError :
13+ from configparser import ConfigParser , NoSectionError
14+
15+
1016STATUSES_COLOR = {'blue' : {'symbol' : 'S' ,
1117 'color' : '\033 [94m' ,
1218 'descr' : 'Stable' },
@@ -87,12 +93,13 @@ class JenkinsCli(object):
8793 "%s branch set to: %s" )
8894
8995 def __init__ (self , args , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
90- self .jenkins = self .auth (args .host , args .username , args .password , timeout )
96+ self .jenkins = self .auth (args .host , args .username , args .password ,
97+ args .environment , timeout )
9198
9299 @classmethod
93- def auth (cls , host = None , username = None , password = None , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
100+ def auth (cls , host = None , username = None , password = None , environment = None , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT ):
94101 if host is None or username is None or password is None :
95- settings_dict = cls .read_settings_from_file ()
102+ settings_dict = cls .read_settings_from_file (environment )
96103 try :
97104 host = host or settings_dict ['host' ]
98105 username = username or settings_dict .get ('username' , None )
@@ -102,26 +109,32 @@ def auth(cls, host=None, username=None, password=None, timeout=socket._GLOBAL_DE
102109 return jenkins .Jenkins (host , username , password , timeout )
103110
104111 @classmethod
105- def read_settings_from_file (cls ):
106- try :
107- current_folder = os .getcwd ()
108- filename = os .path .join (current_folder , cls .SETTINGS_FILE_NAME )
112+ def read_settings_from_file (cls , environment ):
113+ # get config filename
114+ current_folder = os .getcwd ()
115+ filename = os .path .join (current_folder , cls .SETTINGS_FILE_NAME )
116+ if not os .path .exists (filename ):
117+ home_folder = os .path .expanduser ("~" )
118+ filename = os .path .join (home_folder , cls .SETTINGS_FILE_NAME )
109119 if not os .path .exists (filename ):
110- home_folder = os .path .expanduser ("~" )
111- filename = os .path .join (home_folder , cls .SETTINGS_FILE_NAME )
112- if not os .path .exists (filename ):
113- return {}
114- f = open (filename , 'r' )
115- jenkins_settings = f .read ()
120+ return {}
121+
122+ # use the DEFAULT section if no env is specified
123+ if not environment :
124+ environment = 'DEFAULT'
125+
126+ # read the config file
127+ config = ConfigParser ()
128+ try :
129+ config .readfp (open (filename , 'r' ))
116130 except Exception as e :
117131 raise CliException ('Error reading %s: %s' % (filename , e ))
118132
119- settings_dict = {}
120- for setting_line in jenkins_settings .split ('\n ' ):
121- if "=" in setting_line :
122- key , value = setting_line .split ("=" , 1 )
123- settings_dict [key .strip ()] = value .strip ()
124- return settings_dict
133+ # return the variables as dict
134+ try :
135+ return dict (config .items (environment ))
136+ except NoSectionError :
137+ raise CliException ('No such environment: %s' % environment )
125138
126139 def run_command (self , args ):
127140 command = args .jenkins_command
0 commit comments