forked from greatagent/ga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
206 lines (175 loc) · 5.82 KB
/
common.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
# coding:utf-8
# common.py
# Author: Wang Wei Qiang <[email protected]>
__config__ = 'autoupdate.ini'
__file__ = 'autoupdate.py'
import sys
import os
import glob
sys.path += glob.glob('%s/*.egg' % os.path.dirname(os.path.abspath(__file__)))
sys.path += glob.glob('%s/lib/*.egg' % os.path.dirname(os.path.abspath(__file__)))
try:
if 'threading' in sys.modules:
del sys.modules['threading']
import gevent
import gevent.socket
import gevent.monkey
gevent.monkey.patch_all()
except (ImportError, SystemError):
gevent = None
try:
import OpenSSL
except ImportError:
OpenSSL = None
import re
import ConfigParser
import hashlib
import random
import shutil
import rsa,base92,pyasn1
class FileUtil(object):
@staticmethod
def getfile(filename):
global __file__
__file__ = os.path.abspath(__file__)
if os.path.islink(__file__):
__file__ = getattr(os, 'readlink', lambda x:x)(__file__)
os.chdir(os.path.dirname(os.path.abspath(__file__)))
return os.path.join(os.path.dirname(__file__), filename)
@staticmethod
def has_file(filename):
return os.path.isfile(FileUtil.getfile(filename))
@staticmethod
def delete_dir(dir):
if os.path.exists(dir):
shutil.rmtree(dir)
@staticmethod
def if_has_file_remove(filename,showinfo = False):
if os.path.isfile(FileUtil.getfile(filename)):
try :
os.remove(FileUtil.getfile(filename))
if showinfo:
print 'Remove '+filename+' OK!'
except :
return
@staticmethod
def get_file_sha1(f):
m = hashlib.sha1()
while True:
data = f.read(10240)
if not data:
break
m.update(data)
return m.hexdigest()
@staticmethod
def get_data_sha1(data):
m = hashlib.sha1(data)
return m.hexdigest()
@staticmethod
def sumfile(fpath):
input = FileUtil.open(fpath,'rb')
sum = FileUtil.get_file_sha1(input)
input.close()
return sum
@staticmethod
def cur_file_dir():
path = sys.path[0]
if os.path.isdir(path):
return path
elif os.path.isfile(path):
return os.path.dirname(path)
@staticmethod
def open(path,type):
path = path.replace('\\','/')
if type.endswith('w') or type.startswith('w'):
pathdir = ''
paths = path.split('/')
i = 1
for str in paths:
if i == len(paths):
break
if not str == '':
pathdir += '/'+str
i = i+1
str = ''
pathdir = pathdir[1:]
if not os.path.isdir(pathdir) and not pathdir == '':
pathdir = pathdir.replace((FileUtil.cur_file_dir()).replace('\\','/')+'/','')
dir = ''
for str in pathdir.split('/'):
if not str == '':
dir = dir+'/'+str
if dir.startswith('/'):
dir = dir[1:]
try :
os.mkdir(dir)
print 'MakeDir '+dir+' OK!'
except :
#print 'MakeDir '+dir+' OK!'
continue
if path.endswith(sysconfig.REGEX_ONLYW)or type.endswith('b'):
return open(path,type)
else:
return open(path,type+"b")
elif type.endswith('r') or type.startswith('r'):
if path.endswith(sysconfig.REGEX_ONLYR)or type.endswith('b'):
return open(path,type)
else:
return open(path,type+"b")
else:
return
class MyConfigParser(ConfigParser.ConfigParser):
def optionxform(self, optionstr):
return optionstr
class Config(object):
def __init__(self,config):
"""load config from proxy.ini"""
ConfigParser.RawConfigParser.OPTCRE = re.compile(r'(?P<option>[^=\s][^=]*)\s*(?P<vi>[=])\s*(?P<value>.*)$')
self.FILENAME = config
self.CONFIG = MyConfigParser()
self.CONFIG.read(FileUtil.getfile(config))
def writeconfig(self,section, option,str):
if not self.CONFIG.has_section(section):
self.CONFIG.add_section(section)
self.CONFIG.set(section,option,str)
f = FileUtil.open(FileUtil.getfile(self.FILENAME),'w')
self.CONFIG.write(f)
f.close()
def getconfig(self,section, option):
return self.CONFIG.get(section, option)if self.CONFIG.has_option(section, option) else ''
def getsection(self,section):
return self.CONFIG.items(section) if self.CONFIG.has_section(section) else ''
class Common(object):
"""Global Config Object"""
def __init__(self):
"""load config from ini"""
self.CONFIG = Config(__config__).CONFIG
self.AUTOUPDATE_SERVER_STR = self.CONFIG.get('autoupdate', 'server')
self.AUTOUPDATE_SERVER = self.CONFIG.get('autoupdate',self.AUTOUPDATE_SERVER_STR ).split('|')
self.REGEX_START = tuple(x for x in self.CONFIG.get('regex', 'start').split('|') if x)
self.REGEX_END = tuple(x for x in self.CONFIG.get('regex', 'end').split('|') if x)
self.REGEX_ONLYW = tuple(x for x in self.CONFIG.get('regex', 'onlyw').split('|') if x)
self.REGEX_ONLYR = tuple(x for x in self.CONFIG.get('regex', 'onlyr').split('|') if x)
self.CONFIG_SHA1 = self.CONFIG.get('config', 'sha1')
self.CONFIG_SIGN = self.CONFIG.get('config', 'sign')
self.CONFIG_NEEDCLEAN = self.CONFIG.get('config', 'needclean')
self.CONFIG_GIT = self.CONFIG.get('config', 'git')
self.CONFIG_VERSIONFILE = self.CONFIG.get('config', 'versionfile')
self.CONFIG_PUBKEY = self.CONFIG.get('config', 'pubkey')
self.CONFIG_PRIKEY = self.CONFIG.get('config', 'prikey')
self.CONFIG_AUTHOR = self.CONFIG.get('config', 'author')
self.CONFIG_NAMES = self.CONFIG.get('config', 'names')
self.CONFIG_VERSION = self.CONFIG.get('config', 'version')
def reloadini(self):
self.__init__()
def info(self):
random.shuffle(self.AUTOUPDATE_SERVER)
info = ''
info += '------------------------------------------------------\n'
info += 'GreatAgent Version : %s \n'% (self.CONFIG_VERSION)
info += 'RunTime :python/%s %spyopenssl/%s rsa/%s PyASN1/%s base92/%s\n' % (sys.version[:5], gevent and 'gevent/%s ' % gevent.__version__ or '', getattr(OpenSSL, '__version__', 'Disabled'),rsa.__version__,pyasn1.__version__,base92.__version__)
info += 'Update Server : %s\n' % '|'.join(self.AUTOUPDATE_SERVER)
info += '------------------------------------------------------\n'
return info
sysconfig = Common()