-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.py
More file actions
222 lines (175 loc) · 5.75 KB
/
utils.py
File metadata and controls
222 lines (175 loc) · 5.75 KB
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
#
# Copyright 2025 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Common utilities."""
import datetime
import logging
import os
import signal
import time
import traceback
from functools import wraps
from typing import Any, Callable, List, Tuple, Union
from urllib import parse as urlparse
__all__ = [
"handle_teardown_signals",
"datetime_to_seconds",
"is_true",
"is_false",
"retry",
"extract_http_scheme_host_port",
"remove_http_proxy_env_vars",
]
def remove_http_proxy_env_vars() -> None:
"""Removes HTTP(s) proxies from environment variables.
Removes the following environment variables:
* http_proxy
* https_proxy
* HTTP_PROXY
* HTTPS_PROXY
This function can be used in Splunk modular inputs code before starting the
ingestion to ensure that no proxy is going to be used when doing requests.
In case of proxy is needed, it can be defined in the modular inputs code.
"""
env_vars_to_remove = (
"http_proxy",
"https_proxy",
"HTTP_PROXY",
"HTTPS_PROXY",
)
for env_var in env_vars_to_remove:
if env_var in os.environ:
del os.environ[env_var]
def handle_teardown_signals(callback: Callable):
"""Register handler for SIGTERM/SIGINT/SIGBREAK signal.
Catch SIGTERM/SIGINT/SIGBREAK signals, and invoke callback
Note: this should be called in main thread since Python only catches
signals in main thread.
Arguments:
callback: Callback for tear down signals.
"""
signal.signal(signal.SIGTERM, callback)
signal.signal(signal.SIGINT, callback)
if os.name == "nt":
signal.signal(signal.SIGBREAK, callback)
def datetime_to_seconds(dt: datetime.datetime) -> float:
"""Convert UTC datetime to seconds since epoch.
Arguments:
dt: Date time.
Returns:
Seconds since epoch.
"""
epoch_time = datetime.datetime.utcfromtimestamp(0)
return (dt - epoch_time).total_seconds()
def is_true(val: Union[str, int]) -> bool:
"""Decide if `val` is true.
Arguments:
val: Value to check.
Returns:
True or False.
"""
value = str(val).strip().upper()
if value in ("1", "TRUE", "T", "Y", "YES"):
return True
return False
def is_false(val: Union[str, int]) -> bool:
"""Decide if `val` is false.
Arguments:
val: Value to check.
Returns:
True or False.
"""
value = str(val).strip().upper()
if value in ("0", "FALSE", "F", "N", "NO", "NONE", ""):
return True
return False
def retry(
retries: int = 3,
reraise: bool = True,
default_return: Any = None,
exceptions: List = None,
):
"""A decorator to run function with max `retries` times if there is
exception.
Arguments:
retries: (optional) Max retries times, default is 3.
reraise: Whether exception should be reraised, default is True.
default_return: (optional) Default return value for function
run after max retries and reraise is False.
exceptions: (optional) List of exceptions that should retry.
"""
max_tries = max(retries, 0) + 1
def do_retry(func):
@wraps(func)
def wrapper(*args, **kwargs):
last_ex = None
for i in range(max_tries):
try:
return func(*args, **kwargs)
except Exception as e:
logging.warning(
"Run function: %s failed: %s.",
func.__name__,
traceback.format_exc(),
)
if not exceptions or any(
isinstance(e, exception) for exception in exceptions
):
last_ex = e
if i < max_tries - 1:
time.sleep(2**i)
else:
raise
if reraise:
raise last_ex
else:
return default_return
return wrapper
return do_retry
def extract_http_scheme_host_port(http_url: str) -> Tuple:
"""Extract scheme, host and port from a HTTP URL.
Arguments:
http_url: HTTP URL to extract.
Returns:
A tuple of scheme, host and port
Raises:
ValueError: If `http_url` is not in http(s)://hostname:port format.
"""
http_info = urlparse.urlparse(http_url)
if not http_info.scheme or not http_info.hostname or not http_info.port:
raise ValueError(http_url + " is not in http(s)://hostname:port format")
return http_info.scheme, http_info.hostname, http_info.port
def get_appname_from_path(absolute_path):
"""Gets name of the app from its path.
Arguments:
absolute_path: path of app
Returns:
"""
absolute_path = os.path.normpath(absolute_path)
parts = absolute_path.split(os.path.sep)
parts.reverse()
for key in ("apps", "peer-apps", "manager-apps"):
try:
idx = parts.index(key)
except ValueError:
continue
else:
try:
if parts[idx + 1] == "etc":
return parts[idx - 1]
except IndexError:
pass
continue
return "-"