20
20
import shlex
21
21
import subprocess
22
22
from os .path import expanduser , expandvars
23
+ from pathlib import Path
23
24
from typing import List , Optional , Union
24
25
25
- from gitlab .const import USER_AGENT
26
+ from gitlab .const import DEFAULT_URL , USER_AGENT
26
27
27
-
28
- def _env_config () -> List [str ]:
29
- if "PYTHON_GITLAB_CFG" in os .environ :
30
- return [os .environ ["PYTHON_GITLAB_CFG" ]]
31
- return []
32
-
33
-
34
- _DEFAULT_FILES : List [str ] = _env_config () + [
28
+ _DEFAULT_FILES : List [str ] = [
35
29
"/etc/python-gitlab.cfg" ,
36
- os . path . expanduser ( "~/ .python-gitlab.cfg" ),
30
+ str ( Path . home () / " .python-gitlab.cfg" ),
37
31
]
38
32
39
33
HELPER_PREFIX = "helper:"
40
34
41
35
HELPER_ATTRIBUTES = ["job_token" , "http_password" , "private_token" , "oauth_token" ]
42
36
43
37
38
+ def _resolve_file (filepath : Union [Path , str ]) -> str :
39
+ resolved = Path (filepath ).resolve (strict = True )
40
+ return str (resolved )
41
+
42
+
43
+ def _get_config_files (
44
+ config_files : Optional [List [str ]] = None ,
45
+ ) -> Union [str , List [str ]]:
46
+ if config_files :
47
+ return config_files
48
+
49
+ try :
50
+ env_config = os .environ ["PYTHON_GITLAB_CFG" ]
51
+ return _resolve_file (env_config )
52
+ except KeyError :
53
+ pass
54
+ except OSError as e :
55
+ raise GitlabConfigMissingError (
56
+ f"Cannot read config from PYTHON_GITLAB_CFG in " f"{ env_config } : { e } "
57
+ )
58
+
59
+ default_files = []
60
+
61
+ for config_file in _DEFAULT_FILES :
62
+ try :
63
+ resolved = _resolve_file (config_file )
64
+ except OSError :
65
+ continue
66
+ default_files .append (resolved )
67
+
68
+ return default_files
69
+
70
+
44
71
class ConfigError (Exception ):
45
72
pass
46
73
@@ -66,155 +93,149 @@ def __init__(
66
93
self , gitlab_id : Optional [str ] = None , config_files : Optional [List [str ]] = None
67
94
) -> None :
68
95
self .gitlab_id = gitlab_id
69
- _files = config_files or _DEFAULT_FILES
70
- file_exist = False
71
- for file in _files :
72
- if os .path .exists (file ):
73
- file_exist = True
74
- if not file_exist :
75
- raise GitlabConfigMissingError (
76
- "Config file not found. \n Please create one in "
77
- "one of the following locations: {} \n or "
78
- "specify a config file using the '-c' parameter." .format (
79
- ", " .join (_DEFAULT_FILES )
80
- )
81
- )
96
+ self .http_username : Optional [str ] = None
97
+ self .http_password : Optional [str ] = None
98
+ self .job_token : Optional [str ] = None
99
+ self .oauth_token : Optional [str ] = None
100
+ self .private_token : Optional [str ] = None
101
+
102
+ self .api_version : str = "4"
103
+ self .order_by : Optional [str ] = None
104
+ self .pagination : Optional [str ] = None
105
+ self .per_page : Optional [int ] = None
106
+ self .retry_transient_errors : bool = False
107
+ self .ssl_verify : Union [bool , str ] = True
108
+ self .timeout : int = 60
109
+ self .url : str = DEFAULT_URL
110
+ self .user_agent : str = USER_AGENT
82
111
83
- self ._config = configparser .ConfigParser ()
84
- self ._config .read (_files )
112
+ self ._files = _get_config_files (config_files )
113
+ if self ._files :
114
+ self ._parse_config ()
115
+
116
+ def _parse_config (self ) -> None :
117
+ _config = configparser .ConfigParser ()
118
+ _config .read (self ._files )
85
119
86
120
if self .gitlab_id is None :
87
121
try :
88
- self .gitlab_id = self . _config .get ("global" , "default" )
122
+ self .gitlab_id = _config .get ("global" , "default" )
89
123
except Exception as e :
90
124
raise GitlabIDError (
91
125
"Impossible to get the gitlab id (not specified in config file)"
92
126
) from e
93
127
94
128
try :
95
- self .url = self . _config .get (self .gitlab_id , "url" )
129
+ self .url = _config .get (self .gitlab_id , "url" )
96
130
except Exception as e :
97
131
raise GitlabDataError (
98
132
"Impossible to get gitlab details from "
99
133
f"configuration ({ self .gitlab_id } )"
100
134
) from e
101
135
102
- self .ssl_verify : Union [bool , str ] = True
103
136
try :
104
- self .ssl_verify = self . _config .getboolean ("global" , "ssl_verify" )
137
+ self .ssl_verify = _config .getboolean ("global" , "ssl_verify" )
105
138
except ValueError :
106
139
# Value Error means the option exists but isn't a boolean.
107
140
# Get as a string instead as it should then be a local path to a
108
141
# CA bundle.
109
142
try :
110
- self .ssl_verify = self . _config .get ("global" , "ssl_verify" )
143
+ self .ssl_verify = _config .get ("global" , "ssl_verify" )
111
144
except Exception :
112
145
pass
113
146
except Exception :
114
147
pass
115
148
try :
116
- self .ssl_verify = self . _config .getboolean (self .gitlab_id , "ssl_verify" )
149
+ self .ssl_verify = _config .getboolean (self .gitlab_id , "ssl_verify" )
117
150
except ValueError :
118
151
# Value Error means the option exists but isn't a boolean.
119
152
# Get as a string instead as it should then be a local path to a
120
153
# CA bundle.
121
154
try :
122
- self .ssl_verify = self . _config .get (self .gitlab_id , "ssl_verify" )
155
+ self .ssl_verify = _config .get (self .gitlab_id , "ssl_verify" )
123
156
except Exception :
124
157
pass
125
158
except Exception :
126
159
pass
127
160
128
- self .timeout = 60
129
161
try :
130
- self .timeout = self . _config .getint ("global" , "timeout" )
162
+ self .timeout = _config .getint ("global" , "timeout" )
131
163
except Exception :
132
164
pass
133
165
try :
134
- self .timeout = self . _config .getint (self .gitlab_id , "timeout" )
166
+ self .timeout = _config .getint (self .gitlab_id , "timeout" )
135
167
except Exception :
136
168
pass
137
169
138
- self .private_token = None
139
170
try :
140
- self .private_token = self . _config .get (self .gitlab_id , "private_token" )
171
+ self .private_token = _config .get (self .gitlab_id , "private_token" )
141
172
except Exception :
142
173
pass
143
174
144
- self .oauth_token = None
145
175
try :
146
- self .oauth_token = self . _config .get (self .gitlab_id , "oauth_token" )
176
+ self .oauth_token = _config .get (self .gitlab_id , "oauth_token" )
147
177
except Exception :
148
178
pass
149
179
150
- self .job_token = None
151
180
try :
152
- self .job_token = self . _config .get (self .gitlab_id , "job_token" )
181
+ self .job_token = _config .get (self .gitlab_id , "job_token" )
153
182
except Exception :
154
183
pass
155
184
156
- self .http_username = None
157
- self .http_password = None
158
185
try :
159
- self .http_username = self . _config .get (self .gitlab_id , "http_username" )
160
- self .http_password = self . _config .get (self .gitlab_id , "http_password" )
186
+ self .http_username = _config .get (self .gitlab_id , "http_username" )
187
+ self .http_password = _config .get (self .gitlab_id , "http_password" )
161
188
except Exception :
162
189
pass
163
190
164
191
self ._get_values_from_helper ()
165
192
166
- self .api_version = "4"
167
193
try :
168
- self .api_version = self . _config .get ("global" , "api_version" )
194
+ self .api_version = _config .get ("global" , "api_version" )
169
195
except Exception :
170
196
pass
171
197
try :
172
- self .api_version = self . _config .get (self .gitlab_id , "api_version" )
198
+ self .api_version = _config .get (self .gitlab_id , "api_version" )
173
199
except Exception :
174
200
pass
175
201
if self .api_version not in ("4" ,):
176
202
raise GitlabDataError (f"Unsupported API version: { self .api_version } " )
177
203
178
- self .per_page = None
179
204
for section in ["global" , self .gitlab_id ]:
180
205
try :
181
- self .per_page = self . _config .getint (section , "per_page" )
206
+ self .per_page = _config .getint (section , "per_page" )
182
207
except Exception :
183
208
pass
184
209
if self .per_page is not None and not 0 <= self .per_page <= 100 :
185
210
raise GitlabDataError (f"Unsupported per_page number: { self .per_page } " )
186
211
187
- self .pagination = None
188
212
try :
189
- self .pagination = self . _config .get (self .gitlab_id , "pagination" )
213
+ self .pagination = _config .get (self .gitlab_id , "pagination" )
190
214
except Exception :
191
215
pass
192
216
193
- self .order_by = None
194
217
try :
195
- self .order_by = self . _config .get (self .gitlab_id , "order_by" )
218
+ self .order_by = _config .get (self .gitlab_id , "order_by" )
196
219
except Exception :
197
220
pass
198
221
199
- self .user_agent = USER_AGENT
200
222
try :
201
- self .user_agent = self . _config .get ("global" , "user_agent" )
223
+ self .user_agent = _config .get ("global" , "user_agent" )
202
224
except Exception :
203
225
pass
204
226
try :
205
- self .user_agent = self . _config .get (self .gitlab_id , "user_agent" )
227
+ self .user_agent = _config .get (self .gitlab_id , "user_agent" )
206
228
except Exception :
207
229
pass
208
230
209
- self .retry_transient_errors = False
210
231
try :
211
- self .retry_transient_errors = self . _config .getboolean (
232
+ self .retry_transient_errors = _config .getboolean (
212
233
"global" , "retry_transient_errors"
213
234
)
214
235
except Exception :
215
236
pass
216
237
try :
217
- self .retry_transient_errors = self . _config .getboolean (
238
+ self .retry_transient_errors = _config .getboolean (
218
239
self .gitlab_id , "retry_transient_errors"
219
240
)
220
241
except Exception :
0 commit comments