Skip to content

Commit 4f7da6d

Browse files
author
临寒
committed
fix file encoding problem
1 parent a2d5cfb commit 4f7da6d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

xUnique.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from urllib import urlretrieve
2727
from fileinput import (input as fi_input, close as fi_close)
2828
from re import compile as re_compile
29-
from sys import argv as sys_argv
29+
from sys import (argv as sys_argv, getfilesystemencoding as sys_get_fs_encoding)
3030
from collections import deque
3131
from filecmp import cmp as filecmp_cmp
3232

@@ -65,8 +65,8 @@ def __init__(self, xcodeproj_path):
6565

6666
def pbxproj_to_json(self):
6767
pbproj_to_json_cmd = ['plutil', '-convert', 'json', '-o', '-', self.xcode_pbxproj_path]
68-
json_str = sp_co(pbproj_to_json_cmd)
69-
return json.loads(json_str)
68+
json_unicode_str = sp_co(pbproj_to_json_cmd).decode(sys_get_fs_encoding())
69+
return json.loads(json_unicode_str)
7070

7171
def __set_to_result(self, parent_hex, current_hex, current_path_key):
7272
current_node = self.nodes[current_hex]
@@ -116,9 +116,11 @@ def replace_uuids_with_file(self):
116116
print 'replace UUIDs and remove unused UUIDs'
117117
uuid_ptn = re_compile('(?<=\s)[0-9A-F]{24}(?=[\s;])')
118118
for line in fi_input(self.xcode_pbxproj_path, backup='.bak', inplace=1):
119+
# project.pbxproj is an utf-8 encoded file
120+
line = line.decode('utf-8')
119121
uuid_list = uuid_ptn.findall(line)
120122
if not uuid_list:
121-
print line,
123+
print line.encode('utf-8'),
122124
else:
123125
new_line = line
124126
# remove line with non-existing element
@@ -128,7 +130,7 @@ def replace_uuids_with_file(self):
128130
else:
129131
for uuid in uuid_list:
130132
new_line = new_line.replace(uuid, self.__result[uuid]['new_key'])
131-
print new_line,
133+
print new_line.encode('utf-8'),
132134
fi_close()
133135
unlink(self.xcode_pbxproj_path + '.bak')
134136

@@ -186,6 +188,8 @@ def file_dir_cmp(x, y):
186188
return cmp(x, y)
187189

188190
for line in fi_input(self.xcode_pbxproj_path, backup='.bak', inplace=1):
191+
# project.pbxproj is an utf-8 encoded file
192+
line = line.decode('utf-8')
189193
last_two.append(line)
190194
if len(last_two) > 2:
191195
last_two.popleft()
@@ -200,7 +204,7 @@ def file_dir_cmp(x, y):
200204
if fc_end_ptn.search(line):
201205
if lines:
202206
lines.sort(key=lambda file_str: files_key_ptn.search(file_str).group())
203-
print ''.join(lines),
207+
print ''.join(lines).encode('utf-8'),
204208
lines = []
205209
files_flag = False
206210
fc_end_ptn = '\);'
@@ -218,7 +222,7 @@ def file_dir_cmp(x, y):
218222
if lines:
219223
if last_two[0] != self.__main_group_hex:
220224
lines.sort(key=lambda file_str: children_pbx_key_ptn.search(file_str).group(),cmp=file_dir_cmp)
221-
print ''.join(lines),
225+
print ''.join(lines).encode('utf-8'),
222226
lines = []
223227
child_flag = False
224228
fc_end_ptn = '\);'
@@ -235,7 +239,7 @@ def file_dir_cmp(x, y):
235239
if pbx_end_ptn.search(line):
236240
if lines:
237241
lines.sort(key=lambda file_str: children_pbx_key_ptn.search(file_str).group())
238-
print ''.join(lines),
242+
print ''.join(lines).encode('utf-8'),
239243
lines = []
240244
pbx_flag = False
241245
pbx_end_ptn = ('^.*End ', ' section.*')
@@ -370,6 +374,6 @@ def __unique_build_file(self, parent_hex, build_file_hex):
370374
if len(sys_argv) != 2:
371375
raise SystemExit('usage: xUnique.py path/to/Project.xcodeproj')
372376
else:
373-
xcode_proj_path = sys_argv[1]
377+
xcode_proj_path = sys_argv[1].decode(sys_get_fs_encoding())
374378
xunique = XUnique(xcode_proj_path)
375379
xunique.unique_pbxproj()

0 commit comments

Comments
 (0)