-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_pyc_head.py
More file actions
44 lines (35 loc) · 1.12 KB
/
update_pyc_head.py
File metadata and controls
44 lines (35 loc) · 1.12 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
import binascii
class UpdatePycHead:
"""
update pyc head
"""
@staticmethod
def hexadecimal_read_file(filename):
"""convert the file to hexadecimal
"""
byte_list = []
with open(f'{filename}', 'rb') as f:
byte = f.read(1)
while byte:
byte_list.append('%02x' % (ord(byte)))
byte = f.read(1)
f.close()
return byte_list
def correct_the_data(self, filename):
"""get your papers right
"""
struct = self.hexadecimal_read_file('struct')
norm = self.hexadecimal_read_file(filename)
for i in range(16):
norm.insert(i, struct[i])
return norm
def restore_file(self, filename):
"""restore hexadecimal byte to file
"""
norm = self.correct_the_data(filename)
pyc_name = filename.split(".")[0]
with open(f'{pyc_name}.pyc', 'wb') as f:
for byte in norm:
ascii_byte = binascii.a2b_hex(byte)
f.write(ascii_byte)
f.close()