forked from wushilian/tr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
61 lines (41 loc) · 1.25 KB
/
tools.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
import os, sys
import glob
BIG_FILES = ["./libtorch/lib/libtorch.so"]
_BASEDIR = os.path.dirname(os.path.abspath(__file__))
BIG_FILES = [os.path.join(_BASEDIR, _) for _ in BIG_FILES]
FILE_SIZE = 32 * 1024 * 1024
PART_SEP = ".part."
def split(file_path):
if not os.path.exists(file_path):
return
with open(file_path, "rb") as f:
file_data = f.read()
if len(file_data) <= FILE_SIZE:
return
pos = 0
num = 0
while pos < len(file_data):
with open(file_path + PART_SEP + str(num), "wb") as f:
f.write(file_data[pos:pos + FILE_SIZE])
pos += FILE_SIZE
num += 1
os.remove(file_path)
def join(file_path):
if os.path.exists(file_path):
return
file_parts = glob.glob(file_path + PART_SEP + "*")
if len(file_parts) < 1:
return
file_data = bytes()
for num in range(len(file_parts)):
with open(file_path + PART_SEP + str(num), "rb") as f:
file_data += f.read()
with open(file_path, "wb") as f:
f.write(file_data)
for file_part in file_parts:
os.remove(file_part)
if __name__ == '__main__':
for big_file in BIG_FILES:
split(big_file)
if len(sys.argv) > 1:
join(big_file)