forked from groovecoder/examine-chrome-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
70 lines (55 loc) · 1.62 KB
/
utils.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
import os
import re
import shutil
import requests
from zipfile import ZipFile
def download_file(filename, url):
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
r.raise_for_status()
with open(filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
def unzip_file(destfile):
destdir = os.path.dirname(destfile)
print(destdir)
with ZipFile(destfile, 'r') as zippy:
zippy.extractall(destdir)
return destdir
regex = re.compile('(chrome|browser)((\.\w+){2,3})')
def find(filename):
count = {}
try:
data = open(filename, 'rb').read().decode()
except:
# Probably and encoding error
return
# if 'chrome.' not in data:
# return
while data:
match = regex.search(data)
if match:
group = match.group()
if group == 'chrome.google.com':
break
count.setdefault(group, 0)
count[group] += 1
data = data[match.end():]
else:
break
return count
def examine(directory):
count = {}
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.js'):
full = os.path.join(root, file)
res = find(full)
if res:
for k, v in list(res.items()):
if not k in count:
count[k] = 0
count[k] = count[k] + v
return count