Skip to content

Commit

Permalink
fix file encoding problem
Browse files Browse the repository at this point in the history
  • Loading branch information
临寒 committed Aug 4, 2014
1 parent a2d5cfb commit 4f7da6d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions xUnique.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from urllib import urlretrieve
from fileinput import (input as fi_input, close as fi_close)
from re import compile as re_compile
from sys import argv as sys_argv
from sys import (argv as sys_argv, getfilesystemencoding as sys_get_fs_encoding)
from collections import deque
from filecmp import cmp as filecmp_cmp

Expand Down Expand Up @@ -65,8 +65,8 @@ def __init__(self, xcodeproj_path):

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

def __set_to_result(self, parent_hex, current_hex, current_path_key):
current_node = self.nodes[current_hex]
Expand Down Expand Up @@ -116,9 +116,11 @@ def replace_uuids_with_file(self):
print 'replace UUIDs and remove unused UUIDs'
uuid_ptn = re_compile('(?<=\s)[0-9A-F]{24}(?=[\s;])')
for line in fi_input(self.xcode_pbxproj_path, backup='.bak', inplace=1):
# project.pbxproj is an utf-8 encoded file
line = line.decode('utf-8')
uuid_list = uuid_ptn.findall(line)
if not uuid_list:
print line,
print line.encode('utf-8'),
else:
new_line = line
# remove line with non-existing element
Expand All @@ -128,7 +130,7 @@ def replace_uuids_with_file(self):
else:
for uuid in uuid_list:
new_line = new_line.replace(uuid, self.__result[uuid]['new_key'])
print new_line,
print new_line.encode('utf-8'),
fi_close()
unlink(self.xcode_pbxproj_path + '.bak')

Expand Down Expand Up @@ -186,6 +188,8 @@ def file_dir_cmp(x, y):
return cmp(x, y)

for line in fi_input(self.xcode_pbxproj_path, backup='.bak', inplace=1):
# project.pbxproj is an utf-8 encoded file
line = line.decode('utf-8')
last_two.append(line)
if len(last_two) > 2:
last_two.popleft()
Expand All @@ -200,7 +204,7 @@ def file_dir_cmp(x, y):
if fc_end_ptn.search(line):
if lines:
lines.sort(key=lambda file_str: files_key_ptn.search(file_str).group())
print ''.join(lines),
print ''.join(lines).encode('utf-8'),
lines = []
files_flag = False
fc_end_ptn = '\);'
Expand All @@ -218,7 +222,7 @@ def file_dir_cmp(x, y):
if lines:
if last_two[0] != self.__main_group_hex:
lines.sort(key=lambda file_str: children_pbx_key_ptn.search(file_str).group(),cmp=file_dir_cmp)
print ''.join(lines),
print ''.join(lines).encode('utf-8'),
lines = []
child_flag = False
fc_end_ptn = '\);'
Expand All @@ -235,7 +239,7 @@ def file_dir_cmp(x, y):
if pbx_end_ptn.search(line):
if lines:
lines.sort(key=lambda file_str: children_pbx_key_ptn.search(file_str).group())
print ''.join(lines),
print ''.join(lines).encode('utf-8'),
lines = []
pbx_flag = False
pbx_end_ptn = ('^.*End ', ' section.*')
Expand Down Expand Up @@ -370,6 +374,6 @@ def __unique_build_file(self, parent_hex, build_file_hex):
if len(sys_argv) != 2:
raise SystemExit('usage: xUnique.py path/to/Project.xcodeproj')
else:
xcode_proj_path = sys_argv[1]
xcode_proj_path = sys_argv[1].decode(sys_get_fs_encoding())
xunique = XUnique(xcode_proj_path)
xunique.unique_pbxproj()

0 comments on commit 4f7da6d

Please sign in to comment.