Skip to content

Commit 9f9a295

Browse files
smilesa-maurice
authored andcommitted
Improved compatibility of generate_xml_from_google_services_json.py
Explicitly imported ctypes.wintypes so that pyinstaller will include the module correctly when building the executable and updated the script to work with Python 3's ctypes module and Python 3 strings. This still requires installation of the Universal C Runtime for Windows 7/8 installs. PiperOrigin-RevId: 289697692
1 parent ea00d1f commit 9f9a295

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed
-31.4 KB
Binary file not shown.

generate_xml_from_google_services_json.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
import sys
3131
from xml.etree import ElementTree
3232

33+
if platform.system().lower() == 'windows':
34+
import ctypes.wintypes # pylint: disable=g-import-not-at-top
35+
36+
# Map Python 2's unicode method to encode a string as bytes in python 3.
37+
try:
38+
unicode('') # See whether unicode class is available (Python < 3)
39+
except NameError:
40+
unicode = str # pylint: disable=redefined-builtin,invalid-name
41+
3342
# Input filename if it isn't set.
3443
DEFAULT_INPUT_FILENAME = 'app/google-services.json'
3544
# Output filename if it isn't set.
@@ -231,12 +240,12 @@ def argv_as_unicode_win32():
231240
command_line_to_argv_w = ctypes.windll.shell32.CommandLineToArgvW
232241
command_line_to_argv_w.argtypes = [
233242
ctypes.wintypes.LPCWSTR,
234-
ctypes.wintypes.POINTER(ctypes.wintypes.c_int)
243+
ctypes.POINTER(ctypes.c_int)
235244
]
236-
command_line_to_argv_w.restype = ctypes.wintypes.POINTER(
245+
command_line_to_argv_w.restype = ctypes.POINTER(
237246
ctypes.wintypes.LPWSTR)
238247

239-
argc = ctypes.wintypes.c_int(0)
248+
argc = ctypes.c_int(0)
240249
argv = command_line_to_argv_w(get_command_line_w(), argc)
241250

242251
# Strip the python executable from the arguments if it exists
@@ -298,18 +307,18 @@ def main():
298307
output_filename = DEFAULT_OUTPUT_FILENAME
299308

300309
if args.i:
301-
input_filename_raw = args.i
302310
# Encode the input string (type unicode) as a normal string (type str)
303311
# using the 'utf-8' encoding so that it can be worked with the same as
304312
# input names from other sources (like the defaults).
305-
input_filename = input_filename_raw.encode('utf-8')
313+
input_filename_raw = args.i.encode('utf-8')
314+
# Decode the filename to a unicode string using the 'utf-8' encoding to
315+
# properly handle filepaths with unicode characters in them.
316+
input_filename = input_filename_raw.decode('utf-8')
306317

307318
if args.o:
308319
output_filename = args.o
309320

310-
# Decode the filename to a unicode string using the 'utf-8' encoding to
311-
# properly handle filepaths with unicode characters in them.
312-
with open(input_filename.decode('utf-8'), 'r') as ifile:
321+
with open(input_filename, 'r') as ifile:
313322
file_string = ifile.read()
314323

315324
json_string = None

0 commit comments

Comments
 (0)