forked from alessandrodd/apk_api_key_extractor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsonlines_dump.py
74 lines (65 loc) · 3.03 KB
/
jsonlines_dump.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
import datetime
import json
import logging
import os
from flufl.lock import Lock, AlreadyLockedError, TimeOutError
from abstract_dump import AbstractDump
import config
LOCK_PREFIX = ".lock"
class JsonlinesDump(AbstractDump):
def __init__(self):
self.dest = os.path.abspath(config.jsonlines["dump_file"])
self.strings_dest = os.path.abspath(config.jsonlines["strings_file"])
def dump_apikeys(self, entries, package, version_code, version_name):
lock_acquired = False
while not lock_acquired:
try:
filename = self.dest + LOCK_PREFIX
lock = Lock(filename, lifetime=datetime.timedelta(seconds=6000)) # expires in 10 minutes
if not lock.is_locked:
lock.lock(timeout=datetime.timedelta(milliseconds=350))
lock_acquired = True
with open(self.dest, 'a') as f:
first = True
if os.path.exists(self.dest) and os.path.getsize(self.dest) > 0:
first = False
for entry in entries:
entry_dict = entry.__dict__
entry_dict['package'] = package
entry_dict['versionCode'] = version_code
entry_dict['versionName'] = version_name
if first:
first = False
else:
f.write("\n")
json.dump(entry_dict, f)
if lock.is_locked:
lock.unlock()
except (AlreadyLockedError, TimeOutError):
# some other process is analyzing the file; go ahead and look for another file
pass
def dump_strings(self, entries):
lock_acquired = False
while not lock_acquired:
try:
filename = self.strings_dest + LOCK_PREFIX
lock = Lock(filename, lifetime=datetime.timedelta(seconds=6000)) # expires in 10 minutes
if not lock.is_locked:
lock.lock(timeout=datetime.timedelta(milliseconds=350))
lock_acquired = True
with open(self.strings_dest, 'a') as f:
first = True
if os.path.exists(self.strings_dest) and os.path.getsize(self.strings_dest) > 0:
first = False
for entry in entries:
entry_dict = entry.__dict__
if first:
first = False
else:
f.write("\n")
json.dump(entry_dict, f)
if lock.is_locked:
lock.unlock()
except (AlreadyLockedError, TimeOutError):
# some other process is analyzing the file; go ahead and look for another file
pass