-
Notifications
You must be signed in to change notification settings - Fork 0
/
browsezip.py
executable file
·177 lines (136 loc) · 5.08 KB
/
browsezip.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
169
170
171
172
173
174
175
176
#!/usr/bin/env python
import zipfile
import os
import time
import datetime
def checkForZipsInPath(full_path= None):
# remove the slash if the path is given with it
list_of_dirs = full_path.rstrip('/').split('/')
path_to_zip = ''
for i in list_of_dirs:
path_to_zip += i
#print path_to_zip
if os.path.isfile( path_to_zip +'.zip'):
break
else:
path_to_zip += '/'
if path_to_zip[-1:] is '/':
path_to_zip = None
return os.path.join(path_to_zip)
def browseFileInZip(zipObj=None, path_in_zip=None):
path_in_zip = path_in_zip.rstrip('/')
result = None
if isfileinzip(zipObj, path_in_zip):
result = zipObj.read(path_in_zip)
if isdirinzip(zipObj, path_in_zip):
files_in_dir = [i for i in zipObj.namelist() if path_in_zip+'/' in i and path_in_zip+'/' != i]
if len(files_in_dir) == 0:
#it's empty folder
result = '--empty folder--'
else:
#print '---------fies in folder----------'
items_to_show = []
result = []
for i in files_in_dir:
an_item = i.split(path_in_zip+'/', 1)[1].split('/', 1)[0]
if an_item not in items_to_show:
items_to_show.append(an_item)
if isdirinzip(zipObj, path_in_zip+'/'+an_item):
an_item += '/'
info = zipObj.getinfo(path_in_zip+'/'+an_item)
item_size = info.file_size
item_mtime = time.strftime('%Y-%m-%d %H:%M:%S', (info.date_time + (0, 0, 0)))
item_description = info.comment
result.append({'name':an_item,'link':path_in_zip+'/'+an_item,'size':str(item_size),
'modified':item_mtime, 'description':item_description})
return result
def formatResult(result = None):
if not result:
# not existing
print 'file not found'
elif not isinstance(result, list):
#empty dir or file
print result
else:
# non empty folder
#print result
markup="""
<table>
<tr>
<th>Filename</th>
<th>Size</th>
<th>Date</th>
<th>Comment<th>
</tr>"""
for i in result:
markup += """
<tr>
<td><a href="%s">%s</a></td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>""" % (i['link'],i['name'],i['size'],i['modified'],i['description'])
markup += '</table>'
print markup
def isfileinzip(zipObj=None, path_in_zip=None):
path_in_zip = path_in_zip.rstrip('/')
list_of_items = zipObj.namelist()
isfile = False
#print path_in_zip
if path_in_zip in list_of_items:
isfile = True
return isfile
def isdirinzip(zipObj=None, path_in_zip=None):
path_in_zip = path_in_zip.rstrip('/')
list_of_items = zipObj.namelist()
isdir = False
#print path_in_zip
if path_in_zip+'/' in list_of_items:
isdir = True
return isdir
def browsePath(path=None):
result = None
if os.path.isfile(path):
with open(path) as file_to_read:
result = file_to_read.read()
if os.stat(path).st_size == 0:
result = ' '
elif os.path.isdir(path):
if not os.listdir(path):
result = '--empty folder--'
else:
result = []
for i in os.listdir(path):
file_stat = os.stat(os.path.join(path, i))
item_size = file_stat.st_size
item_mtime = datetime.datetime.fromtimestamp(file_stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S')
item_description = ''
result.append({'name':i,'link': os.path.join(path,i),'size':str(item_size),
'modified':item_mtime, 'description':item_description})
return result
if __name__ == "__main__":
print "Content-Type: text/html\n"
if 'REQUEST_URI' in os.environ:
uri = os.environ['REQUEST_URI']
print uri
requested_path = 'blabla'
requested_path = 'zipfold/delme'
#'134.806_RunMuonEG2015C+RunMuonEG2015C+HLTDR2_25ns+RECODR2_25nsreHLT_HIPM+HARVESTDR2/'
#'step1_RunMuonEG2015C+RunMuonEG2015C+HLTDR2_25ns+RECODR2_25nsreHLT_HIPM+HARVESTDR2.log'
result = None
if os.path.exists(requested_path):
result = browsePath(requested_path)
else:
zip_found_in_path = checkForZipsInPath(requested_path)
if zip_found_in_path:
zip_name = zip_found_in_path+'.zip'
path_in_zip = requested_path.split(zip_found_in_path)[1][1:]
#print zip_found_in_path, zip_name, path_in_zip
zipObj = zipfile.ZipFile(zip_name,'r')
# add the full path to the link
result = browseFileInZip(zipObj, path_in_zip)
if isinstance(result, list):
for i in result:
i['link'] = zip_found_in_path+'/'+i['link']
zipObj.close()
formatResult(result)