blob: 6eb8cb5d998eaa616d4d6780027dade116252298 [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
30async def local_async_exec(command: str
31 ) -> (int, str, str):
32 """
33 Executed a local command using asyncio.
34 TODO - do not know yet if return error code, and stdout and strerr or just one of them
35 """
tierno976d7eb2020-09-14 15:18:22 +000036 if isinstance(command, str):
37 scommand = split(command)
38 else # isinstance(command, (list, tuple)):
39 scommand = command
40
lloretgalleg1d2ff512020-08-01 06:05:58 +000041
42 logger.debug("Execute local command: {}".format(command))
43 process = await asyncio.create_subprocess_exec(
44 *scommand,
45 stdout=asyncio.subprocess.PIPE,
46 stderr=asyncio.subprocess.PIPE
47 )
48
49 # wait for command terminate
50 stdout, stderr = await process.communicate()
51
52 return_code = process.returncode
53 logger.debug("Return code: {}".format(return_code))
54
55 output = ""
56 if stdout:
57 output = stdout.decode()
58 logger.debug("Output: {}".format(output))
59
60 if stderr:
61 out_err = stderr.decode()
62 logger.debug("Stderr: {}".format(out_err))
63
64 return return_code, stdout, stderr