-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.py
169 lines (134 loc) · 3.47 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-
# MultiChain Explorer 2 (c) Coin Sciences Ltd
# All rights reserved under BSD 3-clause license
import sys
import os
import datetime
import json
import cfg
import struct
import signal
import string
def file_read_char(f):
return ord(f.read(1))
def file_read_int32(f):
return struct.unpack('i', f.read(4))[0]
def str_to_int8(data):
return ord(data)
def bytes_to_int32(data):
return struct.unpack('i', data)[0]
def bytes_to_int64(data):
return struct.unpack('q', data)[0]
def bytes_to_hex(data):
toHex = lambda x:''.join(format(c, '02x') for c in x)
return toHex(data)
def print_error(msg):
sys.stderr.write(msg + "\n")
def full_dir_name(dir_name):
return str(dir_name).replace("~",os.path.expanduser("~")).rstrip("/") + "/";
def file_dir_name(file_name):
return os.path.dirname(os.path.realpath(str(file_name).replace("~",os.path.expanduser("~")))).rstrip("/") + "/";
def file_file_name(file_name):
return os.path.split(file_name)[1];
def log_write(data):
return file_write(cfg.log_file,str(datetime.datetime.now()) + " " + str(data) + "\n",True)
def log_error(data):
return log_write("ERROR: " + str(data))
def check_directory(dir_name):
try:
os.mkdir(dir_name)
except:
pass
if not os.path.exists(dir_name):
return False
return True
def directory_exists(dir_name):
if not os.path.exists(dir_name):
return False
return True
def remove_file(file_name):
try:
os.remove(file_name)
except:
pass
def file_write(filename,data,append=False):
mode="w"
if append:
mode="a"
f = open(filename, mode)
f.write(str(data))
f.close
return True
def file_read(filename):
result=None
if os.path.isfile(filename):
f = open(filename, "r")
result=f.read()
f.close
return result
def file_exists(filename):
return os.path.isfile(filename)
def is_process_running(process_id):
try:
os.kill(process_id, 0)
return True
except OSError:
return False
def kill_process(process_id):
try:
os.kill(process_id, signal.SIGKILL)
return True
except OSError:
return False
def get_pid():
return os.getpid()
def become_daemon():
try:
pid = os.fork()
except OSError as e:
print_error(e)
os._exit(1)
if pid == 0:
os.setsid()
try:
pid = os.fork()
except OSError as e:
print_error(e)
os._exit(1)
if (pid == 0):
os.chdir(file_dir_name(__file__))
os.umask(0)
else:
os._exit(0)
else:
os._exit(0)
def read_file_ptr(config):
current_ptr=file_read(config['ptr'])
ptr = (0, 0)
if current_ptr is not None:
ptr_dict=json.loads(current_ptr)
ptr=(ptr_dict["file"],ptr_dict["offset"])
return ptr
def write_file_ptr(config,ptr):
remove_file(config['ptr'])
ptr_dict={"file":ptr[0],"offset":ptr[1]}
return file_write(config['ptr'],json.dumps(ptr_dict))
def is_true(value):
val=str(value).lower()
if val == 'on':
return True
if val == 'yes':
return True
if val == 'true':
return True
return False
def is_printable(s):
"""
Return True if the string is ASCII (with punctuation) and printable
:param s:
:return:
"""
for c in s:
if c not in string.printable:
return False
return True