Gunes Bayir | 66b4a6a | 2023-07-01 22:55:42 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (c) 2023 Arm Limited. |
| 4 | # |
| 5 | # SPDX-License-Identifier: MIT |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | # of this software and associated documentation files (the "Software"), to |
| 9 | # deal in the Software without restriction, including without limitation the |
| 10 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | # sell copies of the Software, and to permit persons to whom the Software is |
| 12 | # furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in all |
| 15 | # copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | # SOFTWARE. |
| 24 | |
| 25 | import os |
| 26 | import logging |
| 27 | import subprocess |
| 28 | |
| 29 | logger = logging.getLogger("Shell") |
| 30 | |
| 31 | class Shell: |
| 32 | def __init__(self, is_interactive=False): |
| 33 | self.line="" |
| 34 | self.env=os.environ.copy() |
| 35 | self.initial_path = self.env["PATH"] |
| 36 | self.save_cwd() |
| 37 | self.is_interactive = is_interactive |
| 38 | |
| 39 | def reset_path(self): |
| 40 | self.env["PATH"]=self.initial_path |
| 41 | |
| 42 | def set_env(self, key, value): |
| 43 | self.env[key] = value |
| 44 | |
| 45 | def append_env(self, key, value): |
| 46 | logger.debug("Appending '%s' to '%s'" % (value, key)) |
| 47 | if key not in list(self.env.keys()): |
| 48 | self.set_env(key,value) |
| 49 | else: |
| 50 | self.env[key] += ":"+value |
| 51 | def prepend_env(self, key, value): |
| 52 | logger.debug("Prepending '%s' to '%s'" % (value, key)) |
| 53 | if key not in list(self.env.keys()): |
| 54 | self.set_env(key,value) |
| 55 | else: |
| 56 | self.env[key] = value+":"+self.env[key] |
| 57 | def run(self, cmd): |
| 58 | if isinstance(cmd, list): |
| 59 | for c in cmd: |
| 60 | self.run_single(c) |
| 61 | else: |
| 62 | self.run_single(cmd) |
| 63 | def run_to_str(self, cmd): |
| 64 | out = "" |
| 65 | if isinstance(cmd, list): |
| 66 | for c in cmd: |
| 67 | out += self.run_single_to_str(c) |
| 68 | else: |
| 69 | out = self.run_single_to_str(cmd) |
| 70 | return out |
| 71 | def cd(self, dirname): |
| 72 | os.chdir(dirname) |
| 73 | |
| 74 | def save_cwd(self): |
| 75 | self.cwd = os.getcwd() |
| 76 | |
| 77 | def restore_cwd(self): |
| 78 | self.cd( self.cwd ) |
| 79 | |
| 80 | def run_single_interactive(self,cmd): |
| 81 | subprocess.check_call(cmd, env=self.env,stderr=subprocess.STDOUT, shell=True) |
| 82 | logger.debug("%s returned" % cmd) |
| 83 | |
| 84 | def run_single(self,cmd): |
| 85 | if self.is_interactive: |
| 86 | self.run_single_interactive(cmd) |
| 87 | else: |
| 88 | self.run_single_to_str(cmd) |
| 89 | |
| 90 | def run_single_to_str_no_output_check(self,cmd): |
| 91 | try: |
| 92 | out = subprocess.check_output(cmd, env=self.env, stderr=subprocess.STDOUT, shell=True) |
| 93 | except subprocess.CalledProcessError as cpe: |
| 94 | out = cpe.output |
| 95 | if (len(out.strip()) > 0): |
| 96 | logger.debug(out) |
| 97 | logger.debug("%s returned" % cmd) |
| 98 | return out |
| 99 | |
| 100 | def run_single_to_str(self,cmd): |
| 101 | out = subprocess.check_output(cmd, env=self.env, stderr=subprocess.STDOUT, shell=True).decode('utf-8') |
| 102 | if (len(out.strip()) > 0): |
| 103 | logger.debug(out) |
| 104 | logger.debug("%s returned" % cmd) |
| 105 | return out |