build: add a new script helper
[platform/upstream/efl.git] / examples_checks.py
1 #!/usr/bin/python3
2 import os
3 import sys
4 import subprocess
5 import json
6 import time
7 import concurrent.futures
8 import argparse
9 import tempfile
10
11 #
12 # preparation calls for the examples
13 #
14
15 def prep_eina_file_02():
16   f = tempfile.NamedTemporaryFile(delete=False)
17   f.write(b"Simulation")
18   return [f.name, "/tmp/copy_file"]
19
20 def prep_eina_xattr_01():
21   f = tempfile.NamedTemporaryFile(delete=False)
22   f.write(b"Simulation")
23   return ["list", f.name]
24
25 def prep_eina_xattr_02():
26   f1 = tempfile.NamedTemporaryFile(delete=False)
27   f1.write(b"Simulation")
28   f2 = tempfile.NamedTemporaryFile(delete=False)
29   f2.write(b"Simulation2")
30   return [f1.name, f2.name]
31
32 def prep_eet_data_simple():
33   f1 = tempfile.NamedTemporaryFile(delete=False)
34   f1.write(b"Simulation")
35   f2 = tempfile.NamedTemporaryFile(delete=False)
36   f2.write(b"Simulation2")
37   return [f1.name, f2.name]
38
39 def prep_eet_data_nested():
40   f1 = tempfile.NamedTemporaryFile(delete=False)
41   f1.write(b"Simulation")
42   f2 = tempfile.NamedTemporaryFile(delete=False)
43   f2.write(b"Simulation2")
44   return [f1.name, f2.name]
45
46 def prep_eet_data_file_descriptor_01():
47   f1 = tempfile.NamedTemporaryFile(delete=False)
48   f1.write(b"Simulation")
49   f2 = tempfile.NamedTemporaryFile(delete=False)
50   f2.write(b"Simulation2")
51   return [f1.name, f2.name, "acc", "Example-Simulation"]
52
53 def prep_eet_data_file_descriptor_02():
54   f1 = tempfile.NamedTemporaryFile(delete=False)
55   f1.write(b"Simulation")
56   f2 = tempfile.NamedTemporaryFile(delete=False)
57   f2.write(b"Simulation2")
58   return [f1.name, f2.name, "union", "5", "Example-Simulation"]
59
60 example_preparation = {
61   "eina_file_02" : prep_eina_file_02,
62   "eina_xattr_01" : prep_eina_xattr_01,
63   "eina_xattr_02" : prep_eina_xattr_02,
64   "eet-data-simple" : prep_eet_data_simple,
65   "eet-data-nested" : prep_eet_data_nested,
66   "eet-data-simple" : prep_eet_data_simple,
67   "eet-data-file_descriptor_01" : prep_eet_data_file_descriptor_01,
68   "eet-data-file_descriptor_02" : prep_eet_data_file_descriptor_02,
69 }
70
71 #
72 # Holds up the state of the ran examples
73 #
74
75 class State:
76   def __init__(self, examples):
77     self.max_n = examples
78     self.n = 1
79     self.count_fail = 0
80     self.count_success = 0
81     self.count_err_output = 0
82     print("Found "+str(self.max_n)+" Examples")
83
84   def add_run(self, command, error_in_output, exitcode):
85     print("{}/{} {} {} {} ".format(self.n, self.max_n, ("SUCCESS" if exitcode == 0 else "FAIL"), ("CLEAN" if error_in_output == False else "ERR"), command))
86     self.n = self.n + 1
87     if exitcode != 0:
88       self.count_fail += 1
89     if error_in_output == True:
90       self.count_err_output += 1
91     if exitcode == 0 and error_in_output == False:
92       self.count_success += 1
93
94   def print_summary(self):
95     print("Summary")
96     print("  Failed: "+str(self.count_fail)+"/"+str(self.max_n))
97     print("  Errored: "+str(self.count_err_output)+"/"+str(self.max_n))
98     print("  Success: "+str(self.count_success)+"/"+str(self.max_n))
99
100 #
101 # this simulates the startup of the example, and the closing after 1s
102 #
103
104 def simulate_example(example):
105   args = []
106   if os.path.basename(example) in example_preparation:
107     args = example_preparation[os.path.basename(example)]()
108   run = subprocess.Popen([G.builddir + "/" + example] + args,
109       stdout = subprocess.PIPE,
110       stderr = subprocess.PIPE,
111   )
112   time.sleep(1)
113   run.terminate()
114   try:
115     outs, errs = run.communicate(timeout=2)
116   except Exception as e:
117     run.kill()
118     return (example, True, -1)
119   else:
120     return (example, True if b'ERR' in outs or b'ERR' in errs else False, run.poll())
121
122
123 parser = argparse.ArgumentParser(description='Run the examples of efl')
124 parser.add_argument('builddir', metavar='build', help='the path where to find the meson build directory')
125
126 G = parser.parse_args()
127 #Run meson to fetch all examples
128 meson_introspect = subprocess.Popen(["meson", "introspect", G.builddir, "--targets"],
129       stdout = subprocess.PIPE,
130       stderr = subprocess.PIPE,
131 )
132 meson_introspect.poll()
133 build_targets = json.loads(meson_introspect.stdout.read())
134 examples = [b["filename"] for b in build_targets if "examples" in b["filename"] and b["type"] == "executable"]
135 state = State(len(examples))
136 #simulate all examples in parallel with up to 5 runners
137 with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
138   futures = [executor.submit(simulate_example, example) for example in examples]
139   for future in concurrent.futures.as_completed(futures):
140     example_run = future.result()
141     state.add_run(example_run[0], example_run[1], example_run[2])
142 state.print_summary()