python: remove not needed imports in various python scripts
[platform/upstream/efl.git] / examples_checks.py
1 #!/usr/bin/python3
2 import os
3 import subprocess
4 import json
5 import time
6 import concurrent.futures
7 import argparse
8 import tempfile
9
10 #
11 # preparation calls for the examples
12 #
13
14 def prep_eina_file_02():
15   f = tempfile.NamedTemporaryFile(delete=False)
16   f.write(b"Simulation")
17   return [f.name, "/tmp/copy_file"]
18
19 def prep_eina_xattr_01():
20   f = tempfile.NamedTemporaryFile(delete=False)
21   f.write(b"Simulation")
22   return ["list", f.name]
23
24 def prep_eina_xattr_02():
25   f1 = tempfile.NamedTemporaryFile(delete=False)
26   f1.write(b"Simulation")
27   f2 = tempfile.NamedTemporaryFile(delete=False)
28   f2.write(b"Simulation2")
29   return [f1.name, f2.name]
30
31 def prep_eet_data_simple():
32   f1 = tempfile.NamedTemporaryFile(delete=False)
33   f1.write(b"Simulation")
34   f2 = tempfile.NamedTemporaryFile(delete=False)
35   f2.write(b"Simulation2")
36   return [f1.name, f2.name]
37
38 def prep_eet_data_nested():
39   f1 = tempfile.NamedTemporaryFile(delete=False)
40   f1.write(b"Simulation")
41   f2 = tempfile.NamedTemporaryFile(delete=False)
42   f2.write(b"Simulation2")
43   return [f1.name, f2.name]
44
45 def prep_eet_data_file_descriptor_01():
46   f1 = tempfile.NamedTemporaryFile(delete=False)
47   f1.write(b"Simulation")
48   f2 = tempfile.NamedTemporaryFile(delete=False)
49   f2.write(b"Simulation2")
50   return [f1.name, f2.name, "acc", "Example-Simulation"]
51
52 def prep_eet_data_file_descriptor_02():
53   f1 = tempfile.NamedTemporaryFile(delete=False)
54   f1.write(b"Simulation")
55   f2 = tempfile.NamedTemporaryFile(delete=False)
56   f2.write(b"Simulation2")
57   return [f1.name, f2.name, "union", "5", "Example-Simulation"]
58
59 def prep_ecore_getopt_example():
60   return ["string-1", "120", "apple", "none-optional-arg"]
61
62 def prep_eio_file_copy():
63   f1 = tempfile.NamedTemporaryFile(delete=False)
64   f1.write(b"Simulation")
65   return [f1.name, "/tmp/eio_dst_file"]
66
67 def prep_eio_file_ls():
68   return ["/tmp/"]
69
70 def prep_edje_color_class():
71   return ["beta", "red", "green", "blue"]
72
73 def prep_ecore_con_url_headers_example():
74   return ["GET", "www.enlightenment.org"]
75
76 def prep_ecore_con_url_download_example():
77   return ["www.enlightenment.org"]
78
79 def prep_ecore_con_url_cookies_example():
80   return ["www.enlightenment.org"]
81
82 def prep_ecore_con_client_example():
83   return ["enlightenment.org", "80"]
84
85 def prep_ecore_con_lookup_example():
86   return ["enlightenment.org"]
87
88 def prep_ecore_con_server_example():
89   return ["enlightenment.org", "1234"]
90
91 def prep_ecore_con_client_simple_example():
92   return ["enlightenment.org", "80"]
93
94 example_preparation = {
95   "eina_file_02" : prep_eina_file_02,
96   "eina_xattr_01" : prep_eina_xattr_01,
97   "eina_xattr_02" : prep_eina_xattr_02,
98   "eet-data-simple" : prep_eet_data_simple,
99   "eet-data-nested" : prep_eet_data_nested,
100   "eet-data-simple" : prep_eet_data_simple,
101   "eet-data-file_descriptor_01" : prep_eet_data_file_descriptor_01,
102   "eet-data-file_descriptor_02" : prep_eet_data_file_descriptor_02,
103   "ecore_getopt_example" : prep_ecore_getopt_example,
104   "eio_file_copy" : prep_eio_file_copy,
105   "eio_file_ls" : prep_eio_file_ls,
106   "edje-color-class" : prep_edje_color_class,
107   "ecore_con_url_headers_example" : prep_ecore_con_url_headers_example,
108   "ecore_con_url_download_example" : prep_ecore_con_url_download_example,
109   "ecore_con_url_cookies_example" : prep_ecore_con_url_cookies_example,
110   "ecore_con_client_example" : prep_ecore_con_client_example,
111   "ecore_con_lookup_example" : prep_ecore_con_lookup_example,
112   "ecore_con_server_example" : prep_ecore_con_server_example,
113   "ecore_con_client_simple_example" : prep_ecore_con_client_simple_example,
114 }
115
116 #
117 # Holds up the state of the ran examples
118 #
119
120 class State:
121   def __init__(self, examples):
122     self.max_n = examples
123     self.n = 1
124     self.count_fail = 0
125     self.count_success = 0
126     self.count_err_output = 0
127     print("Found "+str(self.max_n)+" Examples")
128
129   def add_run(self, command, error_in_output, exitcode):
130     print("{}/{} {} {} {} ".format(self.n, self.max_n, ("SUCCESS" if exitcode == 0 else "FAIL"), ("CLEAN" if error_in_output == False else "ERR"), command))
131     self.n = self.n + 1
132     if exitcode != 0:
133       self.count_fail += 1
134     if error_in_output == True:
135       self.count_err_output += 1
136     if exitcode == 0 and error_in_output == False:
137       self.count_success += 1
138
139   def print_summary(self):
140     print("Summary")
141     print("  Failed: "+str(self.count_fail)+"/"+str(self.max_n))
142     print("  Errored: "+str(self.count_err_output)+"/"+str(self.max_n))
143     print("  Success: "+str(self.count_success)+"/"+str(self.max_n))
144
145 #
146 # this simulates the startup of the example, and the closing after 1s
147 #
148
149 def simulate_example(example):
150   args = []
151   if os.path.basename(example) in example_preparation:
152     args = example_preparation[os.path.basename(example)]()
153
154   #meson changed behaviour from 0.49 to 0.50 so we need this:
155   if os.path.isabs(example):
156     example_dir = example
157   else:
158     example_dir = os.path.join(G.builddir, example)
159
160   run = subprocess.Popen([example_dir] + args,
161       stdout = subprocess.PIPE,
162       stderr = subprocess.PIPE,
163   )
164   time.sleep(1)
165   run.terminate()
166   try:
167     outs, errs = run.communicate(timeout=2)
168   except Exception as e:
169     run.kill()
170     return (example, True, -1)
171   else:
172     return (example, True if b'ERR' in outs or b'ERR' in errs else False, run.poll())
173
174 #meson changed behaviour from 0.49 to 0.50 so we need this:
175 def meson_fetch_filename(filename_object):
176   if isinstance(filename_object, str):
177     return filename_object
178   else:
179     return filename_object[0]
180
181
182 parser = argparse.ArgumentParser(description='Run the examples of efl')
183 parser.add_argument('builddir', metavar='build', help='the path where to find the meson build directory')
184
185
186 G = parser.parse_args()
187 #Run meson to fetch all examples
188 meson_introspect = subprocess.Popen(["meson", "introspect", G.builddir, "--targets"],
189       stdout = subprocess.PIPE,
190       stderr = subprocess.PIPE,
191 )
192 meson_introspect.poll()
193 build_targets = json.loads(meson_introspect.stdout.read())
194 examples = [meson_fetch_filename(b["filename"]) for b in build_targets if "examples" in meson_fetch_filename(b["filename"]) and b["type"] == "executable"]
195 state = State(len(examples))
196 #simulate all examples in parallel with up to 5 runners
197 with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
198   futures = [executor.submit(simulate_example, example) for example in examples]
199   for future in concurrent.futures.as_completed(futures):
200     example_run = future.result()
201     state.add_run(example_run[0], example_run[1], example_run[2])
202 state.print_summary()