-
Notifications
You must be signed in to change notification settings - Fork 148
/
test.py
executable file
·149 lines (107 loc) · 3.8 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
#
# Copyright 2015-2016 Jose Fonseca
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
import os.path
import traceback
import multiprocessing
def test(arg):
sys.stdout.write(arg + '\n')
sys.stdout.flush()
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from xdot.ui import DotWidget, DotWindow
class TestDotWidget(DotWidget):
def __init__(self, filename):
DotWidget.__init__(self)
self.dirname, basename = os.path.split(filename)
name, ext = os.path.splitext(basename)
self.name = name
def on_draw(self, widget, cr):
DotWidget.on_draw(self, widget, cr)
if True:
# Cairo screenshot
import cairo
# Scale to give 96 dpi instead of 72 dpi
dpi = 96.0
scale = dpi/72.0
w = int(self.graph.width*scale)
h = int(self.graph.height*scale)
CAIRO_XMAX = 32767
CAIRO_YMAX = 32767
if w >= CAIRO_XMAX:
w = CAIRO_XMAX
scale = w/self.graph.width
h = int(self.graph.height*scale)
if h >= CAIRO_YMAX:
h = CAIRO_YMAX
scale = h/self.graph.height
w = int(self.graph.width*scale)
assert w <= CAIRO_XMAX
assert h <= CAIRO_YMAX
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
cr = cairo.Context(surface)
cr.set_source_rgba(1.0, 1.0, 1.0, 1.0)
cr.paint()
cr.scale(scale, scale)
self.graph.draw(cr, highlight_items=self.highlight)
surface.write_to_png(os.path.join(self.dirname, self.name + '.png'))
if False:
# GTK 3 screenshot
window = self.get_window()
w = window.get_width()
h = window.get_height()
pixbuf = Gdk.pixbuf_get_from_window(window, 0, 0, w, h)
pixbuf.savev(os.path.join(self.dirname, self.name + '.png'), 'png', (), ())
Gtk.main_quit()
def error_dialog(self, message):
sys.stderr.write(message)
sys.stderr.write("\n")
result = True
widget = TestDotWidget(arg)
window = DotWindow(widget)
window.connect('delete-event', Gtk.main_quit)
try:
try:
dotcode = open(arg, 'rb').read()
window.set_dotcode(dotcode)
except:
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
result = False
else:
window.show()
Gtk.main()
finally:
window.destroy()
return result
def main():
args = sys.argv[1:]
pool = multiprocessing.Pool(multiprocessing.cpu_count())
results = pool.map(test, args)
# Exit with status 1 if any failed
try:
results.index(False)
except ValueError:
status = 0
else:
status = 1
sys.exit(status)
if __name__ == '__main__':
main()