2015-01-26 11:10:30 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-05-31 19:08:41 +00:00
|
|
|
import functools
|
2015-01-26 11:10:30 +00:00
|
|
|
import asyncio
|
2015-03-04 15:01:56 +00:00
|
|
|
import sys
|
2015-10-11 21:50:50 +00:00
|
|
|
import os
|
2018-01-29 09:18:13 +00:00
|
|
|
import threading
|
|
|
|
|
2015-01-26 11:10:30 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def wait_run_in_executor(func, *args, **kwargs):
|
2015-01-26 11:10:30 +00:00
|
|
|
"""
|
|
|
|
Run blocking code in a different thread and wait
|
2015-02-09 01:10:04 +00:00
|
|
|
for the result.
|
2015-01-26 11:10:30 +00:00
|
|
|
|
|
|
|
:param func: Run this function in a different thread
|
|
|
|
:param args: Parameters of the function
|
2016-05-31 19:08:41 +00:00
|
|
|
:param kwargs: Keyword parameters of the function
|
2015-01-26 11:10:30 +00:00
|
|
|
:returns: Return the result of the function
|
|
|
|
"""
|
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
2016-05-31 19:08:41 +00:00
|
|
|
future = loop.run_in_executor(None, functools.partial(func, *args, **kwargs))
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.wait([future])
|
2015-01-26 11:10:30 +00:00
|
|
|
return future.result()
|
2015-02-16 16:20:07 +00:00
|
|
|
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def cancellable_wait_run_in_executor(func, *args, **kwargs):
|
2018-01-29 09:18:13 +00:00
|
|
|
"""
|
|
|
|
Run blocking code in a different thread and wait
|
|
|
|
for the result. Support cancellation.
|
|
|
|
|
|
|
|
:param func: Run this function in a different thread
|
|
|
|
:param args: Parameters of the function
|
|
|
|
:param kwargs: Keyword parameters of the function
|
|
|
|
:returns: Return the result of the function
|
|
|
|
"""
|
|
|
|
stopped_event = threading.Event()
|
2021-04-13 09:16:50 +00:00
|
|
|
kwargs["stopped_event"] = stopped_event
|
2018-01-29 09:18:13 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await wait_run_in_executor(func, *args, **kwargs)
|
2019-11-11 04:44:31 +00:00
|
|
|
except asyncio.CancelledError:
|
2018-01-29 09:18:13 +00:00
|
|
|
stopped_event.set()
|
|
|
|
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def subprocess_check_output(*args, cwd=None, env=None, stderr=False):
|
2015-02-16 16:20:07 +00:00
|
|
|
"""
|
|
|
|
Run a command and capture output
|
|
|
|
|
|
|
|
:param *args: List of command arguments
|
2015-02-18 14:18:18 +00:00
|
|
|
:param cwd: Current working directory
|
2015-02-16 16:20:07 +00:00
|
|
|
:param env: Command environment
|
2017-10-12 15:32:45 +00:00
|
|
|
:param stderr: Read on stderr
|
2015-02-16 16:20:07 +00:00
|
|
|
:returns: Command output
|
|
|
|
"""
|
|
|
|
|
2017-10-12 15:32:45 +00:00
|
|
|
if stderr:
|
2018-10-15 10:05:49 +00:00
|
|
|
proc = await asyncio.create_subprocess_exec(*args, stderr=asyncio.subprocess.PIPE, cwd=cwd, env=env)
|
2023-05-14 05:58:50 +00:00
|
|
|
_, output = await proc.communicate()
|
2017-10-12 15:32:45 +00:00
|
|
|
else:
|
2023-01-16 10:04:46 +00:00
|
|
|
proc = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL, cwd=cwd, env=env)
|
2023-05-14 05:58:50 +00:00
|
|
|
output, _ = await proc.communicate()
|
2015-02-16 16:20:07 +00:00
|
|
|
if output is None:
|
|
|
|
return ""
|
2015-06-16 13:46:12 +00:00
|
|
|
# If we received garbage we ignore invalid characters
|
2016-06-10 18:36:41 +00:00
|
|
|
# it should happens only when user try to use another binary
|
2015-06-16 13:46:12 +00:00
|
|
|
# and the code of VPCS, dynamips... Will detect it's not the correct binary
|
|
|
|
return output.decode("utf-8", errors="ignore")
|
2015-02-26 10:29:57 +00:00
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def wait_for_process_termination(process, timeout=10):
|
2015-02-26 10:29:57 +00:00
|
|
|
"""
|
|
|
|
Wait for a process terminate, and raise asyncio.TimeoutError in case of
|
|
|
|
timeout.
|
|
|
|
|
|
|
|
In theory this can be implemented by just:
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.wait_for(self._iou_process.wait(), timeout=100)
|
2015-02-26 10:29:57 +00:00
|
|
|
|
|
|
|
But it's broken before Python 3.4:
|
|
|
|
http://bugs.python.org/issue23140
|
|
|
|
|
|
|
|
:param process: An asyncio subprocess
|
|
|
|
:param timeout: Timeout in seconds
|
|
|
|
"""
|
|
|
|
|
2017-01-31 12:43:05 +00:00
|
|
|
if sys.version_info >= (3, 5):
|
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.wait_for(process.wait(), timeout=timeout)
|
2017-01-31 12:43:05 +00:00
|
|
|
except ProcessLookupError:
|
|
|
|
return
|
2016-11-17 17:09:46 +00:00
|
|
|
else:
|
|
|
|
while timeout > 0:
|
|
|
|
if process.returncode is not None:
|
|
|
|
return
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.sleep(0.1)
|
2016-11-17 17:09:46 +00:00
|
|
|
timeout -= 0.1
|
|
|
|
raise asyncio.TimeoutError()
|
2015-03-04 15:01:56 +00:00
|
|
|
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _check_process(process, termination_callback):
|
2015-03-04 15:01:56 +00:00
|
|
|
if not hasattr(sys, "_called_from_test") or not sys._called_from_test:
|
2018-10-15 10:05:49 +00:00
|
|
|
returncode = await process.wait()
|
2015-03-04 15:01:56 +00:00
|
|
|
if asyncio.iscoroutinefunction(termination_callback):
|
2018-10-15 10:05:49 +00:00
|
|
|
await termination_callback(returncode)
|
2015-03-04 15:01:56 +00:00
|
|
|
else:
|
|
|
|
termination_callback(returncode)
|
|
|
|
|
|
|
|
|
|
|
|
def monitor_process(process, termination_callback):
|
2015-05-13 19:53:42 +00:00
|
|
|
"""Call termination_callback when a process dies"""
|
2015-03-04 15:01:56 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
asyncio.ensure_future(_check_process(process, termination_callback))
|
2015-10-11 21:50:50 +00:00
|
|
|
|
|
|
|
|
2020-06-10 11:07:13 +00:00
|
|
|
async def wait_for_file_creation(path, timeout=60):
|
2015-10-11 21:50:50 +00:00
|
|
|
|
|
|
|
while timeout > 0:
|
|
|
|
if os.path.exists(path):
|
|
|
|
return
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.sleep(0.5)
|
2015-10-11 21:50:50 +00:00
|
|
|
timeout -= 0.5
|
|
|
|
raise asyncio.TimeoutError()
|
2015-11-02 01:51:12 +00:00
|
|
|
|
|
|
|
|
2018-08-25 07:10:47 +00:00
|
|
|
def locking(f):
|
|
|
|
@functools.wraps(f)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def wrapper(oself, *args, **kwargs):
|
2018-08-25 07:10:47 +00:00
|
|
|
lock_name = "__" + f.__name__ + "_lock"
|
|
|
|
if not hasattr(oself, lock_name):
|
|
|
|
setattr(oself, lock_name, asyncio.Lock())
|
2018-10-15 10:05:49 +00:00
|
|
|
async with getattr(oself, lock_name):
|
|
|
|
return await f(oself, *args, **kwargs)
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2018-08-25 07:10:47 +00:00
|
|
|
return wrapper
|