summaryrefslogtreecommitdiff
path: root/scripts/base.py
blob: 324298e16832e5bd52408948db03a45f4b0e1d82 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! /usr/bin/env python3
#===============================
#
# base
#
# 2019/02/07 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
#===============================
import os
import re
import subprocess

#====================================
#
# base
#
# it supports do/run/run1 for using external command
#
#====================================
class base:
    __top = os.path.abspath(__file__ + "/../../");
    __key = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
    __bsp = ["bsp41x", "bsp39x"]

    #--------------------
    # chomp
    #--------------------
    def chomp(self, text):
        return re.sub(r"\n$", r"", text);

    #--------------------
    # top()
    #--------------------
    def top(self):
        return base.__top;

    #--------------------
    # bsp_list()
    #--------------------
    def bsp_list(self):
        return base.__bsp;

    #--------------------
    # is_key()
    #--------------------
    def is_key(self, key):
        return re.match(base.__key, key);

    #--------------------
    # color
    #--------------------
    def color(self, color, text):
        return "\033[{}m{}\033[0m".format(self.config(color), text)

    #--------------------
    # do()
    #
    # do command
    #--------------------
    def do(self, command):
        return subprocess.run(command, shell=True).returncode;

    #--------------------
    # tolist()
    #--------------------
    def tolist(self, string):
        if (len(string) > 0):
            return string.split('\n');

        return [];

    #--------------------
    # run()
    #
    # run command and get result as plane text
    #--------------------
    def run2(self, command):

        # Ughhhh
        # I don't like python external command !!
        # (ノ `Д´)ノ  go away !!
        child = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        stdout, stderr = child.communicate()

        return self.chomp(stdout.decode("utf-8")), child.returncode;

    def run(self, command):
        return self.run2(command)[0]

    #--------------------
    # run1()
    #
    # run command and get result as list
    #--------------------
    def runl(self, command):

        # call run() and exchange result as array
        #
        # "xxxxxxx
        #  yyyyyyy
        #  zzzzzzz"
        # ->
        # ["xxxxxxx",
        #  "yyyyyyy",
        #  "zzzzzzz"]
        return self.tolist(self.run(command));

    #--------------------
    # config()
    #
    # read settings from config
    #--------------------
    def config(self, item):
        if (not os.path.exists("{}/.config".format(self.top()))):
            print("\nplease copy .config.sample to .config\n" +
                  "and edit it for your environment\n");
            exit();

        config = self.run("grep {} {}/.config | cut -d : -f2".\
                        format(item, self.top()));
        return self.chomp(config);