|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright 2014 Google Inc. | |
4 # | |
5 # Use of this source code is governed by a BSD-style license that can be | |
6 # found in the LICENSE file. | |
7 | |
8 """ | |
rmistry
2014/04/28 16:58:44
"""Script to test out suitableForGpuRasterization
robertphillips
2014/04/28 17:45:58
Done.
| |
9 Script to test out suitableForGpuRasterization (via gpuveto) | |
10 """ | |
11 | |
12 import glob | |
13 import os | |
14 import re | |
15 import subprocess | |
16 import sys | |
17 | |
18 # Set the PYTHONPATH to include the tools directory. | |
19 sys.path.append( | |
20 os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)) | |
21 import find_run_binary | |
22 | |
23 USAGE = """ | |
24 Usage: | |
25 {command} SKP_FILE | |
26 {command} SKP_DIR\n | |
27 """ | |
28 | |
29 def find_path_to_program(program): | |
30 """ | |
31 Returns path to an existing program binary. | |
rmistry
2014/04/28 16:58:44
Keep this line above, eg:
"""Returns path to an ex
robertphillips
2014/04/28 17:45:58
Done.
| |
32 | |
33 @param program: Basename of the program to find (e.g., 'render_pictures'). | |
34 | |
35 @returns absolute path to the program binary, as a string. | |
36 | |
37 Raises: | |
38 Exception: unable to find the program binary. | |
39 """ | |
40 return find_run_binary.find_path_to_program(program) | |
41 | |
rmistry
2014/04/28 16:58:44
Nit: Need 2 newlines between top level definitions
robertphillips
2014/04/28 17:45:58
Done.
| |
42 def list_files(dir_or_file): | |
43 """ | |
44 Accepts a directory or filename. Returns a list of all the files in the | |
45 directory or just the file. | |
46 """ | |
rmistry
2014/04/28 16:58:44
Change above to:
"""Returns list of all files fro
robertphillips
2014/04/28 17:45:58
Done.
| |
47 files = [] | |
48 for globbedpath in glob.iglob(dir_or_file): # useful on win32 | |
49 if os.path.isdir(globbedpath): | |
50 for filename in os.listdir(globbedpath): | |
51 newpath = os.path.join(globbedpath, filename) | |
52 if os.path.isfile(newpath): | |
53 files.append(newpath) | |
54 elif os.path.isfile(globbedpath): | |
55 files.append(globbedpath) | |
56 return files | |
57 | |
58 def execute_program(args): | |
59 """ | |
60 Execute a process and wait for it to complete. Returns all | |
61 output (stderr and stdout). | |
62 | |
63 @param args is passed into subprocess.Popen(). | |
64 | |
65 @returns a tuple (returncode, output) | |
66 """ | |
rmistry
2014/04/28 16:58:44
"""Executes a process and waits for it to complete
robertphillips
2014/04/28 17:45:58
Done.
| |
67 proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDO UT) | |
rmistry
2014/04/28 16:58:44
Nit: max line length in python is 80.
robertphillips
2014/04/28 17:45:58
Done.
| |
68 output, ignore = proc.communicate() | |
69 errcode = proc.returncode | |
70 return (errcode, output) | |
71 | |
72 class TestObj(object): | |
73 | |
74 def __init__(self): | |
75 self.bench_pictures = find_path_to_program('bench_pictures') | |
76 self.gpuveto = find_path_to_program('gpuveto') | |
77 assert os.path.isfile(self.bench_pictures) | |
78 assert os.path.isfile(self.gpuveto) | |
79 self.truePositives = 0 | |
80 self.falsePositives = 0 | |
81 self.trueNegatives = 0 | |
82 self.falseNegatives = 0 | |
83 | |
84 def process_skps(self, dir_or_file): | |
85 for skp in enumerate(dir_or_file): | |
86 self.process_skp(skp[1]) | |
87 | |
88 sys.stdout.write('TP %d FP %d TN %d FN %d\n' % (self.truePositives, | |
89 self.falsePositives, | |
90 self.trueNegatives, | |
91 self.falseNegatives)) | |
92 | |
93 | |
94 def process_skp(self, skp_file): | |
95 assert os.path.isfile(skp_file) | |
96 | |
97 # run gpuveto on the skp | |
98 args = [self.gpuveto, '-r', skp_file] | |
99 returncode, output = execute_program(args) | |
100 if (returncode != 0): | |
101 return | |
102 | |
103 if ('unsuitable' in output): | |
104 suitable = False | |
105 else: | |
106 assert 'suitable' in output | |
107 suitable = True | |
108 | |
109 # run raster config | |
110 args = [self.bench_pictures, '-r', skp_file, '--repeat', '20', '--config ', '8888'] | |
rmistry
2014/04/28 16:58:44
Nit: max line length in python is 80.
robertphillips
2014/04/28 17:45:58
Done.
| |
111 returncode, output = execute_program(args) | |
112 if (returncode != 0): | |
113 return | |
114 | |
115 matches = re.findall('[\d.]+', output) | |
116 if len(matches) != 4: | |
117 return | |
118 | |
119 rasterTime = float(matches[3]) | |
120 | |
121 # run gpu config | |
122 args2 = [self.bench_pictures, '-r', skp_file, '--repeat', '20', '--confi g', 'gpu'] | |
rmistry
2014/04/28 16:58:44
Nit: max line length in python is 80.
robertphillips
2014/04/28 17:45:58
Done.
| |
123 returncode, output = execute_program(args2) | |
124 if (returncode != 0): | |
125 return | |
126 | |
127 matches = re.findall('[\d.]+', output) | |
128 if len(matches) != 4: | |
129 return | |
130 | |
131 gpuTime = float(matches[3]) | |
132 | |
133 sys.stdout.write("%s: gpuveto: %d raster %.2f gpu: %.2f\n" % (skp_file, | |
rmistry
2014/04/28 16:58:44
Move all arguments to the next line:
sys.stdout.w
robertphillips
2014/04/28 17:45:58
Done.
| |
134 suitable, | |
135 rasterTime , | |
136 gpuTime)) | |
137 | |
138 if suitable: | |
139 if gpuTime < rasterTime: | |
140 self.truePositives += 1 | |
141 else: | |
142 self.falsePositives += 1 | |
143 else: | |
144 if gpuTime < rasterTime: | |
145 self.falseNegatives += 1 | |
146 else: | |
147 self.trueNegatives += 1 | |
148 | |
149 def main(main_argv): | |
150 if not main_argv or main_argv[0] in ['-h', '-?', '-help', '--help']: | |
rmistry
2014/04/28 16:58:44
Could you please use argparse instead for this? It
| |
151 sys.stderr.write(USAGE.format(command=__file__)) | |
152 return 1 | |
153 TestObj().process_skps(list_files(main_argv)) | |
154 | |
155 if __name__ == '__main__': | |
156 exit(main(sys.argv[1])) | |
rmistry
2014/04/28 16:58:44
Please use sys.exit(main(sys.argv[1])) instead.
robertphillips
2014/04/28 17:45:58
Done.
| |
OLD | NEW |