-
Notifications
You must be signed in to change notification settings - Fork 454
/
async_utils.py
369 lines (308 loc) · 10.3 KB
/
async_utils.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later
# This file also includes the RateLimiter function with the following license:
#
# Copyright 2018 Quentin Pradet
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE."
""" Utility classes for the CVE Binary Tool """
from __future__ import annotations
import asyncio
import glob
import gzip
import itertools
import os
import shutil
import subprocess
import sys
import tempfile
import time
from functools import partial, wraps
from cve_bin_tool.util import inpath
def async_wrap(func):
"""
Wrapper to use synchronous functions in asynchronous context.
"""
@wraps(func)
async def run(*args, loop=None, executor=None, **kwargs):
"""
Takes a synchronous function and executes it using specified executor.
Parameters :
loop (optional, event loop): Event loop to be used.
executor (optional, executor): Executor for calling the synchronous function in.
"""
if loop is None:
loop = asyncio.get_event_loop()
pfunc = partial(func, *args, **kwargs)
return await loop.run_in_executor(executor, pfunc)
return run
def get_event_loop():
"""
Gets or creates an event loop.
"""
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
if sys.platform.startswith("win"):
if isinstance(loop, asyncio.SelectorEventLoop):
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
return loop
def run_coroutine(coro):
"""
Runs an asynchronous coroutine and returns its result.
"""
loop = get_event_loop()
aws = asyncio.ensure_future(coro, loop=loop)
result = loop.run_until_complete(aws)
return result
async def aio_run_command(args, process_can_fail=True):
"""
Asynchronously run a command in a subprocess and return its output, error and return code
Parameters :
process_can_fail (Optional, bool) : If False, non-zero return codes result in errors.
Returns :
stdout: The output of the subprocess.
stderr: The error of the subprocess.
returncode: The returncode of the subprocess
"""
process = await asyncio.create_subprocess_exec(
*args,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await process.communicate()
if process.returncode != 0 and not process_can_fail:
raise subprocess.CalledProcessError(
args, process.returncode, output=stdout, stderr=stderr
)
return stdout, stderr, process.returncode # binary encoded
class ChangeDirContext:
"""
Allows temporary changes in the current working directory.
Manages context to allow going to destination directory and return back to original.
"""
def __init__(self, destination_dir):
self.current_dir = os.getcwd()
self.destination_dir = destination_dir
async def __aenter__(self):
"""
Changes into specified destination directory.
"""
os.chdir(self.destination_dir)
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""
Revert changes to return to current working directory.
"""
os.chdir(self.current_dir)
class FileIO:
"""
Provides asynchronous methods for file operations
"""
_open = async_wrap(open)
_name_idx: int | None = 0
_mode_idx = 1
_mode = "r"
def __init__(self, *args, **kwargs):
# Do some trick to get exact filename and mode regardless of args or kwargs
flatargs = list(itertools.chain(args, kwargs.values()))
if (
self.__class__._name_idx is not None
and len(flatargs) > self.__class__._name_idx
):
self.name = flatargs[self.__class__._name_idx]
if (
self.__class__._mode_idx is not None
and len(flatargs) > self.__class__._mode_idx
):
self._mode = flatargs[self.__class__._mode_idx]
self._args = args
self._kwargs = kwargs
self._file = None
async def __call__(self):
"""Convenience method to allow call like following:
f = await FileIO("some file path", "r")()
Note: We can't make async __init__
"""
return await self.open()
async def open(self):
"""
Opens the file asynchronously.
"""
file = await self.__class__._open(*self._args, **self._kwargs)
self._file = file
self._setup()
return self
def _setup(self):
"""
Sets up the file object with asynchronous methods.
"""
if not self._file:
raise RuntimeError("Invalid Use: Call open() before calling _setup()")
common_async_attrs = {
"close",
"flush",
"isatty",
"read",
"readline",
"readlines",
"seek",
"tell",
"truncate",
"write",
"writelines",
}
common_sync_attrs = {
"detach",
"fileno",
"readable",
"writable",
"seekable",
"closed",
"mode",
"name",
}
strings_sync_attrs = {
"buffer",
"encoding",
"errors",
"line_buffering",
"newlines",
}
bytes_async_attrs = {"readinto", "readinto1", "read1"}
bytes_sync_attrs = {"raw"}
if "b" in self._mode:
async_attrs = common_async_attrs | bytes_async_attrs
sync_attrs = common_sync_attrs | bytes_sync_attrs
else:
async_attrs = common_async_attrs
sync_attrs = common_sync_attrs | strings_sync_attrs
[
setattr(self, attr, async_wrap(getattr(self._file, attr)))
for attr in async_attrs
if hasattr(self._file, attr)
]
[
setattr(self, attr, getattr(self._file, attr))
for attr in sync_attrs
if hasattr(self._file, attr)
]
async def __aenter__(self):
"""
Enters the asynchronous context.
"""
return await self.open()
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""
Exits the asynchronous context.
"""
return await self.close()
async def __anext__(self):
"""
Retrieves next line from the file asynchronously.
"""
line = await self.readline()
if line:
return line
else:
raise StopAsyncIteration
def __aiter__(self):
"""
Returns an asynchronous iterator for the file.
"""
return self
class TemporaryFile(FileIO):
"""
Asynchronous temporary FileIO wrapper.
"""
_open = async_wrap(tempfile.TemporaryFile)
_name_idx: int | None = None
_mode_idx = 0
_mode = "w+b"
def _setup(self):
"""
Sets up the temporary file.
"""
super()._setup()
self.name = self._file.name
class NamedTemporaryFile(TemporaryFile):
"""
Asynchronous Named Temporary File I/O Wrapper.
"""
_open = async_wrap(tempfile.NamedTemporaryFile)
class SpooledTemporaryFile(TemporaryFile):
"""
Asynchronous Spooled Temporary File I/O Wrapper.
"""
_open = async_wrap(tempfile.SpooledTemporaryFile)
_mode_idx = 1
class GzipFile(FileIO):
"""
Asynchronous Gzip File I/O Wrapper.
"""
_open = async_wrap(gzip.GzipFile)
class RateLimiter:
"""Rate limits an HTTP client that would make get() and post() calls.
Calls are rate-limited by host.
https://quentin.pradet.me/blog/how-do-you-rate-limit-calls-with-aiohttp.html
This class is not thread-safe.
Copyright 2018 Quentin Pradet
See license at top of file.
"""
RATE = 10
MAX_TOKENS = 10
def __init__(self, client):
self.client = client
self.tokens = self.MAX_TOKENS
self.updated_at = time.monotonic()
async def get(self, *args, **kwargs):
"""
Waits for a token then performs a get request."""
await self.wait_for_token()
return self.client.get(*args, **kwargs)
async def wait_for_token(self):
"""
Waits for a token to be available.
"""
while self.tokens < 1:
self.add_new_tokens()
await asyncio.sleep(0.1)
self.tokens -= 1
def add_new_tokens(self):
"""
Add new tokens if needed. Updates token count as required.
"""
now = time.monotonic()
time_since_update = now - self.updated_at
new_tokens = time_since_update * self.RATE
if self.tokens + new_tokens >= 1:
self.tokens = min(self.tokens + new_tokens, self.MAX_TOKENS)
self.updated_at = now
async def close(self):
"""
Closes the client connection.
"""
await self.client.close()
aio_rmdir = async_wrap(shutil.rmtree)
aio_rmfile = async_wrap(os.remove)
aio_unpack_archive = async_wrap(shutil.unpack_archive)
aio_glob = async_wrap(glob.glob)
aio_mkdtemp = async_wrap(tempfile.mkdtemp)
aio_makedirs = async_wrap(os.makedirs)
aio_inpath = async_wrap(inpath)