blob: 915e97ffe0296ca0267d208510ea17c2375d62e3 [file] [log] [blame]
lloretgalleg1d2ff512020-08-01 06:05:58 +00001##
2# Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U.
3# This file is part of OSM
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15# implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# For those usages not covered by the Apache License, Version 2.0 please
20# contact with: nfvlabs@tid.es
21##
22
23import logging
24import asyncio
25from shlex import split
26
27logger = logging.getLogger("osm_ee.util")
28
29
garciadeblas43fc9352024-07-09 14:30:44 +020030async def local_async_exec(command: str) -> (int, str, str):
lloretgalleg1d2ff512020-08-01 06:05:58 +000031 """
garciadeblas43fc9352024-07-09 14:30:44 +020032 Executed a local command using asyncio.
33 TODO - do not know yet if return error code, and stdout and strerr or just one of them
lloretgalleg1d2ff512020-08-01 06:05:58 +000034 """
35 scommand = split(command)
36
37 logger.debug("Execute local command: {}".format(command))
38 process = await asyncio.create_subprocess_exec(
garciadeblas43fc9352024-07-09 14:30:44 +020039 *scommand, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
lloretgalleg1d2ff512020-08-01 06:05:58 +000040 )
41
42 # wait for command terminate
43 stdout, stderr = await process.communicate()
44
45 return_code = process.returncode
46 logger.debug("Return code: {}".format(return_code))
47
48 output = ""
49 if stdout:
50 output = stdout.decode()
51 logger.debug("Output: {}".format(output))
52
53 if stderr:
54 out_err = stderr.decode()
55 logger.debug("Stderr: {}".format(out_err))
56
57 return return_code, stdout, stderr