• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2021-2025 Huawei Device Co., Ltd.
5# Licensed under the Apache License, Version 2.0 (the 'License');
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an 'AS IS' BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import subprocess
18from pathlib import Path
19from typing import List, TextIO, Union, Optional
20from abc import ABC, abstractmethod
21
22
23class CmdExecutor(ABC):
24    @abstractmethod
25    def run_command(self, command: List[str], stdout: Union[TextIO, int] = subprocess.PIPE) -> Union[str, int]:
26        pass
27
28    @abstractmethod
29    def get_binary(self, binary_name: str, version: Optional[str] = None) -> Union[str, Path]:
30        pass
31
32
33class LinuxCmdExecutor(CmdExecutor):
34    def run_command(self, command: List[str], stdout: Union[TextIO, int] = subprocess.PIPE) -> Union[str, int]:
35        return subprocess.run(command, check=True, text=True, stdout=stdout, timeout=5400).stdout
36
37    def get_binary(self, binary_name: str, version: Optional[str] = None) -> str:
38        return f"{binary_name}-{version}" if version else binary_name
39