forked from Skeletrox/usb-backend-pinut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
73 lines (50 loc) · 1.81 KB
/
extract.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
import zipfile,os,shutil
def extractit(path_of_file):
#ekstep file uploaded path obtained using file.path
file_path=path_of_file
zip_ref=zipfile.ZipFile(file_path,'r')
index=file_path.find(".ecar")
#folder name
folder=file_path[:index]
#create a folder for the ekstep file uploaded uisng its own name
os.makedirs(folder)
#ekstep file uploaded folder which contains the unzip version of the ekstep file uploaded
zip_ref.extractall(folder)
zip_ref.close()
#move the ecar file to ekstep file folder starting with the name do_
for filename in os.listdir(folder):
if(not filename.endswith(".json")):
break
index=file_path.rfind('/')
file_name=file_path[index+1:]
shutil.copy2(file_path,folder+"/"+filename+"/"+file_name)
#change the name of manifest file to folder name
change_name=folder[folder.rfind('/')+1:]
#print change_name
#renames the manifest file inside the ekstep file uploaded folder
os.rename(folder+"/manifest.json",folder+"/"+change_name+".json")
#list for storing the extracted items names
content_list=[]
#collects the folders and files extracted inside the content_list
for filename in os.listdir(folder):
content_list.append(filename)
#print content_list
index=file_path.find("/ecar_files")
content_path=file_path[:index]
content_path+="/content"
#files list
#move the contents of the ekstep file uploaded folder
for filename in os.listdir(folder):
print filename
if(filename.endswith(".json")):
shutil.move(folder+"/"+filename,content_path+"/"+"json_files")
else:
try:
shutil.move(folder+"/"+filename,content_path)
except:
print "This file already exists"
break
#remove's the ekstep file uploaded folder which is empty right now
shutil.rmtree(folder)
#name of the folder which we got after extracting .ecar file
return content_list