7
7
import socket
8
8
from xml .etree import ElementTree
9
9
10
+ try :
11
+ from ConfigParser import ConfigParser , NoSectionError
12
+ except ImportError :
13
+ from configparser import ConfigParser , NoSectionError
14
+
15
+
10
16
STATUSES_COLOR = {'blue' : {'symbol' : 'S' ,
11
17
'color' : '\033 [94m' ,
12
18
'descr' : 'Stable' },
@@ -87,12 +93,13 @@ class JenkinsCli(object):
87
93
"%s branch set to: %s" )
88
94
89
95
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 )
91
98
92
99
@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 ):
94
101
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 )
96
103
try :
97
104
host = host or settings_dict ['host' ]
98
105
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
102
109
return jenkins .Jenkins (host , username , password , timeout )
103
110
104
111
@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 )
109
119
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' ))
116
130
except Exception as e :
117
131
raise CliException ('Error reading %s: %s' % (filename , e ))
118
132
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 )
125
138
126
139
def run_command (self , args ):
127
140
command = args .jenkins_command
0 commit comments