s3_cmd_bin 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +28 -0
  5. data/Rakefile +1 -0
  6. data/lib/s3_cmd_bin/version.rb +3 -0
  7. data/lib/s3_cmd_bin.rb +15 -0
  8. data/resources/ChangeLog +1462 -0
  9. data/resources/INSTALL +97 -0
  10. data/resources/LICENSE +339 -0
  11. data/resources/MANIFEST.in +2 -0
  12. data/resources/Makefile +4 -0
  13. data/resources/NEWS +234 -0
  14. data/resources/README +342 -0
  15. data/resources/S3/ACL.py +224 -0
  16. data/resources/S3/ACL.pyc +0 -0
  17. data/resources/S3/AccessLog.py +92 -0
  18. data/resources/S3/AccessLog.pyc +0 -0
  19. data/resources/S3/BidirMap.py +42 -0
  20. data/resources/S3/BidirMap.pyc +0 -0
  21. data/resources/S3/CloudFront.py +773 -0
  22. data/resources/S3/CloudFront.pyc +0 -0
  23. data/resources/S3/Config.py +294 -0
  24. data/resources/S3/Config.pyc +0 -0
  25. data/resources/S3/ConnMan.py +71 -0
  26. data/resources/S3/ConnMan.pyc +0 -0
  27. data/resources/S3/Exceptions.py +88 -0
  28. data/resources/S3/Exceptions.pyc +0 -0
  29. data/resources/S3/FileDict.py +53 -0
  30. data/resources/S3/FileDict.pyc +0 -0
  31. data/resources/S3/FileLists.py +517 -0
  32. data/resources/S3/FileLists.pyc +0 -0
  33. data/resources/S3/HashCache.py +53 -0
  34. data/resources/S3/HashCache.pyc +0 -0
  35. data/resources/S3/MultiPart.py +137 -0
  36. data/resources/S3/MultiPart.pyc +0 -0
  37. data/resources/S3/PkgInfo.py +14 -0
  38. data/resources/S3/PkgInfo.pyc +0 -0
  39. data/resources/S3/Progress.py +173 -0
  40. data/resources/S3/Progress.pyc +0 -0
  41. data/resources/S3/S3.py +979 -0
  42. data/resources/S3/S3.pyc +0 -0
  43. data/resources/S3/S3Uri.py +223 -0
  44. data/resources/S3/S3Uri.pyc +0 -0
  45. data/resources/S3/SimpleDB.py +178 -0
  46. data/resources/S3/SortedDict.py +66 -0
  47. data/resources/S3/SortedDict.pyc +0 -0
  48. data/resources/S3/Utils.py +462 -0
  49. data/resources/S3/Utils.pyc +0 -0
  50. data/resources/S3/__init__.py +0 -0
  51. data/resources/S3/__init__.pyc +0 -0
  52. data/resources/TODO +52 -0
  53. data/resources/artwork/AtomicClockRadio.ttf +0 -0
  54. data/resources/artwork/TypeRa.ttf +0 -0
  55. data/resources/artwork/site-top-full-size.xcf +0 -0
  56. data/resources/artwork/site-top-label-download.png +0 -0
  57. data/resources/artwork/site-top-label-s3cmd.png +0 -0
  58. data/resources/artwork/site-top-label-s3sync.png +0 -0
  59. data/resources/artwork/site-top-s3tools-logo.png +0 -0
  60. data/resources/artwork/site-top.jpg +0 -0
  61. data/resources/artwork/site-top.png +0 -0
  62. data/resources/artwork/site-top.xcf +0 -0
  63. data/resources/format-manpage.pl +196 -0
  64. data/resources/magic +63 -0
  65. data/resources/run-tests.py +537 -0
  66. data/resources/s3cmd +2116 -0
  67. data/resources/s3cmd.1 +435 -0
  68. data/resources/s3db +55 -0
  69. data/resources/setup.cfg +2 -0
  70. data/resources/setup.py +80 -0
  71. data/resources/testsuite.tar.gz +0 -0
  72. data/resources/upload-to-sf.sh +7 -0
  73. data/s3_cmd_bin.gemspec +23 -0
  74. metadata +152 -0
data/resources/s3cmd ADDED
@@ -0,0 +1,2116 @@
1
+ #!/usr/bin/python
2
+
3
+ ## Amazon S3 manager
4
+ ## Author: Michal Ludvig <michal@logix.cz>
5
+ ## http://www.logix.cz/michal
6
+ ## License: GPL Version 2
7
+
8
+ import sys
9
+
10
+ if float("%d.%d" %(sys.version_info[0], sys.version_info[1])) < 2.4:
11
+ sys.stderr.write("ERROR: Python 2.4 or higher required, sorry.\n")
12
+ sys.exit(1)
13
+
14
+ import logging
15
+ import time
16
+ import os
17
+ import re
18
+ import errno
19
+ import glob
20
+ import traceback
21
+ import codecs
22
+ import locale
23
+ import subprocess
24
+ import htmlentitydefs
25
+ import socket
26
+ import shutil
27
+ import tempfile
28
+ import S3.Exceptions
29
+
30
+ from copy import copy
31
+ from optparse import OptionParser, Option, OptionValueError, IndentedHelpFormatter
32
+ from logging import debug, info, warning, error
33
+ from distutils.spawn import find_executable
34
+
35
+ def output(message):
36
+ sys.stdout.write(message + "\n")
37
+ sys.stdout.flush()
38
+
39
+ def check_args_type(args, type, verbose_type):
40
+ for arg in args:
41
+ if S3Uri(arg).type != type:
42
+ raise ParameterError("Expecting %s instead of '%s'" % (verbose_type, arg))
43
+
44
+ def cmd_du(args):
45
+ s3 = S3(Config())
46
+ if len(args) > 0:
47
+ uri = S3Uri(args[0])
48
+ if uri.type == "s3" and uri.has_bucket():
49
+ subcmd_bucket_usage(s3, uri)
50
+ return
51
+ subcmd_bucket_usage_all(s3)
52
+
53
+ def subcmd_bucket_usage_all(s3):
54
+ response = s3.list_all_buckets()
55
+
56
+ buckets_size = 0
57
+ for bucket in response["list"]:
58
+ size = subcmd_bucket_usage(s3, S3Uri("s3://" + bucket["Name"]))
59
+ if size != None:
60
+ buckets_size += size
61
+ total_size, size_coeff = formatSize(buckets_size, Config().human_readable_sizes)
62
+ total_size_str = str(total_size) + size_coeff
63
+ output(u"".rjust(8, "-"))
64
+ output(u"%s Total" % (total_size_str.ljust(8)))
65
+
66
+ def subcmd_bucket_usage(s3, uri):
67
+ bucket = uri.bucket()
68
+ object = uri.object()
69
+
70
+ if object.endswith('*'):
71
+ object = object[:-1]
72
+
73
+ bucket_size = 0
74
+ # iterate and store directories to traverse, while summing objects:
75
+ dirs = [object]
76
+ while dirs:
77
+ try:
78
+ response = s3.bucket_list(bucket, prefix=dirs.pop())
79
+ except S3Error, e:
80
+ if S3.codes.has_key(e.info["Code"]):
81
+ error(S3.codes[e.info["Code"]] % bucket)
82
+ return
83
+ else:
84
+ raise
85
+
86
+ # objects in the current scope:
87
+ for obj in response["list"]:
88
+ bucket_size += int(obj["Size"])
89
+
90
+ # directories found in current scope:
91
+ for obj in response["common_prefixes"]:
92
+ dirs.append(obj["Prefix"])
93
+
94
+ total_size, size_coeff = formatSize(bucket_size, Config().human_readable_sizes)
95
+ total_size_str = str(total_size) + size_coeff
96
+ output(u"%s %s" % (total_size_str.ljust(8), uri))
97
+ return bucket_size
98
+
99
+ def cmd_ls(args):
100
+ s3 = S3(Config())
101
+ if len(args) > 0:
102
+ uri = S3Uri(args[0])
103
+ if uri.type == "s3" and uri.has_bucket():
104
+ subcmd_bucket_list(s3, uri)
105
+ return
106
+ subcmd_buckets_list_all(s3)
107
+
108
+ def cmd_buckets_list_all_all(args):
109
+ s3 = S3(Config())
110
+
111
+ response = s3.list_all_buckets()
112
+
113
+ for bucket in response["list"]:
114
+ subcmd_bucket_list(s3, S3Uri("s3://" + bucket["Name"]))
115
+ output(u"")
116
+
117
+
118
+ def subcmd_buckets_list_all(s3):
119
+ response = s3.list_all_buckets()
120
+ for bucket in response["list"]:
121
+ output(u"%s s3://%s" % (
122
+ formatDateTime(bucket["CreationDate"]),
123
+ bucket["Name"],
124
+ ))
125
+
126
+ def subcmd_bucket_list(s3, uri):
127
+ bucket = uri.bucket()
128
+ prefix = uri.object()
129
+
130
+ debug(u"Bucket 's3://%s':" % bucket)
131
+ if prefix.endswith('*'):
132
+ prefix = prefix[:-1]
133
+ try:
134
+ response = s3.bucket_list(bucket, prefix = prefix)
135
+ except S3Error, e:
136
+ if S3.codes.has_key(e.info["Code"]):
137
+ error(S3.codes[e.info["Code"]] % bucket)
138
+ return
139
+ else:
140
+ raise
141
+
142
+ if cfg.list_md5:
143
+ format_string = u"%(timestamp)16s %(size)9s%(coeff)1s %(md5)32s %(uri)s"
144
+ else:
145
+ format_string = u"%(timestamp)16s %(size)9s%(coeff)1s %(uri)s"
146
+
147
+ for prefix in response['common_prefixes']:
148
+ output(format_string % {
149
+ "timestamp": "",
150
+ "size": "DIR",
151
+ "coeff": "",
152
+ "md5": "",
153
+ "uri": uri.compose_uri(bucket, prefix["Prefix"])})
154
+
155
+ for object in response["list"]:
156
+ size, size_coeff = formatSize(object["Size"], Config().human_readable_sizes)
157
+ output(format_string % {
158
+ "timestamp": formatDateTime(object["LastModified"]),
159
+ "size" : str(size),
160
+ "coeff": size_coeff,
161
+ "md5" : object['ETag'].strip('"'),
162
+ "uri": uri.compose_uri(bucket, object["Key"]),
163
+ })
164
+
165
+ def cmd_bucket_create(args):
166
+ s3 = S3(Config())
167
+ for arg in args:
168
+ uri = S3Uri(arg)
169
+ if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
170
+ raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
171
+ try:
172
+ response = s3.bucket_create(uri.bucket(), cfg.bucket_location)
173
+ output(u"Bucket '%s' created" % uri.uri())
174
+ except S3Error, e:
175
+ if S3.codes.has_key(e.info["Code"]):
176
+ error(S3.codes[e.info["Code"]] % uri.bucket())
177
+ return
178
+ else:
179
+ raise
180
+
181
+ def cmd_website_info(args):
182
+ s3 = S3(Config())
183
+ for arg in args:
184
+ uri = S3Uri(arg)
185
+ if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
186
+ raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
187
+ try:
188
+ response = s3.website_info(uri, cfg.bucket_location)
189
+ if response:
190
+ output(u"Bucket %s: Website configuration" % uri.uri())
191
+ output(u"Website endpoint: %s" % response['website_endpoint'])
192
+ output(u"Index document: %s" % response['index_document'])
193
+ output(u"Error document: %s" % response['error_document'])
194
+ else:
195
+ output(u"Bucket %s: Unable to receive website configuration." % (uri.uri()))
196
+ except S3Error, e:
197
+ if S3.codes.has_key(e.info["Code"]):
198
+ error(S3.codes[e.info["Code"]] % uri.bucket())
199
+ return
200
+ else:
201
+ raise
202
+
203
+ def cmd_website_create(args):
204
+ s3 = S3(Config())
205
+ for arg in args:
206
+ uri = S3Uri(arg)
207
+ if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
208
+ raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
209
+ try:
210
+ response = s3.website_create(uri, cfg.bucket_location)
211
+ output(u"Bucket '%s': website configuration created." % (uri.uri()))
212
+ except S3Error, e:
213
+ if S3.codes.has_key(e.info["Code"]):
214
+ error(S3.codes[e.info["Code"]] % uri.bucket())
215
+ return
216
+ else:
217
+ raise
218
+
219
+ def cmd_website_delete(args):
220
+ s3 = S3(Config())
221
+ for arg in args:
222
+ uri = S3Uri(arg)
223
+ if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
224
+ raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
225
+ try:
226
+ response = s3.website_delete(uri, cfg.bucket_location)
227
+ output(u"Bucket '%s': website configuration deleted." % (uri.uri()))
228
+ except S3Error, e:
229
+ if S3.codes.has_key(e.info["Code"]):
230
+ error(S3.codes[e.info["Code"]] % uri.bucket())
231
+ return
232
+ else:
233
+ raise
234
+
235
+ def cmd_bucket_delete(args):
236
+ def _bucket_delete_one(uri):
237
+ try:
238
+ response = s3.bucket_delete(uri.bucket())
239
+ except S3Error, e:
240
+ if e.info['Code'] == 'BucketNotEmpty' and (cfg.force or cfg.recursive):
241
+ warning(u"Bucket is not empty. Removing all the objects from it first. This may take some time...")
242
+ subcmd_object_del_uri(uri.uri(), recursive = True)
243
+ return _bucket_delete_one(uri)
244
+ elif S3.codes.has_key(e.info["Code"]):
245
+ error(S3.codes[e.info["Code"]] % uri.bucket())
246
+ return
247
+ else:
248
+ raise
249
+
250
+ s3 = S3(Config())
251
+ for arg in args:
252
+ uri = S3Uri(arg)
253
+ if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
254
+ raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
255
+ _bucket_delete_one(uri)
256
+ output(u"Bucket '%s' removed" % uri.uri())
257
+
258
+ def cmd_object_put(args):
259
+ cfg = Config()
260
+ s3 = S3(cfg)
261
+
262
+ if len(args) == 0:
263
+ raise ParameterError("Nothing to upload. Expecting a local file or directory and a S3 URI destination.")
264
+
265
+ ## Normalize URI to convert s3://bkt to s3://bkt/ (trailing slash)
266
+ destination_base_uri = S3Uri(args.pop())
267
+ if destination_base_uri.type != 's3':
268
+ raise ParameterError("Destination must be S3Uri. Got: %s" % destination_base_uri)
269
+ destination_base = str(destination_base_uri)
270
+
271
+ if len(args) == 0:
272
+ raise ParameterError("Nothing to upload. Expecting a local file or directory.")
273
+
274
+ local_list, single_file_local = fetch_local_list(args)
275
+
276
+ local_list, exclude_list = filter_exclude_include(local_list)
277
+
278
+ local_count = len(local_list)
279
+
280
+ info(u"Summary: %d local files to upload" % local_count)
281
+
282
+ if local_count > 0:
283
+ if not single_file_local and '-' in local_list.keys():
284
+ raise ParameterError("Cannot specify multiple local files if uploading from '-' (ie stdin)")
285
+ elif single_file_local and local_list.keys()[0] == "-" and destination_base.endswith("/"):
286
+ raise ParameterError("Destination S3 URI must not end with '/' when uploading from stdin.")
287
+ elif not destination_base.endswith("/"):
288
+ if not single_file_local:
289
+ raise ParameterError("Destination S3 URI must end with '/' (ie must refer to a directory on the remote side).")
290
+ local_list[local_list.keys()[0]]['remote_uri'] = unicodise(destination_base)
291
+ else:
292
+ for key in local_list:
293
+ local_list[key]['remote_uri'] = unicodise(destination_base + key)
294
+
295
+ if cfg.dry_run:
296
+ for key in exclude_list:
297
+ output(u"exclude: %s" % unicodise(key))
298
+ for key in local_list:
299
+ if key != "-":
300
+ nicekey = local_list[key]['full_name_unicode']
301
+ else:
302
+ nicekey = "<stdin>"
303
+ output(u"upload: %s -> %s" % (nicekey, local_list[key]['remote_uri']))
304
+
305
+ warning(u"Exiting now because of --dry-run")
306
+ return
307
+
308
+ seq = 0
309
+ for key in local_list:
310
+ seq += 1
311
+
312
+ uri_final = S3Uri(local_list[key]['remote_uri'])
313
+
314
+ extra_headers = copy(cfg.extra_headers)
315
+ full_name_orig = local_list[key]['full_name']
316
+ full_name = full_name_orig
317
+ seq_label = "[%d of %d]" % (seq, local_count)
318
+ if Config().encrypt:
319
+ exitcode, full_name, extra_headers["x-amz-meta-s3tools-gpgenc"] = gpg_encrypt(full_name_orig)
320
+ try:
321
+ response = s3.object_put(full_name, uri_final, extra_headers, extra_label = seq_label)
322
+ except S3UploadError, e:
323
+ error(u"Upload of '%s' failed too many times. Skipping that file." % full_name_orig)
324
+ continue
325
+ except InvalidFileError, e:
326
+ warning(u"File can not be uploaded: %s" % e)
327
+ continue
328
+ speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
329
+ if not Config().progress_meter:
330
+ output(u"File '%s' stored as '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s) %s" %
331
+ (unicodise(full_name_orig), uri_final, response["size"], response["elapsed"],
332
+ speed_fmt[0], speed_fmt[1], seq_label))
333
+ if Config().acl_public:
334
+ output(u"Public URL of the object is: %s" %
335
+ (uri_final.public_url()))
336
+ if Config().encrypt and full_name != full_name_orig:
337
+ debug(u"Removing temporary encrypted file: %s" % unicodise(full_name))
338
+ os.remove(full_name)
339
+
340
+ def cmd_object_get(args):
341
+ cfg = Config()
342
+ s3 = S3(cfg)
343
+
344
+ ## Check arguments:
345
+ ## if not --recursive:
346
+ ## - first N arguments must be S3Uri
347
+ ## - if the last one is S3 make current dir the destination_base
348
+ ## - if the last one is a directory:
349
+ ## - take all 'basenames' of the remote objects and
350
+ ## make the destination name be 'destination_base'+'basename'
351
+ ## - if the last one is a file or not existing:
352
+ ## - if the number of sources (N, above) == 1 treat it
353
+ ## as a filename and save the object there.
354
+ ## - if there's more sources -> Error
355
+ ## if --recursive:
356
+ ## - first N arguments must be S3Uri
357
+ ## - for each Uri get a list of remote objects with that Uri as a prefix
358
+ ## - apply exclude/include rules
359
+ ## - each list item will have MD5sum, Timestamp and pointer to S3Uri
360
+ ## used as a prefix.
361
+ ## - the last arg may be '-' (stdout)
362
+ ## - the last arg may be a local directory - destination_base
363
+ ## - if the last one is S3 make current dir the destination_base
364
+ ## - if the last one doesn't exist check remote list:
365
+ ## - if there is only one item and its_prefix==its_name
366
+ ## download that item to the name given in last arg.
367
+ ## - if there are more remote items use the last arg as a destination_base
368
+ ## and try to create the directory (incl. all parents).
369
+ ##
370
+ ## In both cases we end up with a list mapping remote object names (keys) to local file names.
371
+
372
+ ## Each item will be a dict with the following attributes
373
+ # {'remote_uri', 'local_filename'}
374
+ download_list = []
375
+
376
+ if len(args) == 0:
377
+ raise ParameterError("Nothing to download. Expecting S3 URI.")
378
+
379
+ if S3Uri(args[-1]).type == 'file':
380
+ destination_base = args.pop()
381
+ else:
382
+ destination_base = "."
383
+
384
+ if len(args) == 0:
385
+ raise ParameterError("Nothing to download. Expecting S3 URI.")
386
+
387
+ remote_list = fetch_remote_list(args, require_attribs = False)
388
+ remote_list, exclude_list = filter_exclude_include(remote_list)
389
+
390
+ remote_count = len(remote_list)
391
+
392
+ info(u"Summary: %d remote files to download" % remote_count)
393
+
394
+ if remote_count > 0:
395
+ if destination_base == "-":
396
+ ## stdout is ok for multiple remote files!
397
+ for key in remote_list:
398
+ remote_list[key]['local_filename'] = "-"
399
+ elif not os.path.isdir(destination_base):
400
+ ## We were either given a file name (existing or not)
401
+ if remote_count > 1:
402
+ raise ParameterError("Destination must be a directory or stdout when downloading multiple sources.")
403
+ remote_list[remote_list.keys()[0]]['local_filename'] = deunicodise(destination_base)
404
+ elif os.path.isdir(destination_base):
405
+ if destination_base[-1] != os.path.sep:
406
+ destination_base += os.path.sep
407
+ for key in remote_list:
408
+ remote_list[key]['local_filename'] = destination_base + key
409
+ else:
410
+ raise InternalError("WTF? Is it a dir or not? -- %s" % destination_base)
411
+
412
+ if cfg.dry_run:
413
+ for key in exclude_list:
414
+ output(u"exclude: %s" % unicodise(key))
415
+ for key in remote_list:
416
+ output(u"download: %s -> %s" % (remote_list[key]['object_uri_str'], remote_list[key]['local_filename']))
417
+
418
+ warning(u"Exiting now because of --dry-run")
419
+ return
420
+
421
+ seq = 0
422
+ for key in remote_list:
423
+ seq += 1
424
+ item = remote_list[key]
425
+ uri = S3Uri(item['object_uri_str'])
426
+ ## Encode / Decode destination with "replace" to make sure it's compatible with current encoding
427
+ destination = unicodise_safe(item['local_filename'])
428
+ seq_label = "[%d of %d]" % (seq, remote_count)
429
+
430
+ start_position = 0
431
+
432
+ if destination == "-":
433
+ ## stdout
434
+ dst_stream = sys.__stdout__
435
+ else:
436
+ ## File
437
+ try:
438
+ file_exists = os.path.exists(destination)
439
+ try:
440
+ dst_stream = open(destination, "ab")
441
+ except IOError, e:
442
+ if e.errno == errno.ENOENT:
443
+ basename = destination[:destination.rindex(os.path.sep)]
444
+ info(u"Creating directory: %s" % basename)
445
+ os.makedirs(basename)
446
+ dst_stream = open(destination, "ab")
447
+ else:
448
+ raise
449
+ if file_exists:
450
+ if Config().get_continue:
451
+ start_position = dst_stream.tell()
452
+ elif Config().force:
453
+ start_position = 0L
454
+ dst_stream.seek(0L)
455
+ dst_stream.truncate()
456
+ elif Config().skip_existing:
457
+ info(u"Skipping over existing file: %s" % (destination))
458
+ continue
459
+ else:
460
+ dst_stream.close()
461
+ raise ParameterError(u"File %s already exists. Use either of --force / --continue / --skip-existing or give it a new name." % destination)
462
+ except IOError, e:
463
+ error(u"Skipping %s: %s" % (destination, e.strerror))
464
+ continue
465
+ try:
466
+ response = s3.object_get(uri, dst_stream, start_position = start_position, extra_label = seq_label)
467
+ except S3Error, e:
468
+ if not file_exists: # Delete, only if file didn't exist before!
469
+ debug(u"object_get failed for '%s', deleting..." % (destination,))
470
+ os.unlink(destination)
471
+ raise
472
+
473
+ if response["headers"].has_key("x-amz-meta-s3tools-gpgenc"):
474
+ gpg_decrypt(destination, response["headers"]["x-amz-meta-s3tools-gpgenc"])
475
+ response["size"] = os.stat(destination)[6]
476
+ if not Config().progress_meter and destination != "-":
477
+ speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
478
+ output(u"File %s saved as '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s)" %
479
+ (uri, destination, response["size"], response["elapsed"], speed_fmt[0], speed_fmt[1]))
480
+ if Config().delete_after_fetch:
481
+ s3.object_delete(uri)
482
+ output(u"File %s removed after fetch" % (uri))
483
+
484
+ def cmd_object_del(args):
485
+ for uri_str in args:
486
+ uri = S3Uri(uri_str)
487
+ if uri.type != "s3":
488
+ raise ParameterError("Expecting S3 URI instead of '%s'" % uri_str)
489
+ if not uri.has_object():
490
+ if Config().recursive and not Config().force:
491
+ raise ParameterError("Please use --force to delete ALL contents of %s" % uri_str)
492
+ elif not Config().recursive:
493
+ raise ParameterError("File name required, not only the bucket name. Alternatively use --recursive")
494
+ subcmd_object_del_uri(uri_str)
495
+
496
+ def subcmd_object_del_uri(uri_str, recursive = None):
497
+ s3 = S3(cfg)
498
+
499
+ if recursive is None:
500
+ recursive = cfg.recursive
501
+
502
+ remote_list = fetch_remote_list(uri_str, require_attribs = False, recursive = recursive)
503
+ remote_list, exclude_list = filter_exclude_include(remote_list)
504
+
505
+ remote_count = len(remote_list)
506
+
507
+ info(u"Summary: %d remote files to delete" % remote_count)
508
+
509
+ if cfg.dry_run:
510
+ for key in exclude_list:
511
+ output(u"exclude: %s" % unicodise(key))
512
+ for key in remote_list:
513
+ output(u"delete: %s" % remote_list[key]['object_uri_str'])
514
+
515
+ warning(u"Exiting now because of --dry-run")
516
+ return
517
+
518
+ for key in remote_list:
519
+ item = remote_list[key]
520
+ response = s3.object_delete(S3Uri(item['object_uri_str']))
521
+ output(u"File %s deleted" % item['object_uri_str'])
522
+
523
+ def subcmd_cp_mv(args, process_fce, action_str, message):
524
+ if len(args) < 2:
525
+ raise ParameterError("Expecting two or more S3 URIs for " + action_str)
526
+ dst_base_uri = S3Uri(args.pop())
527
+ if dst_base_uri.type != "s3":
528
+ raise ParameterError("Destination must be S3 URI. To download a file use 'get' or 'sync'.")
529
+ destination_base = dst_base_uri.uri()
530
+
531
+ remote_list = fetch_remote_list(args, require_attribs = False)
532
+ remote_list, exclude_list = filter_exclude_include(remote_list)
533
+
534
+ remote_count = len(remote_list)
535
+
536
+ info(u"Summary: %d remote files to %s" % (remote_count, action_str))
537
+
538
+ if cfg.recursive:
539
+ if not destination_base.endswith("/"):
540
+ destination_base += "/"
541
+ for key in remote_list:
542
+ remote_list[key]['dest_name'] = destination_base + key
543
+ else:
544
+ for key in remote_list:
545
+ if destination_base.endswith("/"):
546
+ remote_list[key]['dest_name'] = destination_base + key
547
+ else:
548
+ remote_list[key]['dest_name'] = destination_base
549
+
550
+ if cfg.dry_run:
551
+ for key in exclude_list:
552
+ output(u"exclude: %s" % unicodise(key))
553
+ for key in remote_list:
554
+ output(u"%s: %s -> %s" % (action_str, remote_list[key]['object_uri_str'], remote_list[key]['dest_name']))
555
+
556
+ warning(u"Exiting now because of --dry-run")
557
+ return
558
+
559
+ seq = 0
560
+ for key in remote_list:
561
+ seq += 1
562
+ seq_label = "[%d of %d]" % (seq, remote_count)
563
+
564
+ item = remote_list[key]
565
+ src_uri = S3Uri(item['object_uri_str'])
566
+ dst_uri = S3Uri(item['dest_name'])
567
+
568
+ extra_headers = copy(cfg.extra_headers)
569
+ response = process_fce(src_uri, dst_uri, extra_headers)
570
+ output(message % { "src" : src_uri, "dst" : dst_uri })
571
+ if Config().acl_public:
572
+ info(u"Public URL is: %s" % dst_uri.public_url())
573
+
574
+ def cmd_cp(args):
575
+ s3 = S3(Config())
576
+ subcmd_cp_mv(args, s3.object_copy, "copy", "File %(src)s copied to %(dst)s")
577
+
578
+ def cmd_mv(args):
579
+ s3 = S3(Config())
580
+ subcmd_cp_mv(args, s3.object_move, "move", "File %(src)s moved to %(dst)s")
581
+
582
+ def cmd_info(args):
583
+ s3 = S3(Config())
584
+
585
+ while (len(args)):
586
+ uri_arg = args.pop(0)
587
+ uri = S3Uri(uri_arg)
588
+ if uri.type != "s3" or not uri.has_bucket():
589
+ raise ParameterError("Expecting S3 URI instead of '%s'" % uri_arg)
590
+
591
+ try:
592
+ if uri.has_object():
593
+ info = s3.object_info(uri)
594
+ output(u"%s (object):" % uri.uri())
595
+ output(u" File size: %s" % info['headers']['content-length'])
596
+ output(u" Last mod: %s" % info['headers']['last-modified'])
597
+ output(u" MIME type: %s" % info['headers']['content-type'])
598
+ output(u" MD5 sum: %s" % info['headers']['etag'].strip('"'))
599
+ else:
600
+ info = s3.bucket_info(uri)
601
+ output(u"%s (bucket):" % uri.uri())
602
+ output(u" Location: %s" % info['bucket-location'])
603
+ acl = s3.get_acl(uri)
604
+ acl_grant_list = acl.getGrantList()
605
+
606
+ try:
607
+ policy = s3.get_policy(uri)
608
+ output(u" policy: %s" % policy)
609
+ except:
610
+ output(u" policy: none")
611
+
612
+ for grant in acl_grant_list:
613
+ output(u" ACL: %s: %s" % (grant['grantee'], grant['permission']))
614
+ if acl.isAnonRead():
615
+ output(u" URL: %s" % uri.public_url())
616
+
617
+ except S3Error, e:
618
+ if S3.codes.has_key(e.info["Code"]):
619
+ error(S3.codes[e.info["Code"]] % uri.bucket())
620
+ return
621
+ else:
622
+ raise
623
+
624
+ def cmd_sync_remote2remote(args):
625
+ def _do_deletes(s3, dst_list):
626
+ # Delete items in destination that are not in source
627
+ if cfg.dry_run:
628
+ for key in dst_list:
629
+ output(u"delete: %s" % dst_list[key]['object_uri_str'])
630
+ else:
631
+ for key in dst_list:
632
+ uri = S3Uri(dst_list[key]['object_uri_str'])
633
+ s3.object_delete(uri)
634
+ output(u"deleted: '%s'" % uri)
635
+
636
+ s3 = S3(Config())
637
+
638
+ # Normalise s3://uri (e.g. assert trailing slash)
639
+ destination_base = unicode(S3Uri(args[-1]))
640
+
641
+ src_list = fetch_remote_list(args[:-1], recursive = True, require_attribs = True)
642
+ dst_list = fetch_remote_list(destination_base, recursive = True, require_attribs = True)
643
+
644
+ src_count = len(src_list)
645
+ dst_count = len(dst_list)
646
+
647
+ info(u"Found %d source files, %d destination files" % (src_count, dst_count))
648
+
649
+ src_list, exclude_list = filter_exclude_include(src_list)
650
+
651
+ src_list, dst_list, update_list, copy_pairs = compare_filelists(src_list, dst_list, src_remote = True, dst_remote = True, delay_updates = cfg.delay_updates)
652
+
653
+ src_count = len(src_list)
654
+ update_count = len(update_list)
655
+ dst_count = len(dst_list)
656
+
657
+ print(u"Summary: %d source files to copy, %d files at destination to delete" % (src_count, dst_count))
658
+
659
+ ### Populate 'target_uri' only if we've got something to sync from src to dst
660
+ for key in src_list:
661
+ src_list[key]['target_uri'] = destination_base + key
662
+ for key in update_list:
663
+ update_list[key]['target_uri'] = destination_base + key
664
+
665
+ if cfg.dry_run:
666
+ for key in exclude_list:
667
+ output(u"exclude: %s" % unicodise(key))
668
+ if cfg.delete_removed:
669
+ for key in dst_list:
670
+ output(u"delete: %s" % dst_list[key]['object_uri_str'])
671
+ for key in src_list:
672
+ output(u"Sync: %s -> %s" % (src_list[key]['object_uri_str'], src_list[key]['target_uri']))
673
+ warning(u"Exiting now because of --dry-run")
674
+ return
675
+
676
+ # if there are copy pairs, we can't do delete_before, on the chance
677
+ # we need one of the to-be-deleted files as a copy source.
678
+ if len(copy_pairs) > 0:
679
+ cfg.delete_after = True
680
+
681
+ # Delete items in destination that are not in source
682
+ if cfg.delete_removed and not cfg.delete_after:
683
+ _do_deletes(s3, dst_list)
684
+
685
+ def _upload(src_list, seq, src_count):
686
+ file_list = src_list.keys()
687
+ file_list.sort()
688
+ for file in file_list:
689
+ seq += 1
690
+ item = src_list[file]
691
+ src_uri = S3Uri(item['object_uri_str'])
692
+ dst_uri = S3Uri(item['target_uri'])
693
+ seq_label = "[%d of %d]" % (seq, src_count)
694
+ extra_headers = copy(cfg.extra_headers)
695
+ try:
696
+ response = s3.object_copy(src_uri, dst_uri, extra_headers)
697
+ output("File %(src)s copied to %(dst)s" % { "src" : src_uri, "dst" : dst_uri })
698
+ except S3Error, e:
699
+ error("File %(src)s could not be copied: %(e)s" % { "src" : src_uri, "e" : e })
700
+ return seq
701
+
702
+ # Perform the synchronization of files
703
+ timestamp_start = time.time()
704
+ seq = 0
705
+ seq = _upload(src_list, seq, src_count + update_count)
706
+ seq = _upload(update_list, seq, src_count + update_count)
707
+ n_copied, bytes_saved = remote_copy(s3, copy_pairs, destination_base)
708
+
709
+ total_elapsed = time.time() - timestamp_start
710
+ outstr = "Done. Copied %d files in %0.1f seconds, %0.2f files/s" % (seq, total_elapsed, seq/total_elapsed)
711
+ if seq > 0:
712
+ output(outstr)
713
+ else:
714
+ info(outstr)
715
+
716
+ # Delete items in destination that are not in source
717
+ if cfg.delete_removed and cfg.delete_after:
718
+ _do_deletes(s3, dst_list)
719
+
720
+ def cmd_sync_remote2local(args):
721
+ def _do_deletes(local_list):
722
+ for key in local_list:
723
+ os.unlink(local_list[key]['full_name'])
724
+ output(u"deleted: %s" % local_list[key]['full_name_unicode'])
725
+
726
+ s3 = S3(Config())
727
+
728
+ destination_base = args[-1]
729
+ local_list, single_file_local = fetch_local_list(destination_base, recursive = True)
730
+ remote_list = fetch_remote_list(args[:-1], recursive = True, require_attribs = True)
731
+
732
+ local_count = len(local_list)
733
+ remote_count = len(remote_list)
734
+
735
+ info(u"Found %d remote files, %d local files" % (remote_count, local_count))
736
+
737
+ remote_list, exclude_list = filter_exclude_include(remote_list)
738
+
739
+ remote_list, local_list, update_list, copy_pairs = compare_filelists(remote_list, local_list, src_remote = True, dst_remote = False, delay_updates = cfg.delay_updates)
740
+
741
+ local_count = len(local_list)
742
+ remote_count = len(remote_list)
743
+ update_count = len(update_list)
744
+ copy_pairs_count = len(copy_pairs)
745
+
746
+ info(u"Summary: %d remote files to download, %d local files to delete, %d local files to hardlink" % (remote_count + update_count, local_count, copy_pairs_count))
747
+
748
+ def _set_local_filename(remote_list, destination_base):
749
+ if len(remote_list) == 0:
750
+ return
751
+ if not os.path.isdir(destination_base):
752
+ ## We were either given a file name (existing or not) or want STDOUT
753
+ if len(remote_list) > 1:
754
+ raise ParameterError("Destination must be a directory when downloading multiple sources.")
755
+ remote_list[remote_list.keys()[0]]['local_filename'] = deunicodise(destination_base)
756
+ else:
757
+ if destination_base[-1] != os.path.sep:
758
+ destination_base += os.path.sep
759
+ for key in remote_list:
760
+ local_filename = destination_base + key
761
+ if os.path.sep != "/":
762
+ local_filename = os.path.sep.join(local_filename.split("/"))
763
+ remote_list[key]['local_filename'] = deunicodise(local_filename)
764
+
765
+ _set_local_filename(remote_list, destination_base)
766
+ _set_local_filename(update_list, destination_base)
767
+
768
+ if cfg.dry_run:
769
+ for key in exclude_list:
770
+ output(u"exclude: %s" % unicodise(key))
771
+ if cfg.delete_removed:
772
+ for key in local_list:
773
+ output(u"delete: %s" % local_list[key]['full_name_unicode'])
774
+ for key in remote_list:
775
+ output(u"download: %s -> %s" % (unicodise(remote_list[key]['object_uri_str']), unicodise(remote_list[key]['local_filename'])))
776
+ for key in update_list:
777
+ output(u"download: %s -> %s" % (update_list[key]['object_uri_str'], update_list[key]['local_filename']))
778
+
779
+ warning(u"Exiting now because of --dry-run")
780
+ return
781
+
782
+ # if there are copy pairs, we can't do delete_before, on the chance
783
+ # we need one of the to-be-deleted files as a copy source.
784
+ if len(copy_pairs) > 0:
785
+ cfg.delete_after = True
786
+
787
+ if cfg.delete_removed and not cfg.delete_after:
788
+ _do_deletes(local_list)
789
+
790
+ def _download(remote_list, seq, total, total_size, dir_cache):
791
+ file_list = remote_list.keys()
792
+ file_list.sort()
793
+ for file in file_list:
794
+ seq += 1
795
+ item = remote_list[file]
796
+ uri = S3Uri(item['object_uri_str'])
797
+ dst_file = item['local_filename']
798
+ seq_label = "[%d of %d]" % (seq, total)
799
+ try:
800
+ dst_dir = os.path.dirname(dst_file)
801
+ if not dir_cache.has_key(dst_dir):
802
+ dir_cache[dst_dir] = Utils.mkdir_with_parents(dst_dir)
803
+ if dir_cache[dst_dir] == False:
804
+ warning(u"%s: destination directory not writable: %s" % (file, dst_dir))
805
+ continue
806
+ try:
807
+ debug(u"dst_file=%s" % unicodise(dst_file))
808
+ # create temporary files (of type .s3cmd.XXXX.tmp) in the same directory
809
+ # for downloading and then rename once downloaded
810
+ chkptfd, chkptfname = tempfile.mkstemp(".tmp",".s3cmd.",os.path.dirname(dst_file))
811
+ debug(u"created chkptfname=%s" % unicodise(chkptfname))
812
+ dst_stream = os.fdopen(chkptfd, "wb")
813
+ response = s3.object_get(uri, dst_stream, extra_label = seq_label)
814
+ dst_stream.close()
815
+ # download completed, rename the file to destination
816
+ os.rename(chkptfname, dst_file)
817
+
818
+ # set permissions on destination file
819
+ original_umask = os.umask(0);
820
+ os.umask(original_umask);
821
+ mode = 0777 - original_umask;
822
+ debug(u"mode=%s" % oct(mode))
823
+
824
+ os.chmod(dst_file, mode);
825
+
826
+ debug(u"renamed chkptfname=%s to dst_file=%s" % (unicodise(chkptfname), unicodise(dst_file)))
827
+ if response['headers'].has_key('x-amz-meta-s3cmd-attrs') and cfg.preserve_attrs:
828
+ attrs = parse_attrs_header(response['headers']['x-amz-meta-s3cmd-attrs'])
829
+ if attrs.has_key('mode'):
830
+ os.chmod(dst_file, int(attrs['mode']))
831
+ if attrs.has_key('mtime') or attrs.has_key('atime'):
832
+ mtime = attrs.has_key('mtime') and int(attrs['mtime']) or int(time.time())
833
+ atime = attrs.has_key('atime') and int(attrs['atime']) or int(time.time())
834
+ os.utime(dst_file, (atime, mtime))
835
+ ## FIXME: uid/gid / uname/gname handling comes here! TODO
836
+ except OSError, e:
837
+ try:
838
+ dst_stream.close()
839
+ os.remove(chkptfname)
840
+ except: pass
841
+ if e.errno == errno.EEXIST:
842
+ warning(u"%s exists - not overwriting" % (dst_file))
843
+ continue
844
+ if e.errno in (errno.EPERM, errno.EACCES):
845
+ warning(u"%s not writable: %s" % (dst_file, e.strerror))
846
+ continue
847
+ if e.errno == errno.EISDIR:
848
+ warning(u"%s is a directory - skipping over" % dst_file)
849
+ continue
850
+ raise e
851
+ except KeyboardInterrupt:
852
+ try:
853
+ dst_stream.close()
854
+ os.remove(chkptfname)
855
+ except: pass
856
+ warning(u"Exiting after keyboard interrupt")
857
+ return
858
+ except Exception, e:
859
+ try:
860
+ dst_stream.close()
861
+ os.remove(chkptfname)
862
+ except: pass
863
+ error(u"%s: %s" % (file, e))
864
+ continue
865
+ # We have to keep repeating this call because
866
+ # Python 2.4 doesn't support try/except/finally
867
+ # construction :-(
868
+ try:
869
+ dst_stream.close()
870
+ os.remove(chkptfname)
871
+ except: pass
872
+ except S3DownloadError, e:
873
+ error(u"%s: download failed too many times. Skipping that file." % file)
874
+ continue
875
+ speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
876
+ if not Config().progress_meter:
877
+ output(u"File '%s' stored as '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s) %s" %
878
+ (uri, unicodise(dst_file), response["size"], response["elapsed"], speed_fmt[0], speed_fmt[1],
879
+ seq_label))
880
+ total_size += response["size"]
881
+ if Config().delete_after_fetch:
882
+ s3.object_delete(uri)
883
+ output(u"File '%s' removed after syncing" % (uri))
884
+ return seq, total_size
885
+
886
+ total_size = 0
887
+ total_elapsed = 0.0
888
+ timestamp_start = time.time()
889
+ dir_cache = {}
890
+ seq = 0
891
+ seq, total_size = _download(remote_list, seq, remote_count + update_count, total_size, dir_cache)
892
+ seq, total_size = _download(update_list, seq, remote_count + update_count, total_size, dir_cache)
893
+
894
+ failed_copy_list = local_copy(copy_pairs, destination_base)
895
+ _set_local_filename(failed_copy_list, destination_base)
896
+ seq, total_size = _download(failed_copy_list, seq, len(failed_copy_list) + remote_count + update_count, total_size, dir_cache)
897
+
898
+ total_elapsed = time.time() - timestamp_start
899
+ speed_fmt = formatSize(total_size/total_elapsed, human_readable = True, floating_point = True)
900
+
901
+ # Only print out the result if any work has been done or
902
+ # if the user asked for verbose output
903
+ outstr = "Done. Downloaded %d bytes in %0.1f seconds, %0.2f %sB/s" % (total_size, total_elapsed, speed_fmt[0], speed_fmt[1])
904
+ if total_size > 0:
905
+ output(outstr)
906
+ else:
907
+ info(outstr)
908
+
909
+ if cfg.delete_removed and cfg.delete_after:
910
+ _do_deletes(local_list)
911
+
912
+ def local_copy(copy_pairs, destination_base):
913
+ # Do NOT hardlink local files by default, that'd be silly
914
+ # For instance all empty files would become hardlinked together!
915
+
916
+ failed_copy_list = FileDict()
917
+ for (src_obj, dst1, relative_file) in copy_pairs:
918
+ src_file = os.path.join(destination_base, dst1)
919
+ dst_file = os.path.join(destination_base, relative_file)
920
+ dst_dir = os.path.dirname(dst_file)
921
+ try:
922
+ if not os.path.isdir(dst_dir):
923
+ debug("MKDIR %s" % dst_dir)
924
+ os.makedirs(dst_dir)
925
+ debug(u"Copying %s to %s" % (src_file, dst_file))
926
+ shutil.copy2(src_file, dst_file)
927
+ except (IOError, OSError), e:
928
+ warning(u'Unable to hardlink or copy files %s -> %s: %s' % (src_file, dst_file, e))
929
+ failed_copy_list[relative_file] = src_obj
930
+ return failed_copy_list
931
+
932
+ def remote_copy(s3, copy_pairs, destination_base):
933
+ saved_bytes = 0
934
+ for (src_obj, dst1, dst2) in copy_pairs:
935
+ debug(u"Remote Copying from %s to %s" % (dst1, dst2))
936
+ dst1_uri = S3Uri(destination_base + dst1)
937
+ dst2_uri = S3Uri(destination_base + dst2)
938
+ extra_headers = copy(cfg.extra_headers)
939
+ try:
940
+ s3.object_copy(dst1_uri, dst2_uri, extra_headers)
941
+ info = s3.object_info(dst2_uri)
942
+ saved_bytes = saved_bytes + int(info['headers']['content-length'])
943
+ output(u"remote copy: %s -> %s" % (dst1, dst2))
944
+ except:
945
+ raise
946
+ return (len(copy_pairs), saved_bytes)
947
+
948
+
949
+ def cmd_sync_local2remote(args):
950
+ def _build_attr_header(local_list, src):
951
+ import pwd, grp
952
+ attrs = {}
953
+ for attr in cfg.preserve_attrs_list:
954
+ if attr == 'uname':
955
+ try:
956
+ val = pwd.getpwuid(local_list[src]['uid']).pw_name
957
+ except KeyError:
958
+ attr = "uid"
959
+ val = local_list[src].get('uid')
960
+ warning(u"%s: Owner username not known. Storing UID=%d instead." % (src, val))
961
+ elif attr == 'gname':
962
+ try:
963
+ val = grp.getgrgid(local_list[src].get('gid')).gr_name
964
+ except KeyError:
965
+ attr = "gid"
966
+ val = local_list[src].get('gid')
967
+ warning(u"%s: Owner groupname not known. Storing GID=%d instead." % (src, val))
968
+ elif attr == 'md5':
969
+ try:
970
+ val = local_list.get_md5(src)
971
+ except IOError:
972
+ val = None
973
+ else:
974
+ val = getattr(local_list[src]['sr'], 'st_' + attr)
975
+ attrs[attr] = val
976
+
977
+ if 'md5' in attrs and attrs['md5'] is None:
978
+ del attrs['md5']
979
+
980
+ result = ""
981
+ for k in attrs: result += "%s:%s/" % (k, attrs[k])
982
+ return { 'x-amz-meta-s3cmd-attrs' : result[:-1] }
983
+
984
+ def _do_deletes(s3, remote_list):
985
+ for key in remote_list:
986
+ uri = S3Uri(remote_list[key]['object_uri_str'])
987
+ s3.object_delete(uri)
988
+ output(u"deleted: '%s'" % uri)
989
+
990
+ def _single_process(local_list):
991
+ for dest in destinations:
992
+ ## Normalize URI to convert s3://bkt to s3://bkt/ (trailing slash)
993
+ destination_base_uri = S3Uri(dest)
994
+ if destination_base_uri.type != 's3':
995
+ raise ParameterError("Destination must be S3Uri. Got: %s" % destination_base_uri)
996
+ destination_base = str(destination_base_uri)
997
+ _child(destination_base, local_list)
998
+ return destination_base_uri
999
+
1000
+ def _parent():
1001
+ # Now that we've done all the disk I/O to look at the local file system and
1002
+ # calculate the md5 for each file, fork for each destination to upload to them separately
1003
+ # and in parallel
1004
+ child_pids = []
1005
+
1006
+ for dest in destinations:
1007
+ ## Normalize URI to convert s3://bkt to s3://bkt/ (trailing slash)
1008
+ destination_base_uri = S3Uri(dest)
1009
+ if destination_base_uri.type != 's3':
1010
+ raise ParameterError("Destination must be S3Uri. Got: %s" % destination_base_uri)
1011
+ destination_base = str(destination_base_uri)
1012
+ child_pid = os.fork()
1013
+ if child_pid == 0:
1014
+ _child(destination_base, local_list)
1015
+ os._exit(0)
1016
+ else:
1017
+ child_pids.append(child_pid)
1018
+
1019
+ while len(child_pids):
1020
+ (pid, status) = os.wait()
1021
+ child_pids.remove(pid)
1022
+
1023
+ return
1024
+
1025
+ def _child(destination_base, local_list):
1026
+ def _set_remote_uri(local_list, destination_base, single_file_local):
1027
+ if len(local_list) > 0:
1028
+ ## Populate 'remote_uri' only if we've got something to upload
1029
+ if not destination_base.endswith("/"):
1030
+ if not single_file_local:
1031
+ raise ParameterError("Destination S3 URI must end with '/' (ie must refer to a directory on the remote side).")
1032
+ local_list[local_list.keys()[0]]['remote_uri'] = unicodise(destination_base)
1033
+ else:
1034
+ for key in local_list:
1035
+ local_list[key]['remote_uri'] = unicodise(destination_base + key)
1036
+
1037
+ def _upload(local_list, seq, total, total_size):
1038
+ file_list = local_list.keys()
1039
+ file_list.sort()
1040
+ for file in file_list:
1041
+ seq += 1
1042
+ item = local_list[file]
1043
+ src = item['full_name']
1044
+ uri = S3Uri(item['remote_uri'])
1045
+ seq_label = "[%d of %d]" % (seq, total)
1046
+ extra_headers = copy(cfg.extra_headers)
1047
+ try:
1048
+ if cfg.preserve_attrs:
1049
+ attr_header = _build_attr_header(local_list, file)
1050
+ debug(u"attr_header: %s" % attr_header)
1051
+ extra_headers.update(attr_header)
1052
+ response = s3.object_put(src, uri, extra_headers, extra_label = seq_label)
1053
+ except InvalidFileError, e:
1054
+ warning(u"File can not be uploaded: %s" % e)
1055
+ continue
1056
+ except S3UploadError, e:
1057
+ error(u"%s: upload failed too many times. Skipping that file." % item['full_name_unicode'])
1058
+ continue
1059
+ speed_fmt = formatSize(response["speed"], human_readable = True, floating_point = True)
1060
+ if not cfg.progress_meter:
1061
+ output(u"File '%s' stored as '%s' (%d bytes in %0.1f seconds, %0.2f %sB/s) %s" %
1062
+ (item['full_name_unicode'], uri, response["size"], response["elapsed"],
1063
+ speed_fmt[0], speed_fmt[1], seq_label))
1064
+ total_size += response["size"]
1065
+ uploaded_objects_list.append(uri.object())
1066
+ return seq, total_size
1067
+
1068
+ remote_list = fetch_remote_list(destination_base, recursive = True, require_attribs = True)
1069
+
1070
+ local_count = len(local_list)
1071
+ remote_count = len(remote_list)
1072
+
1073
+ info(u"Found %d local files, %d remote files" % (local_count, remote_count))
1074
+
1075
+ local_list, exclude_list = filter_exclude_include(local_list)
1076
+
1077
+ if single_file_local and len(local_list) == 1 and len(remote_list) == 1:
1078
+ ## Make remote_key same as local_key for comparison if we're dealing with only one file
1079
+ remote_list_entry = remote_list[remote_list.keys()[0]]
1080
+ # Flush remote_list, by the way
1081
+ remote_list = FileDict()
1082
+ remote_list[local_list.keys()[0]] = remote_list_entry
1083
+
1084
+ local_list, remote_list, update_list, copy_pairs = compare_filelists(local_list, remote_list, src_remote = False, dst_remote = True, delay_updates = cfg.delay_updates)
1085
+
1086
+ local_count = len(local_list)
1087
+ update_count = len(update_list)
1088
+ copy_count = len(copy_pairs)
1089
+ remote_count = len(remote_list)
1090
+
1091
+ info(u"Summary: %d local files to upload, %d files to remote copy, %d remote files to delete" % (local_count + update_count, copy_count, remote_count))
1092
+
1093
+ _set_remote_uri(local_list, destination_base, single_file_local)
1094
+ _set_remote_uri(update_list, destination_base, single_file_local)
1095
+
1096
+ if cfg.dry_run:
1097
+ for key in exclude_list:
1098
+ output(u"exclude: %s" % unicodise(key))
1099
+ for key in local_list:
1100
+ output(u"upload: %s -> %s" % (local_list[key]['full_name_unicode'], local_list[key]['remote_uri']))
1101
+ for key in update_list:
1102
+ output(u"upload: %s -> %s" % (update_list[key]['full_name_unicode'], update_list[key]['remote_uri']))
1103
+ for (src_obj, dst1, dst2) in copy_pairs:
1104
+ output(u"remote copy: %s -> %s" % (dst1, dst2))
1105
+ if cfg.delete_removed:
1106
+ for key in remote_list:
1107
+ output(u"delete: %s" % remote_list[key]['object_uri_str'])
1108
+
1109
+ warning(u"Exiting now because of --dry-run")
1110
+ return
1111
+
1112
+ # if there are copy pairs, we can't do delete_before, on the chance
1113
+ # we need one of the to-be-deleted files as a copy source.
1114
+ if len(copy_pairs) > 0:
1115
+ cfg.delete_after = True
1116
+
1117
+ if cfg.delete_removed and not cfg.delete_after:
1118
+ _do_deletes(s3, remote_list)
1119
+
1120
+ total_size = 0
1121
+ total_elapsed = 0.0
1122
+ timestamp_start = time.time()
1123
+ n, total_size = _upload(local_list, 0, local_count, total_size)
1124
+ n, total_size = _upload(update_list, n, local_count, total_size)
1125
+ n_copies, saved_bytes = remote_copy(s3, copy_pairs, destination_base)
1126
+ if cfg.delete_removed and cfg.delete_after:
1127
+ _do_deletes(s3, remote_list)
1128
+ total_elapsed = time.time() - timestamp_start
1129
+ total_speed = total_elapsed and total_size/total_elapsed or 0.0
1130
+ speed_fmt = formatSize(total_speed, human_readable = True, floating_point = True)
1131
+
1132
+ # Only print out the result if any work has been done or
1133
+ # if the user asked for verbose output
1134
+ outstr = "Done. Uploaded %d bytes in %0.1f seconds, %0.2f %sB/s. Copied %d files saving %d bytes transfer." % (total_size, total_elapsed, speed_fmt[0], speed_fmt[1], n_copies, saved_bytes)
1135
+ if total_size + saved_bytes > 0:
1136
+ output(outstr)
1137
+ else:
1138
+ info(outstr)
1139
+
1140
+ return
1141
+
1142
+ def _invalidate_on_cf(destination_base_uri):
1143
+ cf = CloudFront(cfg)
1144
+ default_index_file = None
1145
+ if cfg.invalidate_default_index_on_cf or cfg.invalidate_default_index_root_on_cf:
1146
+ info_response = s3.website_info(destination_base_uri, cfg.bucket_location)
1147
+ if info_response:
1148
+ default_index_file = info_response['index_document']
1149
+ if len(default_index_file) < 1:
1150
+ default_index_file = None
1151
+
1152
+ result = cf.InvalidateObjects(destination_base_uri, uploaded_objects_list, default_index_file, cfg.invalidate_default_index_on_cf, cfg.invalidate_default_index_root_on_cf)
1153
+ if result['status'] == 201:
1154
+ output("Created invalidation request for %d paths" % len(uploaded_objects_list))
1155
+ output("Check progress with: s3cmd cfinvalinfo cf://%s/%s" % (result['dist_id'], result['request_id']))
1156
+
1157
+
1158
+ # main execution
1159
+ s3 = S3(cfg)
1160
+ uploaded_objects_list = []
1161
+
1162
+ if cfg.encrypt:
1163
+ error(u"S3cmd 'sync' doesn't yet support GPG encryption, sorry.")
1164
+ error(u"Either use unconditional 's3cmd put --recursive'")
1165
+ error(u"or disable encryption with --no-encrypt parameter.")
1166
+ sys.exit(1)
1167
+
1168
+ local_list, single_file_local = fetch_local_list(args[:-1], recursive = True)
1169
+
1170
+ destinations = [args[-1]]
1171
+ if cfg.additional_destinations:
1172
+ destinations = destinations + cfg.additional_destinations
1173
+
1174
+ if 'fork' not in os.__all__ or len(destinations) < 2:
1175
+ destination_base_uri = _single_process(local_list)
1176
+ if cfg.invalidate_on_cf:
1177
+ if len(uploaded_objects_list) == 0:
1178
+ info("Nothing to invalidate in CloudFront")
1179
+ else:
1180
+ _invalidate_on_cf(destination_base_uri)
1181
+ else:
1182
+ _parent()
1183
+ if cfg.invalidate_on_cf:
1184
+ error(u"You cannot use both --cf-invalidate and --add-destination.")
1185
+
1186
+ def cmd_sync(args):
1187
+ if (len(args) < 2):
1188
+ raise ParameterError("Too few parameters! Expected: %s" % commands['sync']['param'])
1189
+
1190
+ if S3Uri(args[0]).type == "file" and S3Uri(args[-1]).type == "s3":
1191
+ return cmd_sync_local2remote(args)
1192
+ if S3Uri(args[0]).type == "s3" and S3Uri(args[-1]).type == "file":
1193
+ return cmd_sync_remote2local(args)
1194
+ if S3Uri(args[0]).type == "s3" and S3Uri(args[-1]).type == "s3":
1195
+ return cmd_sync_remote2remote(args)
1196
+ raise ParameterError("Invalid source/destination: '%s'" % "' '".join(args))
1197
+
1198
+ def cmd_setacl(args):
1199
+ s3 = S3(cfg)
1200
+
1201
+ set_to_acl = cfg.acl_public and "Public" or "Private"
1202
+
1203
+ if not cfg.recursive:
1204
+ old_args = args
1205
+ args = []
1206
+ for arg in old_args:
1207
+ uri = S3Uri(arg)
1208
+ if not uri.has_object():
1209
+ if cfg.acl_public != None:
1210
+ info("Setting bucket-level ACL for %s to %s" % (uri.uri(), set_to_acl))
1211
+ else:
1212
+ info("Setting bucket-level ACL for %s" % (uri.uri()))
1213
+ if not cfg.dry_run:
1214
+ update_acl(s3, uri)
1215
+ else:
1216
+ args.append(arg)
1217
+
1218
+ remote_list = fetch_remote_list(args)
1219
+ remote_list, exclude_list = filter_exclude_include(remote_list)
1220
+
1221
+ remote_count = len(remote_list)
1222
+
1223
+ info(u"Summary: %d remote files to update" % remote_count)
1224
+
1225
+ if cfg.dry_run:
1226
+ for key in exclude_list:
1227
+ output(u"exclude: %s" % unicodise(key))
1228
+ for key in remote_list:
1229
+ output(u"setacl: %s" % remote_list[key]['object_uri_str'])
1230
+
1231
+ warning(u"Exiting now because of --dry-run")
1232
+ return
1233
+
1234
+ seq = 0
1235
+ for key in remote_list:
1236
+ seq += 1
1237
+ seq_label = "[%d of %d]" % (seq, remote_count)
1238
+ uri = S3Uri(remote_list[key]['object_uri_str'])
1239
+ update_acl(s3, uri, seq_label)
1240
+
1241
+ def cmd_setpolicy(args):
1242
+ s3 = S3(cfg)
1243
+ uri = S3Uri(args[1])
1244
+ policy_file = args[0]
1245
+ policy = open(policy_file, 'r').read()
1246
+
1247
+ if cfg.dry_run: return
1248
+
1249
+ response = s3.set_policy(uri, policy)
1250
+
1251
+ #if retsponse['status'] == 200:
1252
+ debug(u"response - %s" % response['status'])
1253
+ if response['status'] == 204:
1254
+ output(u"%s: Policy updated" % uri)
1255
+
1256
+ def cmd_delpolicy(args):
1257
+ s3 = S3(cfg)
1258
+ uri = S3Uri(args[0])
1259
+ if cfg.dry_run: return
1260
+
1261
+ response = s3.delete_policy(uri)
1262
+
1263
+ #if retsponse['status'] == 200:
1264
+ debug(u"response - %s" % response['status'])
1265
+ output(u"%s: Policy deleted" % uri)
1266
+
1267
+
1268
+ def cmd_accesslog(args):
1269
+ s3 = S3(cfg)
1270
+ bucket_uri = S3Uri(args.pop())
1271
+ if bucket_uri.object():
1272
+ raise ParameterError("Only bucket name is required for [accesslog] command")
1273
+ if cfg.log_target_prefix == False:
1274
+ accesslog, response = s3.set_accesslog(bucket_uri, enable = False)
1275
+ elif cfg.log_target_prefix:
1276
+ log_target_prefix_uri = S3Uri(cfg.log_target_prefix)
1277
+ if log_target_prefix_uri.type != "s3":
1278
+ raise ParameterError("--log-target-prefix must be a S3 URI")
1279
+ accesslog, response = s3.set_accesslog(bucket_uri, enable = True, log_target_prefix_uri = log_target_prefix_uri, acl_public = cfg.acl_public)
1280
+ else: # cfg.log_target_prefix == None
1281
+ accesslog = s3.get_accesslog(bucket_uri)
1282
+
1283
+ output(u"Access logging for: %s" % bucket_uri.uri())
1284
+ output(u" Logging Enabled: %s" % accesslog.isLoggingEnabled())
1285
+ if accesslog.isLoggingEnabled():
1286
+ output(u" Target prefix: %s" % accesslog.targetPrefix().uri())
1287
+ #output(u" Public Access: %s" % accesslog.isAclPublic())
1288
+
1289
+ def cmd_sign(args):
1290
+ string_to_sign = args.pop()
1291
+ debug("string-to-sign: %r" % string_to_sign)
1292
+ signature = Utils.sign_string(string_to_sign)
1293
+ output("Signature: %s" % signature)
1294
+
1295
+ def cmd_signurl(args):
1296
+ expiry = args.pop()
1297
+ url_to_sign = S3Uri(args.pop())
1298
+ if url_to_sign.type != 's3':
1299
+ raise ParameterError("Must be S3Uri. Got: %s" % url_to_sign)
1300
+ debug("url to sign: %r" % url_to_sign)
1301
+ signed_url = Utils.sign_url(url_to_sign, expiry)
1302
+ output(signed_url)
1303
+
1304
+ def cmd_fixbucket(args):
1305
+ def _unescape(text):
1306
+ ##
1307
+ # Removes HTML or XML character references and entities from a text string.
1308
+ #
1309
+ # @param text The HTML (or XML) source text.
1310
+ # @return The plain text, as a Unicode string, if necessary.
1311
+ #
1312
+ # From: http://effbot.org/zone/re-sub.htm#unescape-html
1313
+ def _unescape_fixup(m):
1314
+ text = m.group(0)
1315
+ if not htmlentitydefs.name2codepoint.has_key('apos'):
1316
+ htmlentitydefs.name2codepoint['apos'] = ord("'")
1317
+ if text[:2] == "&#":
1318
+ # character reference
1319
+ try:
1320
+ if text[:3] == "&#x":
1321
+ return unichr(int(text[3:-1], 16))
1322
+ else:
1323
+ return unichr(int(text[2:-1]))
1324
+ except ValueError:
1325
+ pass
1326
+ else:
1327
+ # named entity
1328
+ try:
1329
+ text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
1330
+ except KeyError:
1331
+ pass
1332
+ return text # leave as is
1333
+ text = text.encode('ascii', 'xmlcharrefreplace')
1334
+ return re.sub("&#?\w+;", _unescape_fixup, text)
1335
+
1336
+ cfg.urlencoding_mode = "fixbucket"
1337
+ s3 = S3(cfg)
1338
+
1339
+ count = 0
1340
+ for arg in args:
1341
+ culprit = S3Uri(arg)
1342
+ if culprit.type != "s3":
1343
+ raise ParameterError("Expecting S3Uri instead of: %s" % arg)
1344
+ response = s3.bucket_list_noparse(culprit.bucket(), culprit.object(), recursive = True)
1345
+ r_xent = re.compile("&#x[\da-fA-F]+;")
1346
+ response['data'] = unicode(response['data'], 'UTF-8')
1347
+ keys = re.findall("<Key>(.*?)</Key>", response['data'], re.MULTILINE)
1348
+ debug("Keys: %r" % keys)
1349
+ for key in keys:
1350
+ if r_xent.search(key):
1351
+ info("Fixing: %s" % key)
1352
+ debug("Step 1: Transforming %s" % key)
1353
+ key_bin = _unescape(key)
1354
+ debug("Step 2: ... to %s" % key_bin)
1355
+ key_new = replace_nonprintables(key_bin)
1356
+ debug("Step 3: ... then to %s" % key_new)
1357
+ src = S3Uri("s3://%s/%s" % (culprit.bucket(), key_bin))
1358
+ dst = S3Uri("s3://%s/%s" % (culprit.bucket(), key_new))
1359
+ resp_move = s3.object_move(src, dst)
1360
+ if resp_move['status'] == 200:
1361
+ output("File %r renamed to %s" % (key_bin, key_new))
1362
+ count += 1
1363
+ else:
1364
+ error("Something went wrong for: %r" % key)
1365
+ error("Please report the problem to s3tools-bugs@lists.sourceforge.net")
1366
+ if count > 0:
1367
+ warning("Fixed %d files' names. Their ACL were reset to Private." % count)
1368
+ warning("Use 's3cmd setacl --acl-public s3://...' to make")
1369
+ warning("them publicly readable if required.")
1370
+
1371
+ def resolve_list(lst, args):
1372
+ retval = []
1373
+ for item in lst:
1374
+ retval.append(item % args)
1375
+ return retval
1376
+
1377
+ def gpg_command(command, passphrase = ""):
1378
+ debug("GPG command: " + " ".join(command))
1379
+ p = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
1380
+ p_stdout, p_stderr = p.communicate(passphrase + "\n")
1381
+ debug("GPG output:")
1382
+ for line in p_stdout.split("\n"):
1383
+ debug("GPG: " + line)
1384
+ p_exitcode = p.wait()
1385
+ return p_exitcode
1386
+
1387
+ def gpg_encrypt(filename):
1388
+ tmp_filename = Utils.mktmpfile()
1389
+ args = {
1390
+ "gpg_command" : cfg.gpg_command,
1391
+ "passphrase_fd" : "0",
1392
+ "input_file" : filename,
1393
+ "output_file" : tmp_filename,
1394
+ }
1395
+ info(u"Encrypting file %(input_file)s to %(output_file)s..." % args)
1396
+ command = resolve_list(cfg.gpg_encrypt.split(" "), args)
1397
+ code = gpg_command(command, cfg.gpg_passphrase)
1398
+ return (code, tmp_filename, "gpg")
1399
+
1400
+ def gpg_decrypt(filename, gpgenc_header = "", in_place = True):
1401
+ tmp_filename = Utils.mktmpfile(filename)
1402
+ args = {
1403
+ "gpg_command" : cfg.gpg_command,
1404
+ "passphrase_fd" : "0",
1405
+ "input_file" : filename,
1406
+ "output_file" : tmp_filename,
1407
+ }
1408
+ info(u"Decrypting file %(input_file)s to %(output_file)s..." % args)
1409
+ command = resolve_list(cfg.gpg_decrypt.split(" "), args)
1410
+ code = gpg_command(command, cfg.gpg_passphrase)
1411
+ if code == 0 and in_place:
1412
+ debug(u"Renaming %s to %s" % (tmp_filename, filename))
1413
+ os.unlink(filename)
1414
+ os.rename(tmp_filename, filename)
1415
+ tmp_filename = filename
1416
+ return (code, tmp_filename)
1417
+
1418
+ def run_configure(config_file, args):
1419
+ cfg = Config()
1420
+ options = [
1421
+ ("access_key", "Access Key", "Access key and Secret key are your identifiers for Amazon S3"),
1422
+ ("secret_key", "Secret Key"),
1423
+ ("gpg_passphrase", "Encryption password", "Encryption password is used to protect your files from reading\nby unauthorized persons while in transfer to S3"),
1424
+ ("gpg_command", "Path to GPG program"),
1425
+ ("use_https", "Use HTTPS protocol", "When using secure HTTPS protocol all communication with Amazon S3\nservers is protected from 3rd party eavesdropping. This method is\nslower than plain HTTP and can't be used if you're behind a proxy"),
1426
+ ("proxy_host", "HTTP Proxy server name", "On some networks all internet access must go through a HTTP proxy.\nTry setting it here if you can't conect to S3 directly"),
1427
+ ("proxy_port", "HTTP Proxy server port"),
1428
+ ]
1429
+ ## Option-specfic defaults
1430
+ if getattr(cfg, "gpg_command") == "":
1431
+ setattr(cfg, "gpg_command", find_executable("gpg"))
1432
+
1433
+ if getattr(cfg, "proxy_host") == "" and os.getenv("http_proxy"):
1434
+ re_match=re.match("(http://)?([^:]+):(\d+)", os.getenv("http_proxy"))
1435
+ if re_match:
1436
+ setattr(cfg, "proxy_host", re_match.groups()[1])
1437
+ setattr(cfg, "proxy_port", re_match.groups()[2])
1438
+
1439
+ try:
1440
+ while 1:
1441
+ output(u"\nEnter new values or accept defaults in brackets with Enter.")
1442
+ output(u"Refer to user manual for detailed description of all options.")
1443
+ for option in options:
1444
+ prompt = option[1]
1445
+ ## Option-specific handling
1446
+ if option[0] == 'proxy_host' and getattr(cfg, 'use_https') == True:
1447
+ setattr(cfg, option[0], "")
1448
+ continue
1449
+ if option[0] == 'proxy_port' and getattr(cfg, 'proxy_host') == "":
1450
+ setattr(cfg, option[0], 0)
1451
+ continue
1452
+
1453
+ try:
1454
+ val = getattr(cfg, option[0])
1455
+ if type(val) is bool:
1456
+ val = val and "Yes" or "No"
1457
+ if val not in (None, ""):
1458
+ prompt += " [%s]" % val
1459
+ except AttributeError:
1460
+ pass
1461
+
1462
+ if len(option) >= 3:
1463
+ output(u"\n%s" % option[2])
1464
+
1465
+ val = raw_input(prompt + ": ")
1466
+ if val != "":
1467
+ if type(getattr(cfg, option[0])) is bool:
1468
+ # Turn 'Yes' into True, everything else into False
1469
+ val = val.lower().startswith('y')
1470
+ setattr(cfg, option[0], val)
1471
+ output(u"\nNew settings:")
1472
+ for option in options:
1473
+ output(u" %s: %s" % (option[1], getattr(cfg, option[0])))
1474
+ val = raw_input("\nTest access with supplied credentials? [Y/n] ")
1475
+ if val.lower().startswith("y") or val == "":
1476
+ try:
1477
+ # Default, we try to list 'all' buckets which requires
1478
+ # ListAllMyBuckets permission
1479
+ if len(args) == 0:
1480
+ output(u"Please wait, attempting to list all buckets...")
1481
+ S3(Config()).bucket_list("", "")
1482
+ else:
1483
+ # If user specified a bucket name directly, we check it and only it.
1484
+ # Thus, access check can succeed even if user only has access to
1485
+ # to a single bucket and not ListAllMyBuckets permission.
1486
+ output(u"Please wait, attempting to list bucket: " + args[0])
1487
+ uri = S3Uri(args[0])
1488
+ if uri.type == "s3" and uri.has_bucket():
1489
+ S3(Config()).bucket_list(uri.bucket(), "")
1490
+ else:
1491
+ raise Exception(u"Invalid bucket uri: " + args[0])
1492
+
1493
+ output(u"Success. Your access key and secret key worked fine :-)")
1494
+
1495
+ output(u"\nNow verifying that encryption works...")
1496
+ if not getattr(cfg, "gpg_command") or not getattr(cfg, "gpg_passphrase"):
1497
+ output(u"Not configured. Never mind.")
1498
+ else:
1499
+ if not getattr(cfg, "gpg_command"):
1500
+ raise Exception("Path to GPG program not set")
1501
+ if not os.path.isfile(getattr(cfg, "gpg_command")):
1502
+ raise Exception("GPG program not found")
1503
+ filename = Utils.mktmpfile()
1504
+ f = open(filename, "w")
1505
+ f.write(os.sys.copyright)
1506
+ f.close()
1507
+ ret_enc = gpg_encrypt(filename)
1508
+ ret_dec = gpg_decrypt(ret_enc[1], ret_enc[2], False)
1509
+ hash = [
1510
+ Utils.hash_file_md5(filename),
1511
+ Utils.hash_file_md5(ret_enc[1]),
1512
+ Utils.hash_file_md5(ret_dec[1]),
1513
+ ]
1514
+ os.unlink(filename)
1515
+ os.unlink(ret_enc[1])
1516
+ os.unlink(ret_dec[1])
1517
+ if hash[0] == hash[2] and hash[0] != hash[1]:
1518
+ output ("Success. Encryption and decryption worked fine :-)")
1519
+ else:
1520
+ raise Exception("Encryption verification error.")
1521
+
1522
+ except Exception, e:
1523
+ error(u"Test failed: %s" % (e))
1524
+ val = raw_input("\nRetry configuration? [Y/n] ")
1525
+ if val.lower().startswith("y") or val == "":
1526
+ continue
1527
+
1528
+
1529
+ val = raw_input("\nSave settings? [y/N] ")
1530
+ if val.lower().startswith("y"):
1531
+ break
1532
+ val = raw_input("Retry configuration? [Y/n] ")
1533
+ if val.lower().startswith("n"):
1534
+ raise EOFError()
1535
+
1536
+ ## Overwrite existing config file, make it user-readable only
1537
+ old_mask = os.umask(0077)
1538
+ try:
1539
+ os.remove(config_file)
1540
+ except OSError, e:
1541
+ if e.errno != errno.ENOENT:
1542
+ raise
1543
+ f = open(config_file, "w")
1544
+ os.umask(old_mask)
1545
+ cfg.dump_config(f)
1546
+ f.close()
1547
+ output(u"Configuration saved to '%s'" % config_file)
1548
+
1549
+ except (EOFError, KeyboardInterrupt):
1550
+ output(u"\nConfiguration aborted. Changes were NOT saved.")
1551
+ return
1552
+
1553
+ except IOError, e:
1554
+ error(u"Writing config file failed: %s: %s" % (config_file, e.strerror))
1555
+ sys.exit(1)
1556
+
1557
+ def process_patterns_from_file(fname, patterns_list):
1558
+ try:
1559
+ fn = open(fname, "rt")
1560
+ except IOError, e:
1561
+ error(e)
1562
+ sys.exit(1)
1563
+ for pattern in fn:
1564
+ pattern = pattern.strip()
1565
+ if re.match("^#", pattern) or re.match("^\s*$", pattern):
1566
+ continue
1567
+ debug(u"%s: adding rule: %s" % (fname, pattern))
1568
+ patterns_list.append(pattern)
1569
+
1570
+ return patterns_list
1571
+
1572
+ def process_patterns(patterns_list, patterns_from, is_glob, option_txt = ""):
1573
+ """
1574
+ process_patterns(patterns, patterns_from, is_glob, option_txt = "")
1575
+ Process --exclude / --include GLOB and REGEXP patterns.
1576
+ 'option_txt' is 'exclude' / 'include' / 'rexclude' / 'rinclude'
1577
+ Returns: patterns_compiled, patterns_text
1578
+ """
1579
+
1580
+ patterns_compiled = []
1581
+ patterns_textual = {}
1582
+
1583
+ if patterns_list is None:
1584
+ patterns_list = []
1585
+
1586
+ if patterns_from:
1587
+ ## Append patterns from glob_from
1588
+ for fname in patterns_from:
1589
+ debug(u"processing --%s-from %s" % (option_txt, fname))
1590
+ patterns_list = process_patterns_from_file(fname, patterns_list)
1591
+
1592
+ for pattern in patterns_list:
1593
+ debug(u"processing %s rule: %s" % (option_txt, patterns_list))
1594
+ if is_glob:
1595
+ pattern = glob.fnmatch.translate(pattern)
1596
+ r = re.compile(pattern)
1597
+ patterns_compiled.append(r)
1598
+ patterns_textual[r] = pattern
1599
+
1600
+ return patterns_compiled, patterns_textual
1601
+
1602
+ def get_commands_list():
1603
+ return [
1604
+ {"cmd":"mb", "label":"Make bucket", "param":"s3://BUCKET", "func":cmd_bucket_create, "argc":1},
1605
+ {"cmd":"rb", "label":"Remove bucket", "param":"s3://BUCKET", "func":cmd_bucket_delete, "argc":1},
1606
+ {"cmd":"ls", "label":"List objects or buckets", "param":"[s3://BUCKET[/PREFIX]]", "func":cmd_ls, "argc":0},
1607
+ {"cmd":"la", "label":"List all object in all buckets", "param":"", "func":cmd_buckets_list_all_all, "argc":0},
1608
+ {"cmd":"put", "label":"Put file into bucket", "param":"FILE [FILE...] s3://BUCKET[/PREFIX]", "func":cmd_object_put, "argc":2},
1609
+ {"cmd":"get", "label":"Get file from bucket", "param":"s3://BUCKET/OBJECT LOCAL_FILE", "func":cmd_object_get, "argc":1},
1610
+ {"cmd":"del", "label":"Delete file from bucket", "param":"s3://BUCKET/OBJECT", "func":cmd_object_del, "argc":1},
1611
+ #{"cmd":"mkdir", "label":"Make a virtual S3 directory", "param":"s3://BUCKET/path/to/dir", "func":cmd_mkdir, "argc":1},
1612
+ {"cmd":"sync", "label":"Synchronize a directory tree to S3", "param":"LOCAL_DIR s3://BUCKET[/PREFIX] or s3://BUCKET[/PREFIX] LOCAL_DIR", "func":cmd_sync, "argc":2},
1613
+ {"cmd":"du", "label":"Disk usage by buckets", "param":"[s3://BUCKET[/PREFIX]]", "func":cmd_du, "argc":0},
1614
+ {"cmd":"info", "label":"Get various information about Buckets or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_info, "argc":1},
1615
+ {"cmd":"cp", "label":"Copy object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_cp, "argc":2},
1616
+ {"cmd":"mv", "label":"Move object", "param":"s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]", "func":cmd_mv, "argc":2},
1617
+ {"cmd":"setacl", "label":"Modify Access control list for Bucket or Files", "param":"s3://BUCKET[/OBJECT]", "func":cmd_setacl, "argc":1},
1618
+
1619
+ {"cmd":"setpolicy", "label":"Modify Bucket Policy", "param":"FILE s3://BUCKET", "func":cmd_setpolicy, "argc":2},
1620
+ {"cmd":"delpolicy", "label":"Delete Bucket Policy", "param":"s3://BUCKET", "func":cmd_delpolicy, "argc":1},
1621
+
1622
+ {"cmd":"accesslog", "label":"Enable/disable bucket access logging", "param":"s3://BUCKET", "func":cmd_accesslog, "argc":1},
1623
+ {"cmd":"sign", "label":"Sign arbitrary string using the secret key", "param":"STRING-TO-SIGN", "func":cmd_sign, "argc":1},
1624
+ {"cmd":"signurl", "label":"Sign an S3 URL to provide limited public access with expiry", "param":"s3://BUCKET/OBJECT expiry_epoch", "func":cmd_signurl, "argc":2},
1625
+ {"cmd":"fixbucket", "label":"Fix invalid file names in a bucket", "param":"s3://BUCKET[/PREFIX]", "func":cmd_fixbucket, "argc":1},
1626
+
1627
+ ## Website commands
1628
+ {"cmd":"ws-create", "label":"Create Website from bucket", "param":"s3://BUCKET", "func":cmd_website_create, "argc":1},
1629
+ {"cmd":"ws-delete", "label":"Delete Website", "param":"s3://BUCKET", "func":cmd_website_delete, "argc":1},
1630
+ {"cmd":"ws-info", "label":"Info about Website", "param":"s3://BUCKET", "func":cmd_website_info, "argc":1},
1631
+
1632
+ ## CloudFront commands
1633
+ {"cmd":"cflist", "label":"List CloudFront distribution points", "param":"", "func":CfCmd.info, "argc":0},
1634
+ {"cmd":"cfinfo", "label":"Display CloudFront distribution point parameters", "param":"[cf://DIST_ID]", "func":CfCmd.info, "argc":0},
1635
+ {"cmd":"cfcreate", "label":"Create CloudFront distribution point", "param":"s3://BUCKET", "func":CfCmd.create, "argc":1},
1636
+ {"cmd":"cfdelete", "label":"Delete CloudFront distribution point", "param":"cf://DIST_ID", "func":CfCmd.delete, "argc":1},
1637
+ {"cmd":"cfmodify", "label":"Change CloudFront distribution point parameters", "param":"cf://DIST_ID", "func":CfCmd.modify, "argc":1},
1638
+ #{"cmd":"cfinval", "label":"Invalidate CloudFront objects", "param":"s3://BUCKET/OBJECT [s3://BUCKET/OBJECT ...]", "func":CfCmd.invalidate, "argc":1},
1639
+ {"cmd":"cfinvalinfo", "label":"Display CloudFront invalidation request(s) status", "param":"cf://DIST_ID[/INVAL_ID]", "func":CfCmd.invalinfo, "argc":1},
1640
+ ]
1641
+
1642
+ def format_commands(progname, commands_list):
1643
+ help = "Commands:\n"
1644
+ for cmd in commands_list:
1645
+ help += " %s\n %s %s %s\n" % (cmd["label"], progname, cmd["cmd"], cmd["param"])
1646
+ return help
1647
+
1648
+
1649
+ def update_acl(s3, uri, seq_label=""):
1650
+ something_changed = False
1651
+ acl = s3.get_acl(uri)
1652
+ debug(u"acl: %s - %r" % (uri, acl.grantees))
1653
+ if cfg.acl_public == True:
1654
+ if acl.isAnonRead():
1655
+ info(u"%s: already Public, skipping %s" % (uri, seq_label))
1656
+ else:
1657
+ acl.grantAnonRead()
1658
+ something_changed = True
1659
+ elif cfg.acl_public == False: # we explicitely check for False, because it could be None
1660
+ if not acl.isAnonRead():
1661
+ info(u"%s: already Private, skipping %s" % (uri, seq_label))
1662
+ else:
1663
+ acl.revokeAnonRead()
1664
+ something_changed = True
1665
+
1666
+ # update acl with arguments
1667
+ # grant first and revoke later, because revoke has priority
1668
+ if cfg.acl_grants:
1669
+ something_changed = True
1670
+ for grant in cfg.acl_grants:
1671
+ acl.grant(**grant)
1672
+
1673
+ if cfg.acl_revokes:
1674
+ something_changed = True
1675
+ for revoke in cfg.acl_revokes:
1676
+ acl.revoke(**revoke)
1677
+
1678
+ if not something_changed:
1679
+ return
1680
+
1681
+ retsponse = s3.set_acl(uri, acl)
1682
+ if retsponse['status'] == 200:
1683
+ if cfg.acl_public in (True, False):
1684
+ set_to_acl = cfg.acl_public and "Public" or "Private"
1685
+ output(u"%s: ACL set to %s %s" % (uri, set_to_acl, seq_label))
1686
+ else:
1687
+ output(u"%s: ACL updated" % uri)
1688
+
1689
+ class OptionMimeType(Option):
1690
+ def check_mimetype(option, opt, value):
1691
+ if re.compile("^[a-z0-9]+/[a-z0-9+\.-]+(;.*)?$", re.IGNORECASE).match(value):
1692
+ return value
1693
+ raise OptionValueError("option %s: invalid MIME-Type format: %r" % (opt, value))
1694
+
1695
+ class OptionS3ACL(Option):
1696
+ def check_s3acl(option, opt, value):
1697
+ permissions = ('read', 'write', 'read_acp', 'write_acp', 'full_control', 'all')
1698
+ try:
1699
+ permission, grantee = re.compile("^(\w+):(.+)$", re.IGNORECASE).match(value).groups()
1700
+ if not permission or not grantee:
1701
+ raise
1702
+ if permission in permissions:
1703
+ return { 'name' : grantee, 'permission' : permission.upper() }
1704
+ else:
1705
+ raise OptionValueError("option %s: invalid S3 ACL permission: %s (valid values: %s)" %
1706
+ (opt, permission, ", ".join(permissions)))
1707
+ except:
1708
+ raise OptionValueError("option %s: invalid S3 ACL format: %r" % (opt, value))
1709
+
1710
+ class OptionAll(OptionMimeType, OptionS3ACL):
1711
+ TYPE_CHECKER = copy(Option.TYPE_CHECKER)
1712
+ TYPE_CHECKER["mimetype"] = OptionMimeType.check_mimetype
1713
+ TYPE_CHECKER["s3acl"] = OptionS3ACL.check_s3acl
1714
+ TYPES = Option.TYPES + ("mimetype", "s3acl")
1715
+
1716
+ class MyHelpFormatter(IndentedHelpFormatter):
1717
+ def format_epilog(self, epilog):
1718
+ if epilog:
1719
+ return "\n" + epilog + "\n"
1720
+ else:
1721
+ return ""
1722
+
1723
+ def main():
1724
+ global cfg
1725
+
1726
+ commands_list = get_commands_list()
1727
+ commands = {}
1728
+
1729
+ ## Populate "commands" from "commands_list"
1730
+ for cmd in commands_list:
1731
+ if cmd.has_key("cmd"):
1732
+ commands[cmd["cmd"]] = cmd
1733
+
1734
+ default_verbosity = Config().verbosity
1735
+ optparser = OptionParser(option_class=OptionAll, formatter=MyHelpFormatter())
1736
+ #optparser.disable_interspersed_args()
1737
+
1738
+ config_file = None
1739
+ if os.getenv("HOME"):
1740
+ config_file = os.path.join(os.getenv("HOME"), ".s3cfg")
1741
+ elif os.name == "nt" and os.getenv("USERPROFILE"):
1742
+ config_file = os.path.join(os.getenv("USERPROFILE").decode('mbcs'), "Application Data", "s3cmd.ini")
1743
+
1744
+ preferred_encoding = locale.getpreferredencoding() or "UTF-8"
1745
+
1746
+ optparser.set_defaults(encoding = preferred_encoding)
1747
+ optparser.set_defaults(config = config_file)
1748
+ optparser.set_defaults(verbosity = default_verbosity)
1749
+
1750
+ optparser.add_option( "--configure", dest="run_configure", action="store_true", help="Invoke interactive (re)configuration tool. Optionally use as '--configure s3://come-bucket' to test access to a specific bucket instead of attempting to list them all.")
1751
+ optparser.add_option("-c", "--config", dest="config", metavar="FILE", help="Config file name. Defaults to %default")
1752
+ optparser.add_option( "--dump-config", dest="dump_config", action="store_true", help="Dump current configuration after parsing config files and command line options and exit.")
1753
+ optparser.add_option( "--access_key", dest="access_key", help="AWS Access Key")
1754
+ optparser.add_option( "--secret_key", dest="secret_key", help="AWS Secret Key")
1755
+
1756
+ optparser.add_option("-n", "--dry-run", dest="dry_run", action="store_true", help="Only show what should be uploaded or downloaded but don't actually do it. May still perform S3 requests to get bucket listings and other information though (only for file transfer commands)")
1757
+
1758
+ optparser.add_option("-e", "--encrypt", dest="encrypt", action="store_true", help="Encrypt files before uploading to S3.")
1759
+ optparser.add_option( "--no-encrypt", dest="encrypt", action="store_false", help="Don't encrypt files.")
1760
+ optparser.add_option("-f", "--force", dest="force", action="store_true", help="Force overwrite and other dangerous operations.")
1761
+ optparser.add_option( "--continue", dest="get_continue", action="store_true", help="Continue getting a partially downloaded file (only for [get] command).")
1762
+ optparser.add_option( "--skip-existing", dest="skip_existing", action="store_true", help="Skip over files that exist at the destination (only for [get] and [sync] commands).")
1763
+ optparser.add_option("-r", "--recursive", dest="recursive", action="store_true", help="Recursive upload, download or removal.")
1764
+ optparser.add_option( "--check-md5", dest="check_md5", action="store_true", help="Check MD5 sums when comparing files for [sync]. (default)")
1765
+ optparser.add_option( "--no-check-md5", dest="check_md5", action="store_false", help="Do not check MD5 sums when comparing files for [sync]. Only size will be compared. May significantly speed up transfer but may also miss some changed files.")
1766
+ optparser.add_option("-P", "--acl-public", dest="acl_public", action="store_true", help="Store objects with ACL allowing read for anyone.")
1767
+ optparser.add_option( "--acl-private", dest="acl_public", action="store_false", help="Store objects with default ACL allowing access for you only.")
1768
+ optparser.add_option( "--acl-grant", dest="acl_grants", type="s3acl", action="append", metavar="PERMISSION:EMAIL or USER_CANONICAL_ID", help="Grant stated permission to a given amazon user. Permission is one of: read, write, read_acp, write_acp, full_control, all")
1769
+ optparser.add_option( "--acl-revoke", dest="acl_revokes", type="s3acl", action="append", metavar="PERMISSION:USER_CANONICAL_ID", help="Revoke stated permission for a given amazon user. Permission is one of: read, write, read_acp, wr ite_acp, full_control, all")
1770
+
1771
+ optparser.add_option( "--delete-removed", dest="delete_removed", action="store_true", help="Delete remote objects with no corresponding local file [sync]")
1772
+ optparser.add_option( "--no-delete-removed", dest="delete_removed", action="store_false", help="Don't delete remote objects.")
1773
+ optparser.add_option( "--delete-after", dest="delete_after", action="store_true", help="Perform deletes after new uploads [sync]")
1774
+ optparser.add_option( "--delay-updates", dest="delay_updates", action="store_true", help="Put all updated files into place at end [sync]")
1775
+ optparser.add_option( "--add-destination", dest="additional_destinations", action="append", help="Additional destination for parallel uploads, in addition to last arg. May be repeated.")
1776
+ optparser.add_option( "--delete-after-fetch", dest="delete_after_fetch", action="store_true", help="Delete remote objects after fetching to local file (only for [get] and [sync] commands).")
1777
+ optparser.add_option("-p", "--preserve", dest="preserve_attrs", action="store_true", help="Preserve filesystem attributes (mode, ownership, timestamps). Default for [sync] command.")
1778
+ optparser.add_option( "--no-preserve", dest="preserve_attrs", action="store_false", help="Don't store FS attributes")
1779
+ optparser.add_option( "--exclude", dest="exclude", action="append", metavar="GLOB", help="Filenames and paths matching GLOB will be excluded from sync")
1780
+ optparser.add_option( "--exclude-from", dest="exclude_from", action="append", metavar="FILE", help="Read --exclude GLOBs from FILE")
1781
+ optparser.add_option( "--rexclude", dest="rexclude", action="append", metavar="REGEXP", help="Filenames and paths matching REGEXP (regular expression) will be excluded from sync")
1782
+ optparser.add_option( "--rexclude-from", dest="rexclude_from", action="append", metavar="FILE", help="Read --rexclude REGEXPs from FILE")
1783
+ optparser.add_option( "--include", dest="include", action="append", metavar="GLOB", help="Filenames and paths matching GLOB will be included even if previously excluded by one of --(r)exclude(-from) patterns")
1784
+ optparser.add_option( "--include-from", dest="include_from", action="append", metavar="FILE", help="Read --include GLOBs from FILE")
1785
+ optparser.add_option( "--rinclude", dest="rinclude", action="append", metavar="REGEXP", help="Same as --include but uses REGEXP (regular expression) instead of GLOB")
1786
+ optparser.add_option( "--rinclude-from", dest="rinclude_from", action="append", metavar="FILE", help="Read --rinclude REGEXPs from FILE")
1787
+
1788
+ optparser.add_option( "--bucket-location", dest="bucket_location", help="Datacentre to create bucket in. As of now the datacenters are: US (default), EU, ap-northeast-1, ap-southeast-1, sa-east-1, us-west-1 and us-west-2")
1789
+ optparser.add_option( "--reduced-redundancy", "--rr", dest="reduced_redundancy", action="store_true", help="Store object with 'Reduced redundancy'. Lower per-GB price. [put, cp, mv]")
1790
+
1791
+ optparser.add_option( "--access-logging-target-prefix", dest="log_target_prefix", help="Target prefix for access logs (S3 URI) (for [cfmodify] and [accesslog] commands)")
1792
+ optparser.add_option( "--no-access-logging", dest="log_target_prefix", action="store_false", help="Disable access logging (for [cfmodify] and [accesslog] commands)")
1793
+
1794
+ optparser.add_option( "--default-mime-type", dest="default_mime_type", action="store_true", help="Default MIME-type for stored objects. Application default is binary/octet-stream.")
1795
+ optparser.add_option("-M", "--guess-mime-type", dest="guess_mime_type", action="store_true", help="Guess MIME-type of files by their extension or mime magic. Fall back to default MIME-Type as specified by --default-mime-type option")
1796
+ optparser.add_option( "--no-guess-mime-type", dest="guess_mime_type", action="store_false", help="Don't guess MIME-type and use the default type instead.")
1797
+ optparser.add_option("-m", "--mime-type", dest="mime_type", type="mimetype", metavar="MIME/TYPE", help="Force MIME-type. Override both --default-mime-type and --guess-mime-type.")
1798
+
1799
+ optparser.add_option( "--add-header", dest="add_header", action="append", metavar="NAME:VALUE", help="Add a given HTTP header to the upload request. Can be used multiple times. For instance set 'Expires' or 'Cache-Control' headers (or both) using this options if you like.")
1800
+
1801
+ optparser.add_option( "--encoding", dest="encoding", metavar="ENCODING", help="Override autodetected terminal and filesystem encoding (character set). Autodetected: %s" % preferred_encoding)
1802
+ optparser.add_option( "--add-encoding-exts", dest="add_encoding_exts", metavar="EXTENSIONs", help="Add encoding to these comma delimited extensions i.e. (css,js,html) when uploading to S3 )")
1803
+ optparser.add_option( "--verbatim", dest="urlencoding_mode", action="store_const", const="verbatim", help="Use the S3 name as given on the command line. No pre-processing, encoding, etc. Use with caution!")
1804
+
1805
+ optparser.add_option( "--disable-multipart", dest="enable_multipart", action="store_false", help="Disable multipart upload on files bigger than --multipart-chunk-size-mb")
1806
+ optparser.add_option( "--multipart-chunk-size-mb", dest="multipart_chunk_size_mb", type="int", action="store", metavar="SIZE", help="Size of each chunk of a multipart upload. Files bigger than SIZE are automatically uploaded as multithreaded-multipart, smaller files are uploaded using the traditional method. SIZE is in Mega-Bytes, default chunk size is %defaultMB, minimum allowed chunk size is 5MB, maximum is 5GB.")
1807
+
1808
+ optparser.add_option( "--list-md5", dest="list_md5", action="store_true", help="Include MD5 sums in bucket listings (only for 'ls' command).")
1809
+ optparser.add_option("-H", "--human-readable-sizes", dest="human_readable_sizes", action="store_true", help="Print sizes in human readable form (eg 1kB instead of 1234).")
1810
+
1811
+ optparser.add_option( "--ws-index", dest="website_index", action="store", help="Name of error-document (only for [ws-create] command)")
1812
+ optparser.add_option( "--ws-error", dest="website_error", action="store", help="Name of index-document (only for [ws-create] command)")
1813
+
1814
+ optparser.add_option( "--progress", dest="progress_meter", action="store_true", help="Display progress meter (default on TTY).")
1815
+ optparser.add_option( "--no-progress", dest="progress_meter", action="store_false", help="Don't display progress meter (default on non-TTY).")
1816
+ optparser.add_option( "--enable", dest="enable", action="store_true", help="Enable given CloudFront distribution (only for [cfmodify] command)")
1817
+ optparser.add_option( "--disable", dest="enable", action="store_false", help="Enable given CloudFront distribution (only for [cfmodify] command)")
1818
+ optparser.add_option( "--cf-invalidate", dest="invalidate_on_cf", action="store_true", help="Invalidate the uploaded filed in CloudFront. Also see [cfinval] command.")
1819
+ # joseprio: adding options to invalidate the default index and the default
1820
+ # index root
1821
+ optparser.add_option( "--cf-invalidate-default-index", dest="invalidate_default_index_on_cf", action="store_true", help="When using Custom Origin and S3 static website, invalidate the default index file.")
1822
+ optparser.add_option( "--cf-no-invalidate-default-index-root", dest="invalidate_default_index_root_on_cf", action="store_false", help="When using Custom Origin and S3 static website, don't invalidate the path to the default index file.")
1823
+ optparser.add_option( "--cf-add-cname", dest="cf_cnames_add", action="append", metavar="CNAME", help="Add given CNAME to a CloudFront distribution (only for [cfcreate] and [cfmodify] commands)")
1824
+ optparser.add_option( "--cf-remove-cname", dest="cf_cnames_remove", action="append", metavar="CNAME", help="Remove given CNAME from a CloudFront distribution (only for [cfmodify] command)")
1825
+ optparser.add_option( "--cf-comment", dest="cf_comment", action="store", metavar="COMMENT", help="Set COMMENT for a given CloudFront distribution (only for [cfcreate] and [cfmodify] commands)")
1826
+ optparser.add_option( "--cf-default-root-object", dest="cf_default_root_object", action="store", metavar="DEFAULT_ROOT_OBJECT", help="Set the default root object to return when no object is specified in the URL. Use a relative path, i.e. default/index.html instead of /default/index.html or s3://bucket/default/index.html (only for [cfcreate] and [cfmodify] commands)")
1827
+ optparser.add_option("-v", "--verbose", dest="verbosity", action="store_const", const=logging.INFO, help="Enable verbose output.")
1828
+ optparser.add_option("-d", "--debug", dest="verbosity", action="store_const", const=logging.DEBUG, help="Enable debug output.")
1829
+ optparser.add_option( "--version", dest="show_version", action="store_true", help="Show s3cmd version (%s) and exit." % (PkgInfo.version))
1830
+ optparser.add_option("-F", "--follow-symlinks", dest="follow_symlinks", action="store_true", default=False, help="Follow symbolic links as if they are regular files")
1831
+ optparser.add_option( "--cache-file", dest="cache_file", action="store", default="", metavar="FILE", help="Cache FILE containing local source MD5 values")
1832
+ optparser.add_option("-q", "--quiet", dest="quiet", action="store_true", default=False, help="Silence output on stdout")
1833
+
1834
+ optparser.set_usage(optparser.usage + " COMMAND [parameters]")
1835
+ optparser.set_description('S3cmd is a tool for managing objects in '+
1836
+ 'Amazon S3 storage. It allows for making and removing '+
1837
+ '"buckets" and uploading, downloading and removing '+
1838
+ '"objects" from these buckets.')
1839
+ optparser.epilog = format_commands(optparser.get_prog_name(), commands_list)
1840
+ optparser.epilog += ("\nFor more informations see the progect homepage:\n%s\n" % PkgInfo.url)
1841
+ optparser.epilog += ("\nConsider a donation if you have found s3cmd useful:\n%s/donate\n" % PkgInfo.url)
1842
+
1843
+ (options, args) = optparser.parse_args()
1844
+
1845
+ ## Some mucking with logging levels to enable
1846
+ ## debugging/verbose output for config file parser on request
1847
+ logging.basicConfig(level=options.verbosity,
1848
+ format='%(levelname)s: %(message)s',
1849
+ stream = sys.stderr)
1850
+
1851
+ if options.show_version:
1852
+ output(u"s3cmd version %s" % PkgInfo.version)
1853
+ sys.exit(0)
1854
+
1855
+ if options.quiet:
1856
+ try:
1857
+ f = open("/dev/null", "w")
1858
+ sys.stdout.close()
1859
+ sys.stdout = f
1860
+ except IOError:
1861
+ warning(u"Unable to open /dev/null: --quiet disabled.")
1862
+
1863
+ ## Now finally parse the config file
1864
+ if not options.config:
1865
+ error(u"Can't find a config file. Please use --config option.")
1866
+ sys.exit(1)
1867
+
1868
+ try:
1869
+ cfg = Config(options.config)
1870
+ except IOError, e:
1871
+ if options.run_configure:
1872
+ cfg = Config()
1873
+ else:
1874
+ error(u"%s: %s" % (options.config, e.strerror))
1875
+ error(u"Configuration file not available.")
1876
+ error(u"Consider using --configure parameter to create one.")
1877
+ sys.exit(1)
1878
+
1879
+ ## And again some logging level adjustments
1880
+ ## according to configfile and command line parameters
1881
+ if options.verbosity != default_verbosity:
1882
+ cfg.verbosity = options.verbosity
1883
+ logging.root.setLevel(cfg.verbosity)
1884
+
1885
+ ## Default to --progress on TTY devices, --no-progress elsewhere
1886
+ ## Can be overriden by actual --(no-)progress parameter
1887
+ cfg.update_option('progress_meter', sys.stdout.isatty())
1888
+
1889
+ ## Unsupported features on Win32 platform
1890
+ if os.name == "nt":
1891
+ if cfg.preserve_attrs:
1892
+ error(u"Option --preserve is not yet supported on MS Windows platform. Assuming --no-preserve.")
1893
+ cfg.preserve_attrs = False
1894
+ if cfg.progress_meter:
1895
+ error(u"Option --progress is not yet supported on MS Windows platform. Assuming --no-progress.")
1896
+ cfg.progress_meter = False
1897
+
1898
+ ## Pre-process --add-header's and put them to Config.extra_headers SortedDict()
1899
+ if options.add_header:
1900
+ for hdr in options.add_header:
1901
+ try:
1902
+ key, val = hdr.split(":", 1)
1903
+ except ValueError:
1904
+ raise ParameterError("Invalid header format: %s" % hdr)
1905
+ key_inval = re.sub("[a-zA-Z0-9-.]", "", key)
1906
+ if key_inval:
1907
+ key_inval = key_inval.replace(" ", "<space>")
1908
+ key_inval = key_inval.replace("\t", "<tab>")
1909
+ raise ParameterError("Invalid character(s) in header name '%s': \"%s\"" % (key, key_inval))
1910
+ debug(u"Updating Config.Config extra_headers[%s] -> %s" % (key.strip(), val.strip()))
1911
+ cfg.extra_headers[key.strip()] = val.strip()
1912
+
1913
+ ## --acl-grant/--acl-revoke arguments are pre-parsed by OptionS3ACL()
1914
+ if options.acl_grants:
1915
+ for grant in options.acl_grants:
1916
+ cfg.acl_grants.append(grant)
1917
+
1918
+ if options.acl_revokes:
1919
+ for grant in options.acl_revokes:
1920
+ cfg.acl_revokes.append(grant)
1921
+
1922
+ ## Process --(no-)check-md5
1923
+ if options.check_md5 == False:
1924
+ try:
1925
+ cfg.sync_checks.remove("md5")
1926
+ except Exception:
1927
+ pass
1928
+ if options.check_md5 == True and cfg.sync_checks.count("md5") == 0:
1929
+ cfg.sync_checks.append("md5")
1930
+
1931
+ ## Update Config with other parameters
1932
+ for option in cfg.option_list():
1933
+ try:
1934
+ if getattr(options, option) != None:
1935
+ debug(u"Updating Config.Config %s -> %s" % (option, getattr(options, option)))
1936
+ cfg.update_option(option, getattr(options, option))
1937
+ except AttributeError:
1938
+ ## Some Config() options are not settable from command line
1939
+ pass
1940
+
1941
+ ## Special handling for tri-state options (True, False, None)
1942
+ cfg.update_option("enable", options.enable)
1943
+ cfg.update_option("acl_public", options.acl_public)
1944
+
1945
+ ## Check multipart chunk constraints
1946
+ if cfg.multipart_chunk_size_mb < MultiPartUpload.MIN_CHUNK_SIZE_MB:
1947
+ raise ParameterError("Chunk size %d MB is too small, must be >= %d MB. Please adjust --multipart-chunk-size-mb" % (cfg.multipart_chunk_size_mb, MultiPartUpload.MIN_CHUNK_SIZE_MB))
1948
+ if cfg.multipart_chunk_size_mb > MultiPartUpload.MAX_CHUNK_SIZE_MB:
1949
+ raise ParameterError("Chunk size %d MB is too large, must be <= %d MB. Please adjust --multipart-chunk-size-mb" % (cfg.multipart_chunk_size_mb, MultiPartUpload.MAX_CHUNK_SIZE_MB))
1950
+
1951
+ ## CloudFront's cf_enable and Config's enable share the same --enable switch
1952
+ options.cf_enable = options.enable
1953
+
1954
+ ## CloudFront's cf_logging and Config's log_target_prefix share the same --log-target-prefix switch
1955
+ options.cf_logging = options.log_target_prefix
1956
+
1957
+ ## Update CloudFront options if some were set
1958
+ for option in CfCmd.options.option_list():
1959
+ try:
1960
+ if getattr(options, option) != None:
1961
+ debug(u"Updating CloudFront.Cmd %s -> %s" % (option, getattr(options, option)))
1962
+ CfCmd.options.update_option(option, getattr(options, option))
1963
+ except AttributeError:
1964
+ ## Some CloudFront.Cmd.Options() options are not settable from command line
1965
+ pass
1966
+
1967
+ if options.additional_destinations:
1968
+ cfg.additional_destinations = options.additional_destinations
1969
+
1970
+ ## Set output and filesystem encoding for printing out filenames.
1971
+ sys.stdout = codecs.getwriter(cfg.encoding)(sys.stdout, "replace")
1972
+ sys.stderr = codecs.getwriter(cfg.encoding)(sys.stderr, "replace")
1973
+
1974
+ ## Process --exclude and --exclude-from
1975
+ patterns_list, patterns_textual = process_patterns(options.exclude, options.exclude_from, is_glob = True, option_txt = "exclude")
1976
+ cfg.exclude.extend(patterns_list)
1977
+ cfg.debug_exclude.update(patterns_textual)
1978
+
1979
+ ## Process --rexclude and --rexclude-from
1980
+ patterns_list, patterns_textual = process_patterns(options.rexclude, options.rexclude_from, is_glob = False, option_txt = "rexclude")
1981
+ cfg.exclude.extend(patterns_list)
1982
+ cfg.debug_exclude.update(patterns_textual)
1983
+
1984
+ ## Process --include and --include-from
1985
+ patterns_list, patterns_textual = process_patterns(options.include, options.include_from, is_glob = True, option_txt = "include")
1986
+ cfg.include.extend(patterns_list)
1987
+ cfg.debug_include.update(patterns_textual)
1988
+
1989
+ ## Process --rinclude and --rinclude-from
1990
+ patterns_list, patterns_textual = process_patterns(options.rinclude, options.rinclude_from, is_glob = False, option_txt = "rinclude")
1991
+ cfg.include.extend(patterns_list)
1992
+ cfg.debug_include.update(patterns_textual)
1993
+
1994
+ ## Set socket read()/write() timeout
1995
+ socket.setdefaulttimeout(cfg.socket_timeout)
1996
+
1997
+ if cfg.encrypt and cfg.gpg_passphrase == "":
1998
+ error(u"Encryption requested but no passphrase set in config file.")
1999
+ error(u"Please re-run 's3cmd --configure' and supply it.")
2000
+ sys.exit(1)
2001
+
2002
+ if options.dump_config:
2003
+ cfg.dump_config(sys.stdout)
2004
+ sys.exit(0)
2005
+
2006
+ if options.run_configure:
2007
+ # 'args' may contain the test-bucket URI
2008
+ run_configure(options.config, args)
2009
+ sys.exit(0)
2010
+
2011
+ if len(args) < 1:
2012
+ error(u"Missing command. Please run with --help for more information.")
2013
+ sys.exit(1)
2014
+
2015
+ ## Unicodise all remaining arguments:
2016
+ args = [unicodise(arg) for arg in args]
2017
+
2018
+ command = args.pop(0)
2019
+ try:
2020
+ debug(u"Command: %s" % commands[command]["cmd"])
2021
+ ## We must do this lookup in extra step to
2022
+ ## avoid catching all KeyError exceptions
2023
+ ## from inner functions.
2024
+ cmd_func = commands[command]["func"]
2025
+ except KeyError, e:
2026
+ error(u"Invalid command: %s" % e)
2027
+ sys.exit(1)
2028
+
2029
+ if len(args) < commands[command]["argc"]:
2030
+ error(u"Not enough parameters for command '%s'" % command)
2031
+ sys.exit(1)
2032
+
2033
+ try:
2034
+ cmd_func(args)
2035
+ except S3Error, e:
2036
+ error(u"S3 error: %s" % e)
2037
+ sys.exit(1)
2038
+
2039
+ def report_exception(e):
2040
+ sys.stderr.write("""
2041
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2042
+ An unexpected error has occurred.
2043
+ Please report the following lines to:
2044
+ s3tools-bugs@lists.sourceforge.net
2045
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2046
+
2047
+ """)
2048
+ tb = traceback.format_exc(sys.exc_info())
2049
+ e_class = str(e.__class__)
2050
+ e_class = e_class[e_class.rfind(".")+1 : -2]
2051
+ sys.stderr.write(u"Problem: %s: %s\n" % (e_class, e))
2052
+ try:
2053
+ sys.stderr.write("S3cmd: %s\n" % PkgInfo.version)
2054
+ except NameError:
2055
+ sys.stderr.write("S3cmd: unknown version. Module import problem?\n")
2056
+ sys.stderr.write("\n")
2057
+ sys.stderr.write(unicode(tb, errors="replace"))
2058
+
2059
+ if type(e) == ImportError:
2060
+ sys.stderr.write("\n")
2061
+ sys.stderr.write("Your sys.path contains these entries:\n")
2062
+ for path in sys.path:
2063
+ sys.stderr.write(u"\t%s\n" % path)
2064
+ sys.stderr.write("Now the question is where have the s3cmd modules been installed?\n")
2065
+
2066
+ sys.stderr.write("""
2067
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2068
+ An unexpected error has occurred.
2069
+ Please report the above lines to:
2070
+ s3tools-bugs@lists.sourceforge.net
2071
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2072
+ """)
2073
+
2074
+ if __name__ == '__main__':
2075
+ try:
2076
+ ## Our modules
2077
+ ## Keep them in try/except block to
2078
+ ## detect any syntax errors in there
2079
+ from S3.Exceptions import *
2080
+ from S3 import PkgInfo
2081
+ from S3.S3 import S3
2082
+ from S3.Config import Config
2083
+ from S3.SortedDict import SortedDict
2084
+ from S3.FileDict import FileDict
2085
+ from S3.S3Uri import S3Uri
2086
+ from S3 import Utils
2087
+ from S3.Utils import *
2088
+ from S3.Progress import Progress
2089
+ from S3.CloudFront import Cmd as CfCmd
2090
+ from S3.CloudFront import CloudFront
2091
+ from S3.FileLists import *
2092
+ from S3.MultiPart import MultiPartUpload
2093
+
2094
+ main()
2095
+ sys.exit(0)
2096
+
2097
+ except ImportError, e:
2098
+ report_exception(e)
2099
+ sys.exit(1)
2100
+
2101
+ except ParameterError, e:
2102
+ error(u"Parameter problem: %s" % e)
2103
+ sys.exit(1)
2104
+
2105
+ except SystemExit, e:
2106
+ sys.exit(e.code)
2107
+
2108
+ except KeyboardInterrupt:
2109
+ sys.stderr.write("See ya!\n")
2110
+ sys.exit(1)
2111
+
2112
+ except Exception, e:
2113
+ report_exception(e)
2114
+ sys.exit(1)
2115
+
2116
+ # vim:et:ts=4:sts=4:ai