26
26
from urllib import urlretrieve
27
27
from fileinput import (input as fi_input , close as fi_close )
28
28
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 )
30
30
from collections import deque
31
31
from filecmp import cmp as filecmp_cmp
32
32
@@ -65,8 +65,8 @@ def __init__(self, xcodeproj_path):
65
65
66
66
def pbxproj_to_json (self ):
67
67
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 )
70
70
71
71
def __set_to_result (self , parent_hex , current_hex , current_path_key ):
72
72
current_node = self .nodes [current_hex ]
@@ -116,9 +116,11 @@ def replace_uuids_with_file(self):
116
116
print 'replace UUIDs and remove unused UUIDs'
117
117
uuid_ptn = re_compile ('(?<=\s)[0-9A-F]{24}(?=[\s;])' )
118
118
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' )
119
121
uuid_list = uuid_ptn .findall (line )
120
122
if not uuid_list :
121
- print line ,
123
+ print line . encode ( 'utf-8' ) ,
122
124
else :
123
125
new_line = line
124
126
# remove line with non-existing element
@@ -128,7 +130,7 @@ def replace_uuids_with_file(self):
128
130
else :
129
131
for uuid in uuid_list :
130
132
new_line = new_line .replace (uuid , self .__result [uuid ]['new_key' ])
131
- print new_line ,
133
+ print new_line . encode ( 'utf-8' ) ,
132
134
fi_close ()
133
135
unlink (self .xcode_pbxproj_path + '.bak' )
134
136
@@ -186,6 +188,8 @@ def file_dir_cmp(x, y):
186
188
return cmp (x , y )
187
189
188
190
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' )
189
193
last_two .append (line )
190
194
if len (last_two ) > 2 :
191
195
last_two .popleft ()
@@ -200,7 +204,7 @@ def file_dir_cmp(x, y):
200
204
if fc_end_ptn .search (line ):
201
205
if lines :
202
206
lines .sort (key = lambda file_str : files_key_ptn .search (file_str ).group ())
203
- print '' .join (lines ),
207
+ print '' .join (lines ). encode ( 'utf-8' ) ,
204
208
lines = []
205
209
files_flag = False
206
210
fc_end_ptn = '\);'
@@ -218,7 +222,7 @@ def file_dir_cmp(x, y):
218
222
if lines :
219
223
if last_two [0 ] != self .__main_group_hex :
220
224
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' ) ,
222
226
lines = []
223
227
child_flag = False
224
228
fc_end_ptn = '\);'
@@ -235,7 +239,7 @@ def file_dir_cmp(x, y):
235
239
if pbx_end_ptn .search (line ):
236
240
if lines :
237
241
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' ) ,
239
243
lines = []
240
244
pbx_flag = False
241
245
pbx_end_ptn = ('^.*End ' , ' section.*' )
@@ -370,6 +374,6 @@ def __unique_build_file(self, parent_hex, build_file_hex):
370
374
if len (sys_argv ) != 2 :
371
375
raise SystemExit ('usage: xUnique.py path/to/Project.xcodeproj' )
372
376
else :
373
- xcode_proj_path = sys_argv [1 ]
377
+ xcode_proj_path = sys_argv [1 ]. decode ( sys_get_fs_encoding ())
374
378
xunique = XUnique (xcode_proj_path )
375
379
xunique .unique_pbxproj ()
0 commit comments