blob: 1d0a2325615d8b54986c946ae93a41afbd5f36b5 [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 """
36 scommand = split(command)
37
38 logger.debug("Execute local command: {}".format(command))
39 process = await asyncio.create_subprocess_exec(
40 *scommand,
41 stdout=asyncio.subprocess.PIPE,
42 stderr=asyncio.subprocess.PIPE
43 )
44
45 # wait for command terminate
46 stdout, stderr = await process.communicate()
47
48 return_code = process.returncode
49 logger.debug("Return code: {}".format(return_code))
50
51 output = ""
52 if stdout:
53 output = stdout.decode()
54 logger.debug("Output: {}".format(output))
55
56 if stderr:
57 out_err = stderr.decode()
58 logger.debug("Stderr: {}".format(out_err))
59
60 return return_code, stdout, stderr