-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgurulhutils.py
executable file
·107 lines (89 loc) · 3.01 KB
/
gurulhutils.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
import asyncio
import json
import os
import hashlib
import importlib
from pymongo import MongoClient
import time
def sleep( secs ):
s = int( secs ) * 0.001
time.sleep( s )
debug = True
def status_check( status, pipe=None, exit_flag=True ):
if( status != "ok" ):
if pipe:
pipe.send("fail")
pipe.close()
else:
print("FAILED.", flush=True)
print("Exiting . . .", flush=True)
if( exit_flag ): exit()
else:
if pipe:
pipe.send( "ok" )
else:
print("OK.", flush=True)
def db_init( token ):
try:
mongo_client = MongoClient( host=token[0], port=int( token[1] ))
database = mongo_client[ token[2] ]
database.authenticate( token[3], token[4] )
return "ok", database
except Exception as e:
if( debug ): print( "Error in gurulhutils.py, db_init: ", e, flush=True )
return "fail", None
def hashfile( f, hash, bs=65536 ):
f = open( f, "rb" )
buf = f.read(bs)
while( len( buf ) > 0 ):
hash.update(buf)
buf = f.read(bs)
return hash.digest()
def import_list( database, db_name ):
db = database.get_collection( db_name )
import_list = []
cursor = db.find()
for i in range( 0, cursor.count() ):
if( os.path.isfile( cursor[i]["path"]) ):
if( cursor[i]["hash"] == str( hashfile( cursor[i]["path"], hashlib.sha256() ) ) ):
if( debug ): print( cursor[i]["name"], flush=True )
try:
import_list.append( cursor[i] )
import_list[-1].update( { "module" : importlib.import_module( cursor[i]["import"] ) } )
except Exception as e:
if( debug ): print( "Could not import: ", cursor[i]["name"], "\nReason: ", e, flush=True )
else:
if( debug ): print( "File is corrupted: ", cursor[i]["name"], flush=True)
else:
if( debug ): print( "Module not found: ", cursor[i]["name"], flush=True )
return import_list
def wrap_message( message ):
return json.dumps( message ).encode("utf-8")
def unwrap_message( message ):
return json.loads( message.decode("utf-8") )
def format_length( message ):
reply_len = str( len( message ) )
while len( reply_len ) < 6:
reply_len = '0' + reply_len
return reply_len
def socket_send( socket, message ):
message_wrap = wrap_message( message )
message_len = format_length( message_wrap )
message_len = wrap_message( message_len )
if( debug ): print( message_len, message_wrap)
socket.send( message_len )
socket.send( message_wrap )
def socket_recv( socket ):
data_len = socket.recv( 8 )
data_len = unwrap_message( data_len )
data_len = int( data_len )
while 1:
try:
data = socket.recv( data_len )
break
except Exception as e:
print(e)
print(data)
data = unwrap_message( data )
if( debug ): print( data_len, data )
return data