|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +import os |
| 5 | +import platform |
| 6 | +import shutil |
| 7 | +import subprocess |
| 8 | +import stat |
| 9 | +import sys |
| 10 | +import time |
| 11 | + |
| 12 | +SYSTEM = platform.system() |
| 13 | + |
| 14 | +def _exec_cmd_single(command, |
| 15 | + cwd = None, |
| 16 | + env = None, |
| 17 | + shell = False, |
| 18 | + level = 1, |
| 19 | + exit_on_failure = True, |
| 20 | + show_command = False, |
| 21 | + silent = False, |
| 22 | + redirect_file = '', |
| 23 | + redirect_append = False): |
| 24 | + if command == '': |
| 25 | + return 0, [] |
| 26 | + file = None |
| 27 | + if redirect_file != '': |
| 28 | + if redirect_append: |
| 29 | + file = open(redirect_file, 'a') |
| 30 | + else: |
| 31 | + file = open(redirect_file, 'w') |
| 32 | + cmd = None |
| 33 | + if shell: |
| 34 | + cmd = command.strip() |
| 35 | + else: |
| 36 | + cmd = command.strip().split() |
| 37 | + if show_command: |
| 38 | + if not silent: |
| 39 | + print(f'{"+" * level} {command}') |
| 40 | + if not file is None: |
| 41 | + file.write(f'{"+" * level} {command}\n') |
| 42 | + lines_stdout = [] |
| 43 | + p = subprocess.Popen(cmd, |
| 44 | + cwd = cwd, |
| 45 | + stdout = subprocess.PIPE, |
| 46 | + stderr = subprocess.STDOUT, |
| 47 | + env = env, |
| 48 | + shell = shell, |
| 49 | + text = True) |
| 50 | + for line in iter(p.stdout.readline, ''): |
| 51 | + lines_stdout.append(line.strip()) |
| 52 | + if not silent: |
| 53 | + print(line, end = '') |
| 54 | + if not file is None: |
| 55 | + file.write(line) |
| 56 | + p.stdout.close() |
| 57 | + return_code = p.wait() |
| 58 | + if file is not None: |
| 59 | + file.close() |
| 60 | + if exit_on_failure: |
| 61 | + assert p.returncode == 0, f'Command [{command.strip()}] execution failed.' |
| 62 | + return p.returncode, lines_stdout |
| 63 | + |
| 64 | +def exec_cmds(commands, |
| 65 | + cwd = None, |
| 66 | + env = None, |
| 67 | + shell = False, |
| 68 | + exit_on_failure = True, |
| 69 | + stop_on_failure = True, |
| 70 | + show_command = False, |
| 71 | + silent = False, |
| 72 | + redirect_file = '', |
| 73 | + redirect_append = False): |
| 74 | + if show_command: |
| 75 | + silent = False |
| 76 | + if redirect_file != '': |
| 77 | + if not redirect_append and os.path.exists(redirect_file): |
| 78 | + os.remove(redirect_file) |
| 79 | + redirect_append = True |
| 80 | + ret_code = 0 |
| 81 | + ret_lines = [] |
| 82 | + cmds = [] |
| 83 | + for c in commands.split('\n'): |
| 84 | + c = c.strip() |
| 85 | + if c != '': |
| 86 | + cmds.append(c) |
| 87 | + for cmd in cmds: |
| 88 | + r, lines_stdout = _exec_cmd_single(cmd, |
| 89 | + cwd = cwd, |
| 90 | + env = env, |
| 91 | + shell = shell, |
| 92 | + show_command = show_command, |
| 93 | + exit_on_failure = exit_on_failure, |
| 94 | + silent = silent, |
| 95 | + redirect_file = redirect_file, |
| 96 | + redirect_append = redirect_append) |
| 97 | + ret_code = r |
| 98 | + ret_lines += lines_stdout |
| 99 | + if stop_on_failure and r > 0 and len(cmds) > 1: |
| 100 | + print('Execution failed!') |
| 101 | + break |
| 102 | + return ret_code, ret_lines |
| 103 | + |
| 104 | +def check_system_commands(commands): |
| 105 | + absent = 0 |
| 106 | + ret = {} |
| 107 | + for cmd in commands: |
| 108 | + ret[cmd] = shutil.which(cmd) |
| 109 | + if not ret[cmd]: |
| 110 | + print(f'{cmd} not found.') |
| 111 | + absent += 1 |
| 112 | + assert absent == 0, 'Required system command(s) not found.' |
| 113 | + return ret |
| 114 | + |
| 115 | +def remove_directory(directory): |
| 116 | + if SYSTEM == 'Windows': |
| 117 | + for root, dirs, files in os.walk(directory): |
| 118 | + for dir_name in dirs: |
| 119 | + dir_path = os.path.join(root, dir_name) |
| 120 | + os.chmod(dir_path, stat.S_IWRITE) |
| 121 | + for file_name in files: |
| 122 | + file_path = os.path.join(root, file_name) |
| 123 | + os.chmod(file_path, stat.S_IWRITE) |
| 124 | + shutil.rmtree(directory) |
| 125 | + |
| 126 | +def remove_file_dir(item): |
| 127 | + if os.path.isfile(item): |
| 128 | + os.remove(item) |
| 129 | + elif os.path.isdir(item): |
| 130 | + remove_directory(item) |
| 131 | + else: |
| 132 | + pass |
| 133 | + |
| 134 | +def clear_directory(directory): |
| 135 | + for item in os.listdir(directory): |
| 136 | + if item.startswith("."): |
| 137 | + continue |
| 138 | + remove_file_dir(os.path.join(directory, item)) |
| 139 | + |
| 140 | +def update_source_code(dir_name, |
| 141 | + url_repo, |
| 142 | + branch, |
| 143 | + branch_main = 'main', |
| 144 | + basedir = '', |
| 145 | + show_command = False): |
| 146 | + print(f'========== {dir_name} ==========') |
| 147 | + dir_target = os.path.join(basedir, dir_name) |
| 148 | + if not os.path.isdir(dir_target): |
| 149 | + exec_cmds(f'git clone {url_repo} {dir_name}', |
| 150 | + cwd=basedir, |
| 151 | + show_command = show_command) |
| 152 | + if branch != '': |
| 153 | + clear_directory(dir_target) |
| 154 | + exec_cmds(f'''git checkout . |
| 155 | + git checkout {branch_main} |
| 156 | + git pull''', |
| 157 | + cwd = dir_target, |
| 158 | + silent = True) |
| 159 | + exec_cmds(f'git checkout {branch}', |
| 160 | + cwd = dir_target, |
| 161 | + show_command = show_command) |
| 162 | + exec_cmds('''git submodule sync |
| 163 | + git submodule update --init --recursive''', |
| 164 | + cwd = dir_target, |
| 165 | + show_command = show_command) |
| 166 | + |
| 167 | +def source_env(script, |
| 168 | + env = None, |
| 169 | + show_command = False): |
| 170 | + if script == '' or not os.path.exists(script): |
| 171 | + print(f'Incorrect script: {script}') |
| 172 | + return None |
| 173 | + if env is None: |
| 174 | + env = os.environ.copy() |
| 175 | + separator = '========== SEPARATOR ==========' |
| 176 | + command = '' |
| 177 | + if SYSTEM == 'Linux': |
| 178 | + command = f'. {script} && echo "{separator}" && env' |
| 179 | + elif SYSTEM == 'Windows': |
| 180 | + command = f'cmd.exe /c ""{script}" && echo {separator} && set"' |
| 181 | + else: |
| 182 | + pass |
| 183 | + if show_command: |
| 184 | + print(f'+ {command.split("&&")[0].strip()}') |
| 185 | + _, lines_stdout = exec_cmds(command, |
| 186 | + env = env, |
| 187 | + shell = True, |
| 188 | + silent = True) |
| 189 | + parse_start = False |
| 190 | + for line in lines_stdout: |
| 191 | + if parse_start: |
| 192 | + key, value = line.strip().split('=', 1) |
| 193 | + env[key] = value |
| 194 | + if line.strip() == separator: |
| 195 | + parse_start = True |
| 196 | + return env |
| 197 | + |
| 198 | +def get_duration(t0): |
| 199 | + t1 = int(time.time() * 1000) |
| 200 | + return (t1 - t0) / 1000.0 |
| 201 | + |
| 202 | +def download(url, filepath): |
| 203 | + import requests |
| 204 | + response = requests.get(url, stream=True) |
| 205 | + assert response.status_code >= 200 and response.status_code < 300, f'Failed to download {url}.' |
| 206 | + with open(filepath, 'wb') as file: |
| 207 | + for chunk in response.iter_content(chunk_size=8192): |
| 208 | + file.write(chunk) |
0 commit comments