-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_scan.py
128 lines (96 loc) · 3.1 KB
/
file_scan.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
__author__ = 'Artem Nurgaliev [email protected]'
import os
import sys
from os import path
import re
import struct
import hashlib
import threading
import json
import sqlite3 as lite
def read_file(name):
data = open('myfile.zip', 'rb').read()
start = 0
for i in range(3): # show the first 3 file headers
start += 14
fields = struct.unpack('<IIIHH', data[start:start+16])
crc32, comp_size, uncomp_size, filenamesize, extra_size = fields
start += 16
filename = data[start:start+filenamesize]
start += filenamesize
extra = data[start:start+extra_size]
print filename, hex(crc32), comp_size, uncomp_size
start += extra_size + comp_size # skip to the next header
def hashfile(afile, hasher, blocksize=65536):
buf = afile.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = afile.read(blocksize)
return hasher.hexdigest()
def scan_package(pkg_name, packages):
root = os.walk(path.join(pkg_name, "images"))
blocks = []
for cur_dir, dirs, files in root:
# [(fname, hashfile(open(fname, 'rb'), hashlib.sha256())) for fname in files]
for name in files:
fname = path.join(cur_dir, name)
digest = hashfile(open(fname, 'rb'), hashlib.md5())
#print("File: %s - %s" % (fname, digest))
block = {}
block["name"] = fname
block["crc"] = digest
blocks.append(block)
packages.append(blocks)
class ScanPackages(threading.Thread):
def __init__(self, dir_name):
threading.Thread.__init__(self)
self.dir_name = dir_name
self.packages = []
def run(self):
fname = r'.*_meta\.xml'
root = os.walk(self.dir_name)
print("Scan started: %s" % self.dir_name)
for cur_dir, dirs, files in root:
for name in files:
#print("File: %s" % join(cur_dir, name))
if re.match(fname, name):
#print("File: %s" % join(cur_dir, name))
scan_package(cur_dir, self.packages)
print("Scan complete")
output_file = "output.json"
print("Writing JSON data: %s" % output_file)
data = open(output_file, "w")
data.write(json.dumps(self.packages))
data.close()
print("Write colmplete")
def main_scan():
print("Application started")
background = ScanPackages("../PAPG/build/papgg5/cdimage")
background.start()
print("Patience...")
background.join()
def main_json():
data = open("output.json", "r")
packages = json.loads(data.read())
data.close()
for files in packages:
for file in files:
print("File: %s - %s" % (file["name"], file["crc"]))
def main_sqlite():
print("testing sqlite")
con = lite.connect('packages.db')
with con:
cur = con.cursor()
cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")
cur.execute("INSERT INTO Cars VALUES(1,'Audi',52642)")
cur.execute("INSERT INTO Cars VALUES(2,'Mercedes',57127)")
cur.execute("INSERT INTO Cars VALUES(3,'Skoda',9000)")
cur.execute("INSERT INTO Cars VALUES(4,'Volvo',29000)")
cur.execute("INSERT INTO Cars VALUES(5,'Bentley',350000)")
cur.execute("INSERT INTO Cars VALUES(6,'Citroen',21000)")
cur.execute("INSERT INTO Cars VALUES(7,'Hummer',41400)")
cur.execute("INSERT INTO Cars VALUES(8,'Volkswagen',21600)")
if __name__ == "__main__":
main_scan()
#main_json()
#main_sqlite()