|
1 | | -from concurrent.futures import ThreadPoolExecutor |
| 1 | +import concurrent.futures |
2 | 2 | import os |
3 | 3 | import subprocess |
4 | 4 | import sys |
|
19 | 19 | file_base_path = "../../agent/samples/policy/yaml" |
20 | 20 |
|
21 | 21 | def runCmd(arg): |
22 | | - return subprocess.run([arg], stdout=sys.stdout, stderr=sys.stderr, universal_newlines=True, input="", shell=True) |
| 22 | + return subprocess.run([arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, input="", shell=True, check=True) |
23 | 23 |
|
24 | 24 | def timeRunCmd(arg): |
| 25 | + log = [f"========== COMMAND: {arg}"] |
25 | 26 | start = time.time() |
26 | | - proc = runCmd(arg) |
27 | | - end = time.time() |
28 | 27 |
|
29 | | - log = f"COMMAND: {arg}\n" |
30 | | - if proc.returncode != 0: |
31 | | - log += f"`{arg}` failed with exit code {proc.returncode}. Stderr: {proc.stderr}, Stdout: {proc.stdout}\n" |
32 | | - log += f"Time taken: {round(end - start, 2)} seconds" |
33 | | - print(log) |
| 28 | + try: |
| 29 | + p = runCmd(arg) |
| 30 | + except subprocess.CalledProcessError as e: |
| 31 | + log.append(e.stdout) |
| 32 | + log.append(f"+++++ Failed with exit code {e.returncode}") |
| 33 | + raise |
| 34 | + else: |
| 35 | + if p.stdout: |
| 36 | + log.append(p.stdout) |
| 37 | + finally: |
| 38 | + end = time.time() |
| 39 | + log.append(f"Time taken: {round(end - start, 2)} seconds") |
| 40 | + print("\n".join(log)) |
34 | 41 |
|
35 | 42 | # check we can access all files we are about to update |
36 | 43 | for file in default_yamls + silently_ignored + no_policy: |
37 | 44 | filepath = os.path.join(file_base_path, file) |
38 | 45 | if not os.path.exists(filepath): |
39 | | - print(f"filepath does not exists: {filepath}") |
| 46 | + sys.exit(f"filepath does not exists: {filepath}") |
40 | 47 |
|
41 | 48 | # build tool |
42 | | -print("COMMAND: cargo build") |
43 | | -runCmd("cargo build") |
| 49 | +print("========== COMMAND: LIBC=gnu BUILD_TYPE= make") |
| 50 | +runCmd("LIBC=gnu BUILD_TYPE= make") |
44 | 51 |
|
45 | 52 | # update files |
46 | | -genpolicy_path = "target/debug/genpolicy" |
| 53 | +genpolicy_path = "./target/x86_64-unknown-linux-gnu/debug/genpolicy" |
47 | 54 |
|
48 | 55 | total_start = time.time() |
49 | | -executor = ThreadPoolExecutor(max_workers=os.cpu_count()) |
50 | 56 |
|
51 | | -for file in default_yamls + no_policy + needs_containerd_pull: |
52 | | - executor.submit(timeRunCmd, f"sudo {genpolicy_path} -d -y {os.path.join(file_base_path, file)}") |
| 57 | +with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor: |
| 58 | + futures = [] |
53 | 59 |
|
54 | | -for file in silently_ignored: |
55 | | - executor.submit(timeRunCmd, f"sudo {genpolicy_path} -d -s -y {os.path.join(file_base_path, file)}") |
| 60 | + for file in default_yamls + no_policy + needs_containerd_pull: |
| 61 | + cmd = f"sudo {genpolicy_path} -d -y {os.path.join(file_base_path, file)}" |
| 62 | + futures.append(executor.submit(timeRunCmd, cmd)) |
| 63 | + |
| 64 | + for file in silently_ignored: |
| 65 | + cmd = f"sudo {genpolicy_path} -d -s -y {os.path.join(file_base_path, file)}" |
| 66 | + futures.append(executor.submit(timeRunCmd, cmd)) |
| 67 | + |
| 68 | + for future in concurrent.futures.as_completed(futures): |
| 69 | + # Surface any potential exception thrown by the future. |
| 70 | + future.result() |
56 | 71 |
|
57 | | -executor.shutdown() |
58 | 72 | total_end = time.time() |
59 | 73 |
|
60 | 74 | print(f"Total time taken: {total_end - total_start} seconds") |
0 commit comments