blob: 548b64eaa8eaf4061d403a79c3c47324b3b422b0 [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/usr/bin/env python3
2#
Zelalem219df412020-05-17 19:21:20 -05003# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8import os
9import subprocess
10import sys
11import textwrap
12
13
14def 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
26def 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
37def 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
Zelalem219df412020-05-17 19:21:20 -050044def 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.'''
Fathi Boudra422bf772019-12-02 11:10:16 +020048 try:
Zelalem219df412020-05-17 19:21:20 -050049 string = string.decode(encoding)
Fathi Boudra422bf772019-12-02 11:10:16 +020050 except UnicodeDecodeError:
Zelalem219df412020-05-17 19:21:20 -050051 # Capture exceptions caused by invalid characters.
52 print("ERROR:Non-{} characters detected.".format(encoding.upper()))
Fathi Boudra422bf772019-12-02 11:10:16 +020053 print_exception_info()
Zelalem219df412020-05-17 19:21:20 -050054 string = string.decode(encoding, "ignore")
Fathi Boudra422bf772019-12-02 11:10:16 +020055
56 return string
57
58
59def shell_command(cmd_line):
60 '''Executes a shell command. Returns (returncode, stdout, stderr), where
Zelalem219df412020-05-17 19:21:20 -050061 stdout and stderr are strings.'''
Fathi Boudra422bf772019-12-02 11:10:16 +020062
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