forked from RT-Thread/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkgsdb.py
240 lines (200 loc) · 7.56 KB
/
pkgsdb.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# -*- coding:utf-8 -*-
#
# File : pkgsdb.py
# This file is part of RT-Thread RTOS
# COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Change Logs:
# Date Author Notes
# 2018-5-28 SummerGift Add copyright information
#
import sqlite3
import os
import hashlib
import sys
from vars import Import
SHOW_SQL = False
def GetFileMd5(filename):
if not os.path.isfile(filename):
return
myhash = hashlib.md5()
f = open(filename, 'rb')
while True:
b = f.read(8096)
if not b:
break
myhash.update(b)
f.close()
return myhash.hexdigest()
def get_conn(path):
conn = sqlite3.connect(path)
if os.path.exists(path) and os.path.isfile(path):
#print('on disk:[{}]'.format(path))
return conn
else:
conn = None
print('on memory:[:memory:]')
return sqlite3.connect(':memory:')
def close_all(conn):
if conn is not None:
conn.close()
def get_cursor(conn):
if conn is not None:
return conn.cursor()
else:
return get_conn('').cursor()
def create_table(conn, sql):
if sql is not None and sql != '':
cu = get_cursor(conn)
if SHOW_SQL:
print('执行sql:[{}]'.format(sql))
cu.execute(sql)
conn.commit()
#print('create data table successful!')
close_all(conn)
else:
print('the [{}] is empty or equal None!'.format(sql))
def save(conn, sql, data):
'''insert data to database'''
if sql is not None and sql != '':
if data is not None:
cu = get_cursor(conn)
for d in data:
if SHOW_SQL:
print('execute sql:[{}],arguments:[{}]'.format(sql, d))
cu.execute(sql, d)
conn.commit()
close_all(conn)
else:
print('the [{}] is empty or equal None!'.format(sql))
def isdataexist(pathname):
ret = True
dbpathname = Import('dbsqlite_pathname')
conn = get_conn(dbpathname)
c = get_cursor(conn)
sql = 'SELECT md5 from packagefile where pathname = "' + pathname + '"'
cursor = c.execute(sql)
for row in cursor:
dbmd5 = row[0]
if dbmd5:
ret = False
conn.close()
return ret
#将数据添加到数据库,如果数据库中已经存在则不重复添加
def savetodb(pathname, pkgspathname, before_change_name):
dbpathname = Import('dbsqlite_pathname')
bsp_root = Import('bsp_root')
bsppkgs = os.path.join(bsp_root, 'packages')
conn = get_conn(dbpathname)
save_sql = '''insert into packagefile values (?, ?, ?)'''
package = os.path.basename(pkgspathname)
md5pathname = os.path.join(bsppkgs, before_change_name)
#print("pathname to save : %s"%pathname)
#print("md5pathname : %s"%md5pathname)
if not os.path.isfile(md5pathname):
print("md5pathname is Invalid")
md5 = GetFileMd5(md5pathname)
#print("md5 to save : %s"%md5)
data = [(pathname, package, md5)]
save(conn, save_sql, data)
def dbdump(dbpathname):
conn = get_conn(dbpathname)
c = get_cursor(conn)
cursor = c.execute("SELECT pathname, package, md5 from packagefile")
for row in cursor:
print("pathname = ", row[0])
print("package = ", row[1])
print("md5 = ", row[2], "\n")
conn.close()
#delete the unchanged file
def remove_unchangedfile(pathname, dbpathname, dbsqlname):
flag = True
conn = get_conn(dbpathname)
c = get_cursor(conn)
#print('pathname : %s'%pathname)
#print('dbsqlname : %s'%dbsqlname)
filemd5 = GetFileMd5(pathname)
#print("filemd5 : %s"%filemd5)
dbmd5 = 0
sql = 'SELECT md5 from packagefile where pathname = "' + dbsqlname + '"'
#print sql
cursor = c.execute(sql)
for row in cursor:
dbmd5 = row[0] # fetch md5 from databas
#print("dbmd5 : %s"%dbmd5)
if dbmd5 == filemd5:
# delete file info from database
sql = "DELETE from packagefile where pathname = '" + dbsqlname + "'"
cursor = c.execute(sql)
conn.commit()
os.remove(pathname)
else:
print ("%s has been changed." % pathname)
print ('Are you sure you want to permanently delete the file: %s?' %
os.path.basename(pathname))
print ('If you choose to keep the changed file,you should copy the file to another folder. \nbecaues it may be covered by the next update.')
if sys.version_info < (3, 0):
rc = raw_inuput('Press the Y Key to delete the file or just press Enter to keep the file.')
else:
rc = input('Press the Y Key to delete the file or just press Enter to keep the file.')
if rc == 'y' or rc == 'Y':
sql = "DELETE from packagefile where pathname = '" + dbsqlname + "'"
cursor = c.execute(sql)
conn.commit()
os.remove(pathname)
print("%s has been removed.\n" % pathname)
else:
flag = False
conn.close()
return flag
#删除一个包,如果有文件被改动,则提示(y/n)是否要删除,输入y则删除文件,输入其他字符则保留文件。
#如果没有文件被改动,直接删除文件夹,包文件夹被完全删除返回true,有被修改的文件没有被删除返回false
def deletepackdir(dirpath, dbpathname):
flag = getdirdisplay(dirpath, dbpathname)
if flag == True:
if os.path.exists(dirpath):
for root, dirs, files in os.walk(dirpath, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(dirpath)
#print "the dir should be delete"
return flag
#遍历filepath下所有文件,包括子目录
def displaydir(filepath, basepath, length, dbpathname):
flag = True
if os.path.isdir(filepath):
files = os.listdir(filepath)
for fi in files:
fi_d = os.path.join(filepath, fi)
if os.path.isdir(fi_d):
displaydir(fi_d, basepath, length, dbpathname)
else:
pathname = os.path.join(filepath, fi_d)
dbsqlname = basepath + os.path.join(filepath, fi_d)[length:]
#print("dbsqlname : %s"%dbsqlname)
if not remove_unchangedfile(pathname, dbpathname, dbsqlname):
flag = False
return flag
def getdirdisplay(filepath, dbpathname):
display = filepath
length = len(display)
basepath = os.path.basename(filepath)
#print "basepath:",basepath
flag = displaydir(filepath, basepath, length, dbpathname)
return flag