75
81
print Util().version()
78
85
def ensure_bucket(connection, bucket):
79
86
bucket_instance = None
81
bucket_instance = connection.get_bucket(bucket)
88
bucket_instance = connection.get_bucket(bucket)
82
89
except S3ResponseError, s3error:
83
print 'Unable to get bucket %s' % (bucket)
90
print 'Unable to get bucket %s' % bucket
85
92
return bucket_instance
87
95
def get_parts(manifest_filename):
89
dom = minidom.parse(manifest_filename)
97
dom = minidom.parse(manifest_filename)
90
98
manifest_elem = dom.getElementsByTagName('manifest')[0]
91
99
parts_list = manifest_elem.getElementsByTagName('filename')
92
100
for part_elem in parts_list:
93
nodes = part_elem.childNodes
101
nodes = part_elem.childNodes
95
103
if node.nodeType == node.TEXT_NODE:
96
104
parts.append(node.data)
99
108
def get_relative_filename(filename):
100
109
f_parts = filename.split('/')
101
110
return f_parts[len(f_parts) - 1]
103
113
def get_manifests(bucket):
105
115
keys = bucket.get_all_keys()
108
if(k.name.find('manifest') >= 0):
109
manifests.append(k.name)
118
if k.name.find('manifest') >= 0:
119
manifests.append(k.name)
112
123
def download_manifests(bucket, manifests, directory):
113
if (len(manifests) > 0):
114
if not os.path.exists(directory):
115
os.makedirs(directory)
124
if len(manifests) > 0:
125
if not os.path.exists(directory):
126
os.makedirs(directory)
116
127
for manifest in manifests:
119
manifest_filename = os.path.join(directory, manifest)
120
manifest_file = open(manifest_filename, "wb")
122
k.get_contents_to_file(manifest_file)
130
manifest_filename = os.path.join(directory, manifest)
131
manifest_file = open(manifest_filename, 'wb')
133
k.get_contents_to_file(manifest_file)
123
134
except S3ResponseError, s3error:
124
s3error_string = '%s' % (s3error)
125
if (s3error_string.find('200') < 0):
127
print 'unable to download manifest %s' % (manifest)
128
if os.path.exists(manifest_filename):
129
os.remove(manifest_filename)
131
manifest_file.close()
135
s3error_string = '%s' % s3error
136
if s3error_string.find('200') < 0:
138
print 'unable to download manifest %s' % manifest
139
if os.path.exists(manifest_filename):
140
os.remove(manifest_filename)
142
manifest_file.close()
134
146
def delete_parts(bucket, manifests, directory=None):
135
147
for manifest in manifests:
136
manifest_filename = os.path.join(directory, manifest)
137
parts = get_parts(manifest_filename)
143
except S3ResponseError, s3error:
144
s3error_string = '%s' % (s3error)
145
if (s3error_string.find('200') < 0):
147
print 'unable to delete part %s' % (part)
151
def delete_manifests(bucket, manifests, clear, euca, bucket_name):
148
manifest_filename = os.path.join(directory, manifest)
149
parts = get_parts(manifest_filename)
155
except S3ResponseError, s3error:
156
s3error_string = '%s' % s3error
157
if s3error_string.find('200') < 0:
159
print 'unable to delete part %s' % part
163
def delete_manifests(
152
170
for manifest in manifests:
157
except Exception, s3error:
158
s3error_string = '%s' % (s3error)
159
if (s3error_string.find('200') < 0):
161
print 'unable to delete manifest %s' % (manifest)
163
bucket = ensure_bucket(euca.make_connection(), bucket_name)
164
except ConnectionFailed, e:
175
except Exception, s3error:
176
s3error_string = '%s' % s3error
177
if s3error_string.find('200') < 0:
179
print 'unable to delete manifest %s' % manifest
181
bucket = ensure_bucket(euca.make_connection(),
183
except ConnectionFailed, e:
170
except Exception, s3error:
171
s3error_string = '%s' % (s3error)
172
if (s3error_string.find('200') < 0):
174
print 'unable to delete bucket %s' % (bucket.name)
189
except Exception, s3error:
190
s3error_string = '%s' % s3error
191
if s3error_string.find('200') < 0:
193
print 'unable to delete bucket %s' % bucket.name
176
196
def remove_manifests(manifests, directory):
177
197
for manifest in manifests:
178
manifest_filename = os.path.join(directory, manifest)
179
if os.path.exists(manifest_filename):
180
os.remove(manifest_filename)
198
manifest_filename = os.path.join(directory, manifest)
199
if os.path.exists(manifest_filename):
200
os.remove(manifest_filename)
185
euca = Euca2ool('b:m:p:',
186
['bucket=', 'manifest=', 'prefix=', 'clear'],
206
euca = Euca2ool('b:m:p:', ['bucket=', 'manifest=', 'prefix=',
207
'clear'], is_s3=True)
188
208
except Exception, e:
193
213
manifest_path = None
198
for name, value in euca.opts:
218
for (name, value) in euca.opts:
199
219
if name in ('-h', '--help'):
201
221
elif name in ('-b', '--bucket'):
203
223
elif name in ('-m', '--manifest'):
204
224
manifest_path = value
205
elif name == '--clear':
225
elif name == '--clear':
207
227
elif name in ('-p', '--prefix'):
213
euca.validate_file(manifest_path)
214
except FileValidationError:
215
print 'Invalid manifest'
218
conn = euca.make_connection()
219
except ConnectionFailed, e:
233
euca.validate_file(manifest_path)
234
except FileValidationError:
235
print 'Invalid manifest'
238
conn = euca.make_connection()
239
except ConnectionFailed, e:
222
242
bucket_instance = ensure_bucket(conn, bucket)
224
delete_local_manifests = True
225
if not manifest_path:
227
manifests = get_manifests(bucket_instance)
229
manifests = ["%s.manifest.xml" % prefix]
231
manifests = ["%s" % euca.get_relative_filename(manifest_path)]
232
directory = "%s" % euca.get_file_path(manifest_path)
233
delete_local_manifests = False
234
return_code = download_manifests(bucket_instance, manifests, directory)
236
delete_parts(bucket_instance, manifests, directory)
237
delete_manifests(bucket_instance, manifests, clear, euca, bucket)
238
if delete_local_manifests:
239
remove_manifests(manifests, directory)
244
delete_local_manifests = True
245
if not manifest_path:
247
manifests = get_manifests(bucket_instance)
249
manifests = ['%s.manifest.xml' % prefix]
252
% euca.get_relative_filename(manifest_path)]
253
directory = '%s' % euca.get_file_path(manifest_path)
254
delete_local_manifests = False
255
return_code = download_manifests(bucket_instance, manifests,
258
delete_parts(bucket_instance, manifests, directory)
259
delete_manifests(bucket_instance, manifests, clear, euca,
261
if delete_local_manifests:
262
remove_manifests(manifests, directory)
242
print 'bucket must be specified.'
265
print 'bucket must be specified.'
245
if __name__ == "__main__":
269
if __name__ == '__main__':