Leonardo Sandoval | 314eed8 | 2020-08-05 13:32:04 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | import os |
| 9 | import subprocess |
| 10 | import sys |
| 11 | import textwrap |
| 12 | |
| 13 | |
| 14 | def dir_is_ignored(relative_path, ignored_folders): |
| 15 | '''Checks if a directory is on the ignore list or inside one of the ignored |
| 16 | directories. relative_path mustn't end in "/".''' |
| 17 | |
| 18 | # Check if directory is in ignore list |
| 19 | if relative_path in ignored_folders: |
| 20 | return True |
| 21 | |
| 22 | # Check if directory is a subdirectory of one in ignore list |
| 23 | return (relative_path + '/').startswith(ignored_folders) |
| 24 | |
| 25 | |
| 26 | def file_is_ignored(relative_path, valid_file_extensions, ignored_files, ignored_folders): |
| 27 | '''Checks if a file is ignored based on its folder, name and extension.''' |
| 28 | if not relative_path.endswith(valid_file_extensions): |
| 29 | return True |
| 30 | |
| 31 | if relative_path in ignored_files: |
| 32 | return True |
| 33 | |
| 34 | return dir_is_ignored(os.path.dirname(relative_path), ignored_folders) |
| 35 | |
| 36 | |
| 37 | def print_exception_info(): |
| 38 | '''Print some information about the cause of an exception.''' |
| 39 | print("ERROR: Exception:") |
| 40 | print(textwrap.indent(str(sys.exc_info()[0])," ")) |
| 41 | print(textwrap.indent(str(sys.exc_info()[1])," ")) |
| 42 | |
| 43 | |
| 44 | def decode_string(string, encoding='utf-8'): |
| 45 | '''Tries to decode a binary string. It gives an error if it finds |
| 46 | invalid characters, but it will return the string converted anyway, |
| 47 | ignoring these characters.''' |
| 48 | try: |
| 49 | string = string.decode(encoding) |
| 50 | except UnicodeDecodeError: |
| 51 | # Capture exceptions caused by invalid characters. |
| 52 | print("ERROR:Non-{} characters detected.".format(encoding.upper())) |
| 53 | print_exception_info() |
| 54 | string = string.decode(encoding, "ignore") |
| 55 | |
| 56 | return string |
| 57 | |
| 58 | |
| 59 | def shell_command(cmd_line): |
| 60 | '''Executes a shell command. Returns (returncode, stdout, stderr), where |
| 61 | stdout and stderr are strings.''' |
| 62 | |
| 63 | try: |
| 64 | p = subprocess.Popen(cmd_line, stdout=subprocess.PIPE, |
| 65 | stderr=subprocess.PIPE) |
| 66 | (stdout, stderr) = p.communicate() |
| 67 | # No need for p.wait(), p.communicate() does it by default. |
| 68 | except: |
| 69 | print("ERROR: Shell command: ", end="") |
| 70 | print(cmd_line) |
| 71 | print_exception_info() |
| 72 | return (1, None, None) |
| 73 | |
| 74 | stdout = decode_string(stdout) |
| 75 | stderr = decode_string(stderr) |
| 76 | |
| 77 | if p.returncode != 0: |
| 78 | print("ERROR: Shell command failed:") |
| 79 | print(textwrap.indent(str(cmd_line)," ")) |
| 80 | print("ERROR: stdout:") |
| 81 | print(textwrap.indent(stdout," ")) |
| 82 | print("ERROR: stderr:") |
| 83 | print(textwrap.indent(stderr," ")) |
| 84 | |
| 85 | return (p.returncode, stdout, stderr) |
| 86 | |