/
Tools.py
167 lines (143 loc) · 4.9 KB
/
Tools.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
# -*- coding: UTF-8 -*-
# 在本文件中定义了一些操控配置和数据文件的工具
__author__ = 'FinalTheory'
from ConfigParser import ConfigParser
from GlobalDefs import *
import sqlite3
import socket
import sys
import web
web.config.debug = False
import os
from threading import Lock
# 首先是一些辅助函数的定义:
def check_path(path):
if not os.path.exists(path):
os.makedirs(path)
def get_dir_size(dir_path):
size = 0
nums = 0
for root, dirs, files in os.walk(dir_path):
size += sum([os.path.getsize(os.path.join(root, name)) for name in files])
nums += len(files)
return size, nums
def check_port(port, ip='127.0.0.1', timeout=0.1):
port = int(port)
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(timeout)
result = True
try:
sk.connect((ip, port))
except Exception:
result = False
sk.close()
return result
def check_connect():
address = cfg.read('check_server_ip')
port = int(cfg.read('check_server_port'))
time_out = float(cfg.read('check_server_time_out'))
if check_port(port, address, time_out):
return True
return False
# 下面是数据库处理类和配置文件处理类
class DataBaseManager():
def __init__(self):
# Try to open database file, if failed then create a new one
try:
open(cfg.read('db_filename'), 'r').close()
except:
self.InitDataBase()
sys.stderr.write('Warning: database not found, created a new one!\n')
self.mutex = Lock()
# Method to create a entire new database
def InitDataBase(self):
open(cfg.read('db_filename'), 'w').close()
with open(cfg.read('sql_filename')) as fid,\
sqlite3.connect(cfg.read('db_filename')) as conn:
conn.text_factory = str
for sql in fid.read().split('\n\n'):
if len(sql) > 3:
conn.execute(sql)
conn.commit()
def Execute(self, sql):
self.mutex.acquire()
# connect to the database
with sqlite3.connect(cfg.read('db_filename')) as conn:
conn.text_factory = str
conn.execute(sql)
conn.commit()
self.mutex.release()
def Query(self, sql):
with sqlite3.connect(cfg.read('db_filename')) as conn:
conn.text_factory = str
cursor = conn.execute(sql)
return cursor.fetchall()
def QueryFirst(self, sql):
with sqlite3.connect(cfg.read('db_filename')) as conn:
conn.text_factory = str
cursor = conn.execute(sql)
return cursor.fetchone()
class ConfigLoader():
def __init__(self):
try:
open(cfg_name, 'r').close()
except:
sys.stderr.write(u'严重错误,无法读取配置文件!程序自动退出。\n')
exit(-1)
self.config = ConfigParser()
self.config.read(cfg_name)
self.system = dict(self.config.items('system'))
self.config_check()
def config_check(self):
try:
# 首先检查设定的全局存储目录是否合法
check_path(self.read('global_pos'))
# 然后检查管理员的下载目录是否存在
root_path = os.path.join(self.read('global_pos'), 'root')
if not os.path.exists(root_path):
os.mkdir(root_path)
except Exception, err:
sys.stderr.write(u'系统错误,原因:%s\n' % err)
exit(-1)
# 接下来检查端口是否可用
if check_port(self.read('port_name')):
sys.stderr.write(u'系统错误,端口被占用!\n')
exit(-1)
def read(self, key):
result = self.system.get(key)
if type(result) == str:
result = result.decode('gb18030')
return result
def write(self, key, value):
if type(value) == unicode:
value = value.encode('gb18030')
self.system[key] = value
self.config.set('system', key, value)
self.config.write(open(cfg_name, "w"))
class LogManager:
def __init__(self):
open(cfg.read('log_filename'), 'w').close()
self.mutex = Lock()
def log_message(self, msg):
self.mutex.acquire()
with open(cfg.read('log_filename'), 'a') as fid:
# 自动加上换行
if msg[-1] != '\n':
msg += '\n'
if type(msg) == unicode:
msg = msg.encode('gb18030')
fid.write(msg)
self.mutex.release()
# In debug mode, web.py will load global variable twice,
# so we have to use this trick to avoid this.
if web.config.get('_db') is None:
cfg = ConfigLoader()
log = LogManager()
db = DataBaseManager()
web.config._cfg = cfg
web.config._log = log
web.config._db = db
else:
cfg = web.config._cfg
log = web.config._log
db = web.config._db