File: Intrinsic_utils.py

package info (click to toggle)
intel-graphics-compiler 1.0.17791.18-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 102,312 kB
  • sloc: cpp: 935,343; lisp: 286,143; ansic: 16,196; python: 3,279; yacc: 2,487; lex: 1,642; pascal: 300; sh: 174; makefile: 27
file content (76 lines) | stat: -rw-r--r-- 2,779 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
# ========================== begin_copyright_notice ============================
#
# Copyright (C) 2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
# =========================== end_copyright_notice =============================

import os
import argparse
import re

from pathlib import Path
from mako.template import Template
from mako.lookup import TemplateLookup

def safe_open(path, mode = 'w'):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    return open(path, mode)

def remove_all_whitespace_at_line_end(text):
    """Removes all whitespace at the ends of lines
    Args:
        text:   An input text.
    Returns:
        The formatted text without trailing whitespaces at the ends of lines"""
    text = re.sub("[ \t]+(\r?)\n", '\g<1>\n', text, flags=re.MULTILINE)
    return text

def set_windows_line_ends(text):
    """Removes all whitespace at the ends of lines
    Args:
        text:   An input text.
    Returns:
        The formatted text without trailing whitespaces at the ends of lines"""
    text = re.sub("(?<!\r)\n", "\r\n", text, flags=re.MULTILINE)
    return text

def write_to_file_using_template(file_path, template, **kwargs):
    """Gets the formatted text with intendation which corresponds to 4 spaces.
    Args:
        file_path:   output path,
        template:    recipe for a new file,
        kwargs:      arguments for template."""
    try:
        print(" --- Generating %s" % os.path.basename(file_path))
        content = template.render(file_name=os.path.basename(file_path), **kwargs)
        content = remove_all_whitespace_at_line_end(content)
        content = set_windows_line_ends(content)
        if os.path.isfile(file_path):
            os.chmod(file_path, 0o666)
        with safe_open(file_path, 'wb') as f:
            f.write(content.encode())
        print(" --- Successfully generated in the following location: \n    %s"%(os.path.abspath(file_path)))
        os.chmod(file_path, 0o444)
    except IOError as e:
        print(" --- The file is not generated in the following location: \n    %s"%(os.path.abspath(file_path)))
        print("I/O error: %s" % e.strerror)
    except:
        print(" --- The file is not generated in the following location: \n    %s"%(os.path.abspath(file_path)))
        print(exceptions.text_error_template().render())

def from_template_name_to_destination_name(template_filename : str):
    return Path(template_filename).name.replace(".mako", "")

def file_path(path):
    if os.path.isfile(path):
        return path
    else:
        raise argparse.ArgumentTypeError(f"{path} is not a valid path to file")

def dir_path(path):
    if os.path.isdir(path):
        return path
    else:
        raise argparse.ArgumentTypeError(f"{path} is not a valid path to dir")