1 From ffc23c3b3d49d265997b6bb5d55d9463080c1cc8 Mon Sep 17 00:00:00 2001
2 From: Debarshi Ray <debarshir@freedesktop.org>
3 Date: Mon, 10 Feb 2020 15:20:38 +0100
4 Subject: [PATCH] tools: Fix the build with Python 3
6 This is a subset of commit 4a9fdeb64bc87f09 from master.
8 https://gitlab.freedesktop.org/telepathy/telepathy-logger/merge_requests/1
10 tools/c-constants-gen.py | 12 ++++++------
11 tools/glib-client-gen.py | 18 ++++++------------
12 tools/glib-client-marshaller-gen.py | 20 ++++++++++----------
13 tools/glib-ginterface-gen.py | 22 ++++++++--------------
14 tools/glib-gtypes-generator.py | 14 +++++++-------
15 tools/glib-interfaces-gen.py | 14 +++++++-------
16 tools/libglibcodegen.py | 4 ++--
17 tools/libtpcodegen.py | 22 +++++++++++++++++++++-
18 tools/xincludator.py | 13 ++++++++++---
19 9 files changed, 77 insertions(+), 62 deletions(-)
21 diff --git a/tools/c-constants-gen.py b/tools/c-constants-gen.py
22 index c7a93d3714e5..a08afee06196 100644
23 --- a/tools/c-constants-gen.py
24 +++ b/tools/c-constants-gen.py
26 from sys import argv, stdout, stderr
27 import xml.dom.minidom
29 -from libtpcodegen import file_set_contents
30 +from libtpcodegen import file_set_contents, u
31 from libglibcodegen import NS_TP, get_docstring, \
32 get_descendant_text, get_by_path
34 @@ -12,7 +12,7 @@ class Generator(object):
35 self.prefix = prefix + '_'
36 self.spec = get_by_path(dom, "spec")[0]
38 - self.output_base = output_base
39 + self.output_base = output_base
43 @@ -21,14 +21,14 @@ class Generator(object):
47 - file_set_contents(self.output_base + '.h', ''.join(self.__header))
48 - file_set_contents(self.output_base + '-gtk-doc.h', ''.join(self.__docs))
49 + file_set_contents(self.output_base + '.h', u('').join(self.__header).encode('utf-8'))
50 + file_set_contents(self.output_base + '-gtk-doc.h', u('').join(self.__docs).encode('utf-8'))
52 def write(self, code):
53 - self.__header.append(code.encode('utf-8'))
54 + self.__header.append(code)
57 - self.__docs.append(code.encode('utf-8'))
58 + self.__docs.append(code)
62 diff --git a/tools/glib-client-gen.py b/tools/glib-client-gen.py
63 index 6b2b97f1d80d..fb083a114314 100644
64 --- a/tools/glib-client-gen.py
65 +++ b/tools/glib-client-gen.py
66 @@ -27,8 +27,8 @@ import os.path
67 import xml.dom.minidom
68 from getopt import gnu_getopt
70 -from libtpcodegen import file_set_contents
71 -from libglibcodegen import Signature, type_to_gtype, cmp_by_name, \
72 +from libtpcodegen import file_set_contents, key_by_name, u
73 +from libglibcodegen import Signature, type_to_gtype, \
74 get_docstring, xml_escape, get_deprecated
77 @@ -74,18 +74,12 @@ class Generator(object):
78 self.guard = opts.get('--guard', None)
81 - if isinstance(s, unicode):
82 - s = s.encode('utf-8')
83 self.__header.append(s)
86 - if isinstance(s, unicode):
87 - s = s.encode('utf-8')
91 - if isinstance(s, unicode):
92 - s = s.encode('utf-8')
95 def get_iface_quark(self):
96 @@ -1187,7 +1181,7 @@ class Generator(object):
99 nodes = self.dom.getElementsByTagName('node')
100 - nodes.sort(cmp_by_name)
101 + nodes.sort(key=key_by_name)
104 self.do_interface(node)
105 @@ -1240,9 +1234,9 @@ class Generator(object):
106 self.h('#endif /* defined (%s) */' % self.guard)
109 - file_set_contents(self.basename + '.h', '\n'.join(self.__header))
110 - file_set_contents(self.basename + '-body.h', '\n'.join(self.__body))
111 - file_set_contents(self.basename + '-gtk-doc.h', '\n'.join(self.__docs))
112 + file_set_contents(self.basename + '.h', u('\n').join(self.__header).encode('utf-8'))
113 + file_set_contents(self.basename + '-body.h', u('\n').join(self.__body).encode('utf-8'))
114 + file_set_contents(self.basename + '-gtk-doc.h', u('\n').join(self.__docs).encode('utf-8'))
116 def types_to_gtypes(types):
117 return [type_to_gtype(t)[1] for t in types]
118 diff --git a/tools/glib-client-marshaller-gen.py b/tools/glib-client-marshaller-gen.py
119 index cb27d638a45d..cd9823bdffb2 100644
120 --- a/tools/glib-client-marshaller-gen.py
121 +++ b/tools/glib-client-marshaller-gen.py
122 @@ -31,23 +31,23 @@ class Generator(object):
123 for signal in signals:
124 self.do_signal(signal)
127 - print '%s_register_dbus_glib_marshallers (void)' % self.prefix
130 + print('%s_register_dbus_glib_marshallers (void)' % self.prefix)
133 - all = self.marshallers.keys()
134 + all = list(self.marshallers.keys())
136 for marshaller in all:
137 rhs = self.marshallers[marshaller]
139 - print ' dbus_g_object_register_marshaller ('
140 - print ' g_cclosure_marshal_generic,'
141 - print ' G_TYPE_NONE, /* return */'
142 + print(' dbus_g_object_register_marshaller (')
143 + print(' g_cclosure_marshal_generic,')
144 + print(' G_TYPE_NONE, /* return */')
146 - print ' G_TYPE_%s,' % type.replace('VOID', 'NONE')
147 - print ' G_TYPE_INVALID);'
148 + print(' G_TYPE_%s,' % type.replace('VOID', 'NONE'))
149 + print(' G_TYPE_INVALID);')
155 def types_to_gtypes(types):
156 diff --git a/tools/glib-ginterface-gen.py b/tools/glib-ginterface-gen.py
157 index 784397738fc2..97369d36fe48 100644
158 --- a/tools/glib-ginterface-gen.py
159 +++ b/tools/glib-ginterface-gen.py
160 @@ -26,8 +26,8 @@ import sys
162 import xml.dom.minidom
164 -from libtpcodegen import file_set_contents
165 -from libglibcodegen import Signature, type_to_gtype, cmp_by_name, \
166 +from libtpcodegen import file_set_contents, key_by_name, u
167 +from libglibcodegen import Signature, type_to_gtype, \
168 NS_TP, dbus_gutils_wincaps_to_uscore
171 @@ -85,18 +85,12 @@ class Generator(object):
172 self.allow_havoc = allow_havoc
175 - if isinstance(s, unicode):
176 - s = s.encode('utf-8')
177 self.__header.append(s)
180 - if isinstance(s, unicode):
181 - s = s.encode('utf-8')
182 self.__body.append(s)
185 - if isinstance(s, unicode):
186 - s = s.encode('utf-8')
187 self.__docs.append(s)
189 def do_node(self, node):
190 @@ -733,7 +727,7 @@ class Generator(object):
193 nodes = self.dom.getElementsByTagName('node')
194 - nodes.sort(cmp_by_name)
195 + nodes.sort(key=key_by_name)
197 self.h('#include <glib-object.h>')
198 self.h('#include <dbus/dbus-glib.h>')
199 @@ -763,12 +757,12 @@ class Generator(object):
203 - file_set_contents(self.basename + '.h', '\n'.join(self.__header))
204 - file_set_contents(self.basename + '.c', '\n'.join(self.__body))
205 - file_set_contents(self.basename + '-gtk-doc.h', '\n'.join(self.__docs))
206 + file_set_contents(self.basename + '.h', u('\n').join(self.__header).encode('utf-8'))
207 + file_set_contents(self.basename + '.c', u('\n').join(self.__body).encode('utf-8'))
208 + file_set_contents(self.basename + '-gtk-doc.h', u('\n').join(self.__docs).encode('utf-8'))
214 gen-ginterface [OPTIONS] xmlfile Prefix_
216 @@ -788,7 +782,7 @@ options:
217 void symbol (DBusGMethodInvocation *context)
218 and return some sort of "not implemented" error via
219 dbus_g_method_return_error (context, ...)
225 diff --git a/tools/glib-gtypes-generator.py b/tools/glib-gtypes-generator.py
226 index 21dfc6aa78fe..1477bd37bad5 100644
227 --- a/tools/glib-gtypes-generator.py
228 +++ b/tools/glib-gtypes-generator.py
231 import xml.dom.minidom
233 -from libtpcodegen import file_set_contents
234 +from libtpcodegen import file_set_contents, u
235 from libglibcodegen import escape_as_identifier, \
238 @@ -68,13 +68,13 @@ class GTypesGenerator(object):
239 self.need_other_arrays = {}
242 - self.header.append(code.encode("utf-8"))
243 + self.header.append(code)
246 - self.body.append(code.encode("utf-8"))
247 + self.body.append(code)
250 - self.docs.append(code.encode('utf-8'))
251 + self.docs.append(code)
253 def do_mapping_header(self, mapping):
254 members = mapping.getElementsByTagNameNS(NS_TP, 'member')
255 @@ -292,9 +292,9 @@ class GTypesGenerator(object):
256 self.c(' return t;\n')
259 - file_set_contents(self.output + '.h', ''.join(self.header))
260 - file_set_contents(self.output + '-body.h', ''.join(self.body))
261 - file_set_contents(self.output + '-gtk-doc.h', ''.join(self.docs))
262 + file_set_contents(self.output + '.h', u('').join(self.header).encode('utf-8'))
263 + file_set_contents(self.output + '-body.h', u('').join(self.body).encode('utf-8'))
264 + file_set_contents(self.output + '-gtk-doc.h', u('').join(self.docs).encode('utf-8'))
266 if __name__ == '__main__':
268 diff --git a/tools/glib-interfaces-gen.py b/tools/glib-interfaces-gen.py
269 index 410762cdeb11..b67d7b4f0c80 100644
270 --- a/tools/glib-interfaces-gen.py
271 +++ b/tools/glib-interfaces-gen.py
273 from sys import argv, stdout, stderr
274 import xml.dom.minidom
276 -from libtpcodegen import file_set_contents
277 +from libtpcodegen import file_set_contents, u
278 from libglibcodegen import NS_TP, get_docstring, \
279 get_descendant_text, get_by_path
281 @@ -24,22 +24,22 @@ class Generator(object):
282 self.spec = get_by_path(dom, "spec")[0]
285 - self.decls.append(code.encode('utf-8'))
286 + self.decls.append(code)
289 - self.impls.append(code.encode('utf-8'))
290 + self.impls.append(code)
293 - self.docs.append(code.encode('utf-8'))
294 + self.docs.append(code)
297 for f in self.h, self.c:
301 - file_set_contents(self.implfile, ''.join(self.impls))
302 - file_set_contents(self.declfile, ''.join(self.decls))
303 - file_set_contents(self.docfile, ''.join(self.docs))
304 + file_set_contents(self.implfile, u('').join(self.impls).encode('utf-8'))
305 + file_set_contents(self.declfile, u('').join(self.decls).encode('utf-8'))
306 + file_set_contents(self.docfile, u('').join(self.docs).encode('utf-8'))
309 def do_header(self, f):
310 diff --git a/tools/libglibcodegen.py b/tools/libglibcodegen.py
311 index 6a9d21485031..6cd1a6277841 100644
312 --- a/tools/libglibcodegen.py
313 +++ b/tools/libglibcodegen.py
314 @@ -154,7 +154,7 @@ def type_to_gtype(s):
315 return ("GHashTable *", "DBUS_TYPE_G_STRING_STRING_HASHTABLE", "BOXED", False)
316 elif s[:2] == 'a{': #some arbitrary hash tables
317 if s[2] not in ('y', 'b', 'n', 'q', 'i', 'u', 's', 'o', 'g'):
318 - raise Exception, "can't index a hashtable off non-basic type " + s
319 + raise Exception("can't index a hashtable off non-basic type " + s)
320 first = type_to_gtype(s[2])
321 second = type_to_gtype(s[3:-1])
322 return ("GHashTable *", "(dbus_g_type_get_map (\"GHashTable\", " + first[1] + ", " + second[1] + "))", "BOXED", False)
323 @@ -169,4 +169,4 @@ def type_to_gtype(s):
324 return ("GValueArray *", gtype, "BOXED", True)
326 # we just don't know ..
327 - raise Exception, "don't know the GType for " + s
328 + raise Exception("don't know the GType for " + s)
329 diff --git a/tools/libtpcodegen.py b/tools/libtpcodegen.py
330 index 7e9eb9a5078d..99de66340d78 100644
331 --- a/tools/libtpcodegen.py
332 +++ b/tools/libtpcodegen.py
333 @@ -21,6 +21,7 @@ please make any changes there.
334 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
338 from string import ascii_letters, digits
341 @@ -28,6 +29,20 @@ NS_TP = "http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"
343 _ASCII_ALNUM = ascii_letters + digits
345 +if sys.version_info[0] >= 3:
347 + """Return s, which must be a str literal with no non-ASCII characters.
348 + This is like a more restricted form of the Python 2 u'' syntax.
350 + return s.encode('ascii').decode('ascii')
353 + """Return a Unicode version of s, which must be a str literal
354 + (a bytestring) in which each byte is an ASCII character.
355 + This is like a more restricted form of the u'' syntax.
357 + return s.decode('ascii')
359 def file_set_contents(filename, contents):
362 @@ -38,13 +53,15 @@ def file_set_contents(filename, contents):
366 - open(filename + '.tmp', 'w').write(contents)
367 + open(filename + '.tmp', 'wb').write(contents)
368 os.rename(filename + '.tmp', filename)
370 def cmp_by_name(node1, node2):
371 return cmp(node1.getAttributeNode("name").nodeValue,
372 node2.getAttributeNode("name").nodeValue)
374 +def key_by_name(node):
375 + return node.getAttributeNode("name").nodeValue
377 def escape_as_identifier(identifier):
378 """Escape the given string to be a valid D-Bus object path or service
379 @@ -168,6 +185,9 @@ class _SignatureIter:
380 self.remaining = string
383 + return self.__next__()
385 + def __next__(self):
386 if self.remaining == '':
389 diff --git a/tools/xincludator.py b/tools/xincludator.py
390 index 63e106ace136..f9ed49ce475c 100644
391 --- a/tools/xincludator.py
392 +++ b/tools/xincludator.py
397 from sys import argv, stdout, stderr
398 import codecs, locale
400 import xml.dom.minidom
402 -stdout = codecs.getwriter('utf-8')(stdout)
403 +if sys.version_info[0] < 3:
404 + stdout = codecs.getwriter('utf-8')(stdout)
406 NS_XI = 'http://www.w3.org/2001/XInclude'
408 def xincludate(dom, base, dropns = []):
410 - for i in xrange(dom.documentElement.attributes.length):
411 + for i in range(dom.documentElement.attributes.length):
412 attr = dom.documentElement.attributes.item(i)
413 if attr.prefix == 'xmlns':
414 if attr.localName in dropns:
415 @@ -34,6 +36,11 @@ if __name__ == '__main__':
417 dom = xml.dom.minidom.parse(argv[0])
418 xincludate(dom, argv[0])
421 + if sys.version_info[0] >= 3:
422 + xml = dom.toxml(encoding=None)