File: os_utils.py

package info (click to toggle)
clazy 1.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,248 kB
  • sloc: cpp: 23,552; python: 1,450; xml: 450; sh: 237; makefile: 46
file content (105 lines) | stat: -rw-r--r-- 3,543 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import io
import os
import subprocess


def compare_files(expects_failure, expected_file, result_file, message, verbose: bool):
    success = files_are_equal(expected_file, result_file)

    if expects_failure:
        if success:
            print("[XOK]   " + message)
            return False
        else:
            print("[XFAIL] " + message)
            print_differences(expected_file, result_file, verbose)
            return True
    else:
        if success:
            print("[OK]   " + message)
            return True
        else:
            print("[FAIL] " + message)
            print_differences(expected_file, result_file, verbose)
            return False



def run_command(cmd, output_file="", test_env=os.environ, cwd=None, verbose = False, ignore_verbose_command=False, qt_namespaced=False, qt_replace_namespace=True):
    lines, success = get_command_output(cmd, test_env=test_env, verbose=verbose, cwd=cwd, ignore_verbose=ignore_verbose_command)
    # Hack for Windows, we have std::_Vector_base in the expected data
    lines = lines.replace("std::_Container_base0", "std::_Vector_base")
    lines = lines.replace("std::__1::__vector_base_common",
                          "std::_Vector_base")  # Hack for macOS
    lines = lines.replace("std::_Vector_alloc", "std::_Vector_base")
    #  Hack for the time being to adapt to LLVM22 having different output for lambda
    lines = lines.replace("(lambda)", "(anonymous class)")

    # clang-tidy prints the tags slightly different
    if cmd.split(" ")[0].endswith("clang-tidy"):
        lines = lines.replace("[clazy", "[-Wclazy")
    if qt_namespaced and qt_replace_namespace:
        lines = lines.replace("MyQt::", "")

    if not success and not output_file:
        print(lines)
        return False

    if verbose:
        print("Running: " + cmd)
        print("output_file=" + output_file)

    lines = lines.replace('\r\n', '\n')
    if lines.endswith('\n\n'):
        lines = lines[:-1]  # remove *only* one extra newline, all testfiles have a newline based on unix convention
    if output_file:
        f = io.open(output_file, 'w', encoding='utf8')
        f.writelines(lines)
        f.close()
    elif len(lines) > 0:
        print(lines)

    return success

def get_command_output(cmd: str, verbose: bool, test_env=os.environ, cwd=None, ignore_verbose=False):
    success = True

    try:
        if verbose and not ignore_verbose:
            print(cmd)

        # Polish up the env to fix "TypeError: environment can only contain strings" exception
        str_env = {}
        for key in test_env.keys():
            str_env[str(key)] = str(test_env[key])

        output = subprocess.check_output(
            cmd, stderr=subprocess.STDOUT, shell=True, env=str_env, cwd=cwd)
    except subprocess.CalledProcessError as e:
        output = e.output
        success = False

    if type(output) is bytes:
        output = output.decode('utf-8')

    return output, success


def files_are_equal(file1, file2):
    try:
        f = io.open(file1, 'r', encoding='utf-8')
        lines1 = f.readlines()
        f.close()

        f = io.open(file2, 'r', encoding='utf-8')
        lines2 = f.readlines()
        f.close()

        return lines1 == lines2
    except Exception as ex:
        print("Error comparing files:" + str(ex))
        return False

def print_differences(file1, file2, verbose):
    # Returns true if the the files are equal
    return run_command("diff -Naur --strip-trailing-cr {} {}".format(file1, file2), verbose=verbose)