-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine.py
61 lines (46 loc) · 1.52 KB
/
combine.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
"""Functions related to combining the chunks."""
from rem import rem, rem_read_size
import os
base = []
def combine_chunks(is_verbose=False):
"""Combine the chunks according to their number."""
src, des = rem('grab')
number = check_and_pass() + 1
if is_verbose:
print("%d number of chunks in total..", number)
dest = des
des = os.path.dirname(des)
path_tmplt = os.path.join(des, 'cpf_temp', 'cpf_temp_{}')
i = 1
read_size = rem_read_size('grab')
while True:
i += 1
if i == number:
break
file = path_tmplt.format(i)
READ_stream = open(file, 'rb')
CONTENT = READ_stream.read(read_size) # 52428800) # 209715200)
append_file(base[0], CONTENT)
if is_verbose:
print("%d chunk added.", i)
READ_stream.close()
# Rename the base file to des
os.rename(path_tmplt.format(1), dest)
def append_file(src, des):
"""Append the des to src."""
# Open src in append mode
APPEND_STREAM = open(src, 'ab')
APPEND_STREAM.write(des)
APPEND_STREAM.close()
return True
def check_and_pass():
"""Check the cpf_temp folder and pass the files."""
src, des = rem('grab')
dest_folder = os.path.join(os.path.dirname(des), 'cpf_temp')
count = 0
for file in os.listdir(dest_folder):
count += 1
if file == 'cpf_temp_1':
# Take this as the base and append the other files to this one
base.append(os.path.join(dest_folder, file))
return count