|
1 # Copyright (c) 2009 Symbian Foundation Ltd |
|
2 # This component and the accompanying materials are made available |
|
3 # under the terms of the License "Eclipse Public License v1.0" |
|
4 # which accompanies this distribution, and is available |
|
5 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
6 # |
|
7 # Initial Contributors: |
|
8 # Symbian Foundation Ltd - initial contribution. |
|
9 # |
|
10 # Contributors: |
|
11 # mattd <mattd@symbian.org> |
|
12 # |
|
13 # Description: |
|
14 # DBRbaseline - module for handling vanilla baselines |
|
15 # |
|
16 |
|
17 |
|
18 import re |
|
19 import os |
|
20 import string |
|
21 from os.path import join, isfile, stat |
|
22 from stat import * |
|
23 import dbrutils |
|
24 |
|
25 |
|
26 |
|
27 def readdb(dbfile): |
|
28 db = dict() |
|
29 if(isfile(dbfile)): |
|
30 file = open(dbfile,'r') |
|
31 # regex = re.compile('(\S+)\s+(\S+)\s+(\S+)\s+(.+)\n') |
|
32 for line in file: |
|
33 #file structure 'timestamp size hash filename' avoids the problems of spaces in names, etc... |
|
34 results = re.split(':|\n',line) |
|
35 if(len(results) > 3): |
|
36 entry = dict() |
|
37 entry['time'] = results[0] |
|
38 entry['size'] = results[1] |
|
39 entry['md5'] = results[2] |
|
40 if(results[4]): |
|
41 entry['archive'] = results[4] |
|
42 print entry['archive'] |
|
43 db[results[3]] = entry |
|
44 # db[results[3]] = [results[0],results[1],results[2]] |
|
45 # bits = regex.match(line) |
|
46 # if(bits): |
|
47 # db[bits.group(3)] = [bits.group(0), bits.group(1), bits.group(2)] |
|
48 file.close() |
|
49 return db |
|
50 |
|
51 def writedb(db, dbfile): |
|
52 # print 'Writing db to', dbfile |
|
53 file = open(dbfile,'w') |
|
54 for filename in sorted(db): |
|
55 if (len(db[filename]) < 3): |
|
56 db[filename].append('') |
|
57 str = "%s:%s:%s:%s" %( db[filename]['time'],db[filename]['size'],db[filename]['md5'], filename) |
|
58 if('archive' in db[filename]): |
|
59 str = "%s:%s" %(str,db[filename]['archive']) |
|
60 # if(db[filename]['md5'] == 'xxx'): |
|
61 # print 'Warning: no MD5 for %s' % filename |
|
62 # str = "%s:%s:%s:%s\n" %( db[filename][0],db[filename][1],db[filename][2], filename) |
|
63 file.write('%s\n' % str) |
|
64 file.close() |
|
65 |
|
66 def md5test(db, md5testset): |
|
67 changed = set() |
|
68 md5s = dbrutils.generateMD5s(md5testset) |
|
69 for file in md5testset: |
|
70 if(db[file]['md5'] != md5s[file]['md5']): |
|
71 changed.add(file) |
|
72 return changed |
|
73 |
|
74 |
|
75 def updatedb(db1, db2): |
|
76 compareupdatedb(db1, db2, 1) |
|
77 |
|
78 def comparedb(db1, db2): |
|
79 compareupdatedb(db1, db2, 0) |
|
80 |
|
81 def compareupdatedb(db1, db2, update): |
|
82 print "compareupdatedb() is deprecated" |
|
83 db1files = set(db1.keys()) |
|
84 db2files = set(db2.keys()) |
|
85 removed = db1files - db2files |
|
86 added = db2files - db1files |
|
87 common = db1files & db2files |
|
88 |
|
89 touched = set() |
|
90 for file in common: |
|
91 if(db1[file]['time'] != db2[file]['time']): |
|
92 touched.add(file) |
|
93 |
|
94 sizechanged = set() |
|
95 for file in common: |
|
96 if(db1[file]['size'] != db2[file]['size']): |
|
97 sizechanged.add(file) |
|
98 |
|
99 #pobably won't bother with size changed... we know they're different... |
|
100 # md5testset = touched - sizechanged |
|
101 md5testset = touched |
|
102 |
|
103 changed = md5test(db1,md5testset) |
|
104 |
|
105 #remove the ones we know are changed |
|
106 touched = touched - changed |
|
107 |
|
108 print 'Comparing dbs/n' |
|
109 for file in sorted(added): |
|
110 print 'added:', file |
|
111 for file in sorted(removed): |
|
112 print 'removed:', file |
|
113 for file in sorted(touched): |
|
114 print 'touched:', file |
|
115 for file in sorted(changed): |
|
116 print 'changed:', file |
|
117 |
|
118 #update the touched... |
|
119 if(update): |
|
120 for file in sorted(touched): |
|
121 print 'Updating timestamp for: ',file |
|
122 db1[file]['time'] = db2[file]['time'] |