Browse code

Renamed s3.py to s3cmd (take 2)

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3py/trunk@47 830e0280-6d2a-0410-9c65-932aecc39d9d

Michal Ludvig authored on 2007/01/13 22:21:44
Showing 2 changed files
1 1
deleted file mode 100755
... ...
@@ -1,224 +0,0 @@
1
-#!/usr/bin/env 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
-import logging
10
-import time
11
-
12
-from optparse import OptionParser
13
-from logging import debug, info, warning, error
14
-import elementtree.ElementTree as ET
15
-
16
-## Our modules
17
-from S3.S3 import *
18
-
19
-def output(message):
20
-	print message
21
-
22
-def cmd_buckets_list_all(args):
23
-	s3 = S3(AwsConfig())
24
-	response = s3.list_all_buckets()
25
-
26
-	for bucket in response["list"]:
27
-		output("%s  %s" % (
28
-			formatDateTime(bucket["CreationDate"]),
29
-			s3.compose_uri(bucket["Name"]),
30
-			))
31
-
32
-def cmd_buckets_list_all_all(args):
33
-	s3 = S3(AwsConfig())
34
-	response = s3.list_all_buckets()
35
-
36
-	for bucket in response["list"]:
37
-		cmd_bucket_list([bucket["Name"]])
38
-		output("")
39
-
40
-
41
-def cmd_bucket_list(args):
42
-	s3 = S3(AwsConfig())
43
-	isuri, bucket, object = s3.parse_s3_uri(args[0])
44
-	if not isuri:
45
-		bucket = args[0]
46
-	output("Bucket '%s':" % bucket)
47
-	try:
48
-		response = s3.bucket_list(bucket)
49
-	except S3Error, e:
50
-		if S3.codes.has_key(e.Code):
51
-			error(S3.codes[e.Code] % bucket)
52
-			return
53
-		else:
54
-			raise
55
-	for object in response["list"]:
56
-		size, size_coeff = formatSize(object["Size"], AwsConfig.human_readable_sizes)
57
-		output("%s  %s%s  %s" % (
58
-			formatDateTime(object["LastModified"]),
59
-			str(size).rjust(8), size_coeff.ljust(1),
60
-			s3.compose_uri(bucket, object["Key"]),
61
-			))
62
-
63
-def cmd_bucket_create(args):
64
-	s3 = S3(AwsConfig())
65
-	isuri, bucket, object = s3.parse_s3_uri(args[0])
66
-	if not isuri:
67
-		bucket = args[0]
68
-	try:
69
-		response = s3.bucket_create(bucket)
70
-	except S3Error, e:
71
-		if S3.codes.has_key(e.Code):
72
-			error(S3.codes[e.Code] % bucket)
73
-			return
74
-		else:
75
-			raise
76
-	output("Bucket '%s' created" % bucket)
77
-
78
-def cmd_bucket_delete(args):
79
-	s3 = S3(AwsConfig())
80
-	isuri, bucket, object = s3.parse_s3_uri(args[0])
81
-	if not isuri:
82
-		bucket = args[0]
83
-	try:
84
-		response = s3.bucket_delete(bucket)
85
-	except S3Error, e:
86
-		if S3.codes.has_key(e.Code):
87
-			error(S3.codes[e.Code] % bucket)
88
-			return
89
-		else:
90
-			raise
91
-	output("Bucket '%s' removed" % bucket)
92
-
93
-def cmd_object_put(args):
94
-	s3 = S3(AwsConfig())
95
-
96
-	s3uri = args.pop()
97
-	files = args[:]
98
-
99
-	isuri, bucket, object = s3.parse_s3_uri(s3uri)
100
-	if not isuri:
101
-		raise ParameterError("Expecting S3 URI instead of '%s'" % s3uri)
102
-
103
-	if len(files) > 1 and object != "" and not AwsConfig.force:
104
-		error("When uploading multiple files the last argument must")
105
-		error("be a S3 URI specifying just the bucket name")
106
-		error("WITHOUT object name!")
107
-		error("Alternatively use --force argument and the specified")
108
-		error("object name will be prefixed to all stored filanames.")
109
-		exit(1)
110
-
111
-	for file in files:
112
-		if len(files) > 1:
113
-			object_final = object + os.path.basename(file)
114
-		elif object == "":
115
-			object_final = os.path.basename(file)
116
-		else:
117
-			object_final = object
118
-		response = s3.object_put(file, bucket, object_final)
119
-		output("File '%s' stored as %s (%d bytes)" %
120
-			(file, s3.compose_uri(bucket, object_final), response["size"]))
121
-
122
-def cmd_object_get(args):
123
-	s3 = S3(AwsConfig())
124
-	s3uri = args.pop(0)
125
-	isuri, bucket, object = s3.parse_s3_uri(s3uri)
126
-	if not isuri or not bucket or not object:
127
-		raise ParameterError("Expecting S3 object URI instead of '%s'" % s3uri)
128
-	destination = len(args) > 0 and args.pop(0) or object
129
-	if os.path.isdir(destination):
130
-		destination += ("/" + object)
131
-	if not AwsConfig.force and os.path.exists(destination):
132
-		raise ParameterError("File %s already exists. Use --force to overwrite it" % destination)
133
-	response = s3.object_get(destination, bucket, object)
134
-	output("Object %s saved as '%s' (%d bytes)" %
135
-		(s3uri, destination, response["size"]))
136
-
137
-def cmd_object_del(args):
138
-	s3 = S3(AwsConfig())
139
-	s3uri = args.pop(0)
140
-	isuri, bucket, object = s3.parse_s3_uri(s3uri)
141
-	if not isuri or not bucket or not object:
142
-		raise ParameterError("Expecting S3 object URI instead of '%s'" % s3uri)
143
-	response = s3.object_delete(bucket, object)
144
-	output("Object %s deleted" % s3uri)
145
-
146
-commands = {
147
-	"lb" : ("List all buckets", cmd_buckets_list_all, 0),
148
-	"cb" : ("Create bucket", cmd_bucket_create, 1),
149
-	"mb" : ("Create bucket", cmd_bucket_create, 1),
150
-	"rb" : ("Remove bucket", cmd_bucket_delete, 1),
151
-	"db" : ("Remove bucket", cmd_bucket_delete, 1),
152
-	"ls" : ("List objects in bucket", cmd_bucket_list, 1),
153
-	"la" : ("List all object in all buckets", cmd_buckets_list_all_all, 0),
154
-	"put": ("Put file into bucket", cmd_object_put, 2),
155
-	"get": ("Get file from bucket", cmd_object_get, 1),
156
-	"del": ("Delete file from bucket", cmd_object_del, 1),
157
-	}
158
-
159
-if __name__ == '__main__':
160
-	if float("%d.%d" %(sys.version_info[0], sys.version_info[1])) < 2.5:
161
-		sys.stderr.write("ERROR: Python 2.5 or higher required, sorry.\n")
162
-		exit(1)
163
-
164
-	default_verbosity = AwsConfig.verbosity
165
-	optparser = OptionParser()
166
-	optparser.set_defaults(config=os.getenv("HOME")+"/.s3cfg")
167
-	optparser.add_option("-c", "--config", dest="config", metavar="FILE", help="Config file name")
168
-	optparser.set_defaults(verbosity = default_verbosity)
169
-	optparser.add_option("-d", "--debug", dest="verbosity", action="store_const", const=logging.DEBUG, help="Enable debug output")
170
-	optparser.add_option("-v", "--verbose", dest="verbosity", action="store_const", const=logging.INFO, help="Enable verbose output")
171
-	optparser.set_defaults(human_readable = False)
172
-	optparser.add_option("-H", "--human-readable", dest="human_readable", action="store_true", help="Print sizes in human readable form")
173
-	optparser.set_defaults(force = False)
174
-	optparser.add_option("-f", "--force", dest="force", action="store_true", help="Force overwrite and other dangerous operations")
175
-	optparser.set_defaults(show_uri = False)
176
-	optparser.add_option("-u", "--show-uri", dest="show_uri", action="store_true", help="Show complete S3 URI in listings")
177
-
178
-	(options, args) = optparser.parse_args()
179
-
180
-	## Some mucking with logging levels to enable 
181
-	## debugging/verbose output for config file parser on request
182
-	logging.basicConfig(level=options.verbosity, format='%(levelname)s: %(message)s')
183
-	
184
-	## Now finally parse the config file
185
-	AwsConfig(options.config)
186
-
187
-	## And again some logging level adjustments
188
-	## according to configfile and command line parameters
189
-	if options.verbosity != default_verbosity:
190
-		AwsConfig.verbosity = options.verbosity
191
-	logging.root.setLevel(AwsConfig.verbosity)
192
-
193
-	## Update AwsConfig with other parameters
194
-	AwsConfig.human_readable_sizes = options.human_readable
195
-	AwsConfig.force = options.force
196
-	AwsConfig.show_uri = options.show_uri
197
-
198
-	if len(args) < 1:
199
-		error("Missing command. Please run with --help for more information.")
200
-		exit(1)
201
-
202
-	command = args.pop(0)
203
-	try:
204
-		debug("Command: " + commands[command][0])
205
-		## We must do this lookup in extra step to 
206
-		## avoid catching all KeyError exceptions
207
-		## from inner functions.
208
-		cmd_func = commands[command][1]
209
-	except KeyError, e:
210
-		error("Invalid command: %s" % e)
211
-		exit(1)
212
-
213
-	if len(args) < commands[command][2]:
214
-		error("Not enough paramters for command '%s'" % command)
215
-		exit(1)
216
-
217
-	try:
218
-		cmd_func(args)
219
-	except S3Error, e:
220
-		error("S3 error: " + str(e))
221
-	except ParameterError, e:
222
-		error("Parameter problem: " + str(e))
223
-
224
-
... ...
@@ -4,5 +4,5 @@ setup(name = "s3cmd",
4 4
 	author = "Michal Ludvig",
5 5
 	author_email = "michal@logix.cz",
6 6
 	packages = [ 'S3' ],
7
-	scripts = ['s3.py'],
7
+	scripts = ['s3cmd'],
8 8
 	)