FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-gitlab/gitlab/config.py at main · cmarmo/python-gitlab · GitHub
cmarmo
python-gitlab
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-gitlab
/
gitlab
/
config.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
286 lines (233 loc) · 8.81 KB
Breadcrumbs
python-gitlab
/
gitlab
/
config.py
Copy path
File metadata and controls
286 lines (233 loc) · 8.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from
__future__
import
annotations
import
configparser
import
os
import
shlex
import
subprocess
from
os
.
path
import
expanduser
,
expandvars
from
pathlib
import
Path
from
gitlab
.
const
import
USER_AGENT
_DEFAULT_FILES
:
list
[
str
]
=
[
"/etc/python-gitlab.cfg"
,
str
(
Path
.
home
()
/
".python-gitlab.cfg"
),
]
HELPER_PREFIX
=
"helper:"
HELPER_ATTRIBUTES
=
[
"job_token"
,
"http_password"
,
"private_token"
,
"oauth_token"
]
_CONFIG_PARSER_ERRORS
=
(
configparser
.
NoOptionError
,
configparser
.
NoSectionError
)
def
_resolve_file
(
filepath
:
Path
|
str
)
->
str
:
resolved
=
Path
(
filepath
).
resolve
(
strict
=
True
)
return
str
(
resolved
)
def
_get_config_files
(
config_files
:
list
[
str
]
|
None
=
None
)
->
str
|
list
[
str
]:
"""
Return resolved path(s) to config files if they exist, with precedence:
1. Files passed in config_files
2. File defined in PYTHON_GITLAB_CFG
3. User- and system-wide config files
"""
resolved_files
=
[]
if
config_files
:
for
config_file
in
config_files
:
try
:
resolved
=
_resolve_file
(
config_file
)
except
OSError
as
e
:
raise
GitlabConfigMissingError
(
f"Cannot read config from file:
{
e
}
"
)
from
e
resolved_files
.
append
(
resolved
)
return
resolved_files
try
:
env_config
=
os
.
environ
[
"PYTHON_GITLAB_CFG"
]
return
_resolve_file
(
env_config
)
except
KeyError
:
pass
except
OSError
as
e
:
raise
GitlabConfigMissingError
(
f"Cannot read config from PYTHON_GITLAB_CFG:
{
e
}
"
)
from
e
for
config_file
in
_DEFAULT_FILES
:
try
:
resolved
=
_resolve_file
(
config_file
)
except
OSError
:
continue
resolved_files
.
append
(
resolved
)
return
resolved_files
class
ConfigError
(
Exception
):
pass
class
GitlabIDError
(
ConfigError
):
pass
class
GitlabDataError
(
ConfigError
):
pass
class
GitlabConfigMissingError
(
ConfigError
):
pass
class
GitlabConfigHelperError
(
ConfigError
):
pass
class
GitlabConfigParser
:
def
__init__
(
self
,
gitlab_id
:
str
|
None
=
None
,
config_files
:
list
[
str
]
|
None
=
None
)
->
None
:
self
.
gitlab_id
=
gitlab_id
self
.
http_username
:
str
|
None
=
None
self
.
http_password
:
str
|
None
=
None
self
.
job_token
:
str
|
None
=
None
self
.
oauth_token
:
str
|
None
=
None
self
.
private_token
:
str
|
None
=
None
self
.
api_version
:
str
=
"4"
self
.
order_by
:
str
|
None
=
None
self
.
pagination
:
str
|
None
=
None
self
.
per_page
:
int
|
None
=
None
self
.
retry_transient_errors
:
bool
=
False
self
.
ssl_verify
:
bool
|
str
=
True
self
.
timeout
:
int
=
60
self
.
url
:
str
|
None
=
None
self
.
user_agent
:
str
=
USER_AGENT
self
.
keep_base_url
:
bool
=
False
self
.
_files
=
_get_config_files
(
config_files
)
if
self
.
_files
:
self
.
_parse_config
()
if
self
.
gitlab_id
and
not
self
.
_files
:
raise
GitlabConfigMissingError
(
f"A gitlab id was provided (
{
self
.
gitlab_id
}
) but no config file found"
)
def
_parse_config
(
self
)
->
None
:
_config
=
configparser
.
ConfigParser
()
_config
.
read
(
self
.
_files
,
encoding
=
"utf-8"
)
if
self
.
gitlab_id
and
not
_config
.
has_section
(
self
.
gitlab_id
):
raise
GitlabDataError
(
f"A gitlab id was provided (
{
self
.
gitlab_id
}
) "
"but no config section found"
)
if
self
.
gitlab_id
is
None
:
try
:
self
.
gitlab_id
=
_config
.
get
(
"global"
,
"default"
)
except
Exception
as
e
:
raise
GitlabIDError
(
"Impossible to get the gitlab id (not specified in config file)"
)
from
e
try
:
self
.
url
=
_config
.
get
(
self
.
gitlab_id
,
"url"
)
except
Exception
as
e
:
raise
GitlabDataError
(
"Impossible to get gitlab details from "
f"configuration (
{
self
.
gitlab_id
}
)"
)
from
e
try
:
self
.
ssl_verify
=
_config
.
getboolean
(
"global"
,
"ssl_verify"
)
except
ValueError
:
# Value Error means the option exists but isn't a boolean.
# Get as a string instead as it should then be a local path to a
# CA bundle.
self
.
ssl_verify
=
_config
.
get
(
"global"
,
"ssl_verify"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
ssl_verify
=
_config
.
getboolean
(
self
.
gitlab_id
,
"ssl_verify"
)
except
ValueError
:
# Value Error means the option exists but isn't a boolean.
# Get as a string instead as it should then be a local path to a
# CA bundle.
self
.
ssl_verify
=
_config
.
get
(
self
.
gitlab_id
,
"ssl_verify"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
timeout
=
_config
.
getint
(
"global"
,
"timeout"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
timeout
=
_config
.
getint
(
self
.
gitlab_id
,
"timeout"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
private_token
=
_config
.
get
(
self
.
gitlab_id
,
"private_token"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
oauth_token
=
_config
.
get
(
self
.
gitlab_id
,
"oauth_token"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
job_token
=
_config
.
get
(
self
.
gitlab_id
,
"job_token"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
http_username
=
_config
.
get
(
self
.
gitlab_id
,
"http_username"
)
self
.
http_password
=
_config
.
get
(
self
.
gitlab_id
,
"http_password"
)
# pragma: no cover
except
_CONFIG_PARSER_ERRORS
:
pass
self
.
_get_values_from_helper
()
try
:
self
.
api_version
=
_config
.
get
(
"global"
,
"api_version"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
api_version
=
_config
.
get
(
self
.
gitlab_id
,
"api_version"
)
except
_CONFIG_PARSER_ERRORS
:
pass
if
self
.
api_version
not
in
(
"4"
,):
raise
GitlabDataError
(
f"Unsupported API version:
{
self
.
api_version
}
"
)
for
section
in
[
"global"
,
self
.
gitlab_id
]:
try
:
self
.
per_page
=
_config
.
getint
(
section
,
"per_page"
)
except
_CONFIG_PARSER_ERRORS
:
pass
if
self
.
per_page
is
not
None
and
not
0
<=
self
.
per_page
<=
100
:
raise
GitlabDataError
(
f"Unsupported per_page number:
{
self
.
per_page
}
"
)
try
:
self
.
pagination
=
_config
.
get
(
self
.
gitlab_id
,
"pagination"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
order_by
=
_config
.
get
(
self
.
gitlab_id
,
"order_by"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
user_agent
=
_config
.
get
(
"global"
,
"user_agent"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
user_agent
=
_config
.
get
(
self
.
gitlab_id
,
"user_agent"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
keep_base_url
=
_config
.
getboolean
(
"global"
,
"keep_base_url"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
keep_base_url
=
_config
.
getboolean
(
self
.
gitlab_id
,
"keep_base_url"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
retry_transient_errors
=
_config
.
getboolean
(
"global"
,
"retry_transient_errors"
)
except
_CONFIG_PARSER_ERRORS
:
pass
try
:
self
.
retry_transient_errors
=
_config
.
getboolean
(
self
.
gitlab_id
,
"retry_transient_errors"
)
except
_CONFIG_PARSER_ERRORS
:
pass
def
_get_values_from_helper
(
self
)
->
None
:
"""Update attributes that may get values from an external helper program"""
for
attr
in
HELPER_ATTRIBUTES
:
value
=
getattr
(
self
,
attr
)
if
not
isinstance
(
value
,
str
):
continue
if
not
value
.
lower
().
strip
().
startswith
(
HELPER_PREFIX
):
continue
helper
=
value
[
len
(
HELPER_PREFIX
) :].
strip
()
commmand
=
[
expanduser
(
expandvars
(
token
))
for
token
in
shlex
.
split
(
helper
)]
try
:
value
=
(
subprocess
.
check_output
(
commmand
,
stderr
=
subprocess
.
PIPE
)
.
decode
(
"utf-8"
)
.
strip
()
)
except
subprocess
.
CalledProcessError
as
e
:
stderr
=
e
.
stderr
.
decode
().
strip
()
raise
GitlabConfigHelperError
(
f"Failed to read
{
attr
}
value from helper "
f"for
{
self
.
gitlab_id
}
:
\n
{
stderr
}
"
)
from
e
setattr
(
self
,
attr
,
value
)
Back
|
FazBrowse Home
|
New Git URL