• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""
5Copyright (c) 2021 Huawei Device Co., Ltd.
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17
18Description: Implement the public interface in the 262 use case
19"""
20
21import os
22import sys
23import subprocess
24import datetime
25import time
26import shutil
27
28TERM_NORMAL = '\033[0m'
29TERM_YELLOW = '\033[1;33m'
30TERM_BLUE = '\033[1;34m'
31TERM_RED = '\033[1;31m'
32TERM_FUCHSIA = '\033[1;35m'
33
34
35def current_time():
36    return datetime.datetime.now().strftime('%m-%d %H:%M:%S.%f')
37
38
39class Logging():
40    def __init__(self):
41        self.is_logging = True
42
43    def debug(self, info):
44        if self.is_logging:
45            print(
46                f'{current_time()} D:>>>  {TERM_BLUE}{str(info)}{TERM_NORMAL}')
47
48    def info(self, info):
49        if self.is_logging:
50            if len(str(info)) > 100:
51                print(f'{current_time()} I:>>> \n{str(info)} ')
52            else:
53                print(f'{current_time()} I:>>>    {str(info)} ')
54
55
56LOGGING = Logging()
57
58
59class Command():
60    def __init__(self, cmd):
61        self.cmd = cmd
62
63    def run(self):
64        LOGGING.debug("command: " + self.cmd)
65        out = os.popen(self.cmd).read()
66        LOGGING.info(out)
67        return out
68
69
70def run_cmd(command):
71    cmd = Command(command)
72    return cmd.run()
73
74
75class CommandCwd():
76    def __init__(self, cmds, cwd):
77        self.cmds = cmds
78        self.cwd = cwd
79
80    def run(self):
81        cmd = " ".join(self.cmds)
82        LOGGING.debug("command: " + cmd + " | " + "dir: " + self.cwd)
83        proc = subprocess.Popen(self.cmds, cwd=self.cwd)
84        return proc.wait()
85
86
87def run_cmd_cwd(commands, cwd=os.getcwd()):
88    cmd = CommandCwd(commands, cwd)
89    return cmd.run()
90
91
92def sleep(duration):
93    LOGGING.debug("sleeping %d" % duration)
94    time.sleep(duration)
95
96
97def write_file(save_file, result):
98    LOGGING.debug(f"write file:{save_file}")
99    with open(save_file, "a+") as file:
100        file.write(result + "\n")
101        file.flush()
102
103
104def remove_dir(path):
105    if os.path.exists(path):
106        shutil.rmtree(path)
107
108
109def remove_file(file):
110    if os.path.exists(file):
111        os.remove(file)
112
113
114def mkdir(path):
115    if not os.path.exists(path):
116        os.makedirs(path)
117
118
119def report_command(cmd_type, cmd, env=None):
120    sys.stderr.write(f'{TERM_BLUE}{cmd_type}{TERM_NORMAL}\n')
121    if env is not None:
122        sys.stderr.write(''.join(f'{TERM_BLUE}{var}={val} \\{TERM_NORMAL}\n'
123                                 for var, val in sorted(env.items())))
124    cmd_str = (f'{TERM_NORMAL}\n\t{TERM_BLUE}').join(cmd)
125    sys.stderr.write(f'\t{TERM_BLUE}{cmd_str}{TERM_NORMAL}\n')
126    sys.stderr.write("\n")
127
128
129def git_clone(git_url, code_dir):
130    cmd = ['git', 'clone', git_url, code_dir]
131    ret = run_cmd_cwd(cmd)
132    assert not ret, f"\n error: Cloning '{git_url}' failed."
133
134
135def git_checkout(git_bash, cwd):
136    cmd = ['git', 'checkout', git_bash]
137    ret = run_cmd_cwd(cmd, cwd)
138    assert not ret, f"\n error: git checkout '{git_bash}' failed."
139
140
141def git_apply(patch_file, cwd):
142    cmd = ['git', 'apply', patch_file]
143    ret = run_cmd_cwd(cmd, cwd)
144    assert not ret, f"\n error: Failed to apply '{patch_file}'"
145
146
147def git_clean(cwd):
148    cmd = ['git', 'checkout', '--', '.']
149    run_cmd_cwd(cmd, cwd)
150
151
152def npm_install(cwd):
153    cmd = ['npm', 'install']
154    ret = run_cmd_cwd(cmd, cwd)
155    assert not ret, f"\n error: Failed to 'npm install'"
156