-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathpy-2.py
More file actions
92 lines (76 loc) · 2.29 KB
/
py-2.py
File metadata and controls
92 lines (76 loc) · 2.29 KB
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
#!/usr/bin/env python
import leveldb
import sys
import re
import json
import codecs
import os
import shutil
import glob
import zipfile
import traceback
logf = open("github-action-run.log", "w", encoding="utf8")
def log(msg):
print(msg)
logf.write(str(msg)+"\n")
def logAndClose(msg):
print(msg)
logf.write(str(msg)+"\n")
logf.close()
f = sys.argv[1].strip()
log(f"arg1 is:{f}")
log(f"pwd:{os.getcwd()}")
unpack_dir="unpack-output"
shutil.rmtree(unpack_dir, ignore_errors=True)
if os.path.exists(f) and os.path.isfile(f):
try:
shutil.unpack_archive(f, unpack_dir)
except Exception as e:
# qz,traceback.print_tb(e)
msg=traceback.format_exc()
logAndClose(msg)
exit()
print("success")
else:
msg=f"arg 1 do not exist or is not file:{f}."
log(msg)
exit(1)
print("-"*20,"begin os.walk to find the database root")
target_dirs=[]
for dirpath, dirnames, filenames in os.walk(unpack_dir):
for file_name in filenames:
if file_name.startswith("MANIFEST-"):
target_dirs.append(dirpath)
break
if len(target_dirs)==0:
logAndClose(f"no dir that include MANIFEST-xxx file ")
exit(1)
elif len(target_dirs)>1:
logAndClose(f"one more dirs that include MANIFEST-xxx file ")
exit(1)
leveldb_dir = target_dirs[0]
log(f"leveldb database dir:{leveldb_dir}")
output_dir = "extract_tampermonkey_script_output"
shutil.rmtree(output_dir,ignore_errors=True)
# os.mkdir(output_dir)
os.makedirs(output_dir,exist_ok=True)
pattern = re.compile("^@source(.*)$")
db = leveldb.LevelDB(leveldb_dir)
for k, v in db.RangeIter():
m = pattern.match(k.decode('utf-8'))
if m:
name = re.sub("[\W\b]", "_", m.groups()[0].strip())
# full_name = "%s.user.js" % name
full_name = os.path.join(output_dir,"%s.user.js" % name)
print("Writing to %s" % full_name)
content = json.JSONDecoder().decode(v.decode('utf-8'))['value']
with codecs.open(full_name, 'w', 'utf-8') as text_file:
text_file.write(content)
# pack
# "zip", "tar", "gztar","bztar", or "xztar".
# pack_dir='result'
# shutil.rmtree(pack_dir,ignore_errors=True)
# os.makedirs(pack_dir,exist_ok=True)
# shutil.make_archive("result/ret","zip",base_dir=output_dir)
# log(f"result is:result/ret.zip")
logAndClose("finished.")