forked from fffonion/MAClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cross_platform.py
161 lines (149 loc) · 5.94 KB
/
cross_platform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
# coding:utf-8
# maclient compatibility tool
# Contributor:
# fffonion <[email protected]>
import os
import os.path as opath
import sys
import locale
PYTHON3 = sys.version.startswith('3')
IRONPYTHON = sys.platform == 'cli'
EXEBUNDLE = opath.split(sys.argv[0])[1].find('py') == -1
LOCALE = locale.getdefaultlocale()[0]
CODEPAGE = locale.getdefaultlocale()[1]
NICE_TERM = 'NICE_TERM' in os.environ
ANDROID = 'ANDROID_ARGUMENT' in os.environ
convhans = lambda x:x
try:
import ZhConversion
except ImportError:
pass
else:
chans = ZhConversion.convHans()
if LOCALE == 'zh_TW':
convhans = chans.toTW
elif LOCALE == 'zh_HK':
convhans = chans.toHK
if PYTHON3:
import imp
reload = imp.reload
xrange = range
getPATH0 = (not EXEBUNDLE or IRONPYTHON) and \
(PYTHON3 and \
sys.path[0] # python3
or sys.path[0].decode(sys.getfilesystemencoding())
) \
or sys.path[1].decode(sys.getfilesystemencoding()) # pyinstaller build
if ANDROID:
sys.path.insert(0, opath.join(getPATH0, 'modules.zip'))
raw_du8 = (IRONPYTHON or PYTHON3) and \
(lambda str:str) or \
(lambda str:convhans(str).decode('utf-8'))
safestr = (sys.platform.startswith('cli') or PYTHON3 or NICE_TERM)and \
(lambda str: str) or \
(lambda str: str.decode('utf-8').encode(locale.getdefaultlocale()[1] or 'utf-8', 'replace'))
du8 = lambda x: safestr(raw_du8(x))
raw_inputd = PYTHON3 and \
(lambda s:input(s)) \
or \
(lambda s:raw_input(raw_du8(s).encode(CODEPAGE or 'utf-8')).decode(CODEPAGE or 'utf-8').encode('utf-8'))
# from goagent.appcfg
def _win_getpass(prompt='Password:', stream=None):
password = ''
sys.stdout.write(prompt)
while 1:
ch = msvcrt.getch()
if ch == '\b':
if password:
password = password[:-1]
sys.stdout.write('\b \b')
else:
continue
elif ch == '\r':
sys.stdout.write(os.linesep)
return password
else:
password += ch
sys.stdout.write('*')
try:
import msvcrt
getpass = _win_getpass
except ImportError:
import getpass
getpass = getpass.getpass
wsa_errors = None
if sys.platform.startswith('win'):
wsa_errors = {
10004:'中断函数调用',
10013:'权限被拒绝',
10014:'错误的地址',
10022:'参数无效',
10024:'打开的文件太多',
10035:'资源暂时不可用',
10036:'现在正在进行的操作',
10037:'操作已在进行',
10038:'套接字操作非插座进行插座上的',
10039:'所需的目标地址',
10040:'消息太长',
10041:'协议的套接字类型不正确',
10042:'错误的协议选项',
10043:'不支持的协议',
10044:'套接字类型不受支持',
10045:'不支持此操作',
10046:'协议系列不支持',
10047:'系列协议系列不支持地址',
10048:'地址已在使用中',
10049:'无法分配请求的地址',
10050:'网络出现故障',
10051:'网络不可访问',
10052:'网络中断连接重置',
10053:'软件造成连接终止',
10054:'连接被对等方重置',
10055:'没有可用的缓冲区空间',
10056:'套接字已连接',
10057:'套接字未连接',
10058:'套接字关闭后无法发送',
10060:'连接已超时',
10061:'连接被拒绝',
10064:'主机已关闭',
10065:'没有到主机的路由',
10067:'进程太多。',
10091:'网络子系统不可用。',
10092:'超出范围的 Winsock.dll 版本',
10093:'还没有执行成功的 WSAStartup',
10101:'正在进行的正常关机',
10109:'类找不到类型',
11001:'找不到主机。没有此主机不存在。',
11002:'没有发现非权威主机',
11003:'这是一个不可恢复的错误。',
11004:'有效的名称、 请求类型的任何数据记录'
}
def try_load_native(mod_name):#little hack on normal import routine
if mod_name in sys.modules:
return sys.modules[mod_name]
android_internal_mod_name = '/data/data/com.hipipal.qpyplus/files/lib/python2.7/site-packages/%s.so' % mod_name
#copy new files if has former library is in internal storage
# if ANDROID and opath.exists(android_internal_mod_name) and opath.exists('%s.so' % mod_name) and \
# os.stat('%s.so' % mod_name).st_mtime > os.stat(android_internal_mod_name).st_mtime:
# with open(android_internal_mod_name, 'wb') as f:
# f.write(open('%s.so' % mod_name, 'rb').read())
try:
mod = __import__(mod_name)
except ImportError as ex:
if str(ex).find('failed to map segment') and ANDROID:#try to link library in noexec dir
mod_dir = '.'
if not opath.exists(opath.join('.', '%s.so' % mod_name)):
mod_dir = os.environ['PYTHONPATH'].split(':')[-1]#fix path when import is naked(comes with initial)
# python seems to load module in the same directory first, so if we failed here,
# we can assume that this a newer native library in (noexec) sdcard
with open(android_internal_mod_name, 'wb') as f:
f.write(open(opath.join(mod_dir, '%s.so' % mod_name), 'rb').read())
if opath.exists(opath.join(mod_dir, '%s.s_' % mod_name)):
os.remove(opath.join(mod_dir, '%s.s_' % mod_name))
os.rename(opath.join(mod_dir, '%s.so' % mod_name), opath.join(mod_dir, '%s.s_' % mod_name))
mod = __import__(mod_name) #throw exception now, it's the library problem
else:
raise ImportError(str(ex))
#globals()[mod_name] = mod
return mod