git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3py/trunk@60 830e0280-6d2a-0410-9c65-932aecc39d9d
Michal Ludvig authored on 2007/01/24 09:03:12... | ... |
@@ -38,7 +38,7 @@ class S3Error (Exception): |
38 | 38 |
class ParameterError(Exception): |
39 | 39 |
pass |
40 | 40 |
|
41 |
-class S3: |
|
41 |
+class S3(object): |
|
42 | 42 |
http_methods = BidirMap( |
43 | 43 |
GET = 0x01, |
44 | 44 |
PUT = 0x02, |
... | ... |
@@ -280,10 +280,21 @@ class S3: |
280 | 280 |
else: |
281 | 281 |
return object and object or bucket |
282 | 282 |
|
283 |
- def parse_s3_uri(self, uri): |
|
283 |
+ def parse_uri(self, uri): |
|
284 | 284 |
match = re.compile("^s3://([^/]*)/?(.*)").match(uri) |
285 | 285 |
if match: |
286 | 286 |
return (True,) + match.groups() |
287 | 287 |
else: |
288 | 288 |
return (False, "", "") |
289 | 289 |
|
290 |
+ def is_uri(self, uri): |
|
291 |
+ isuri, bucket, object = self.parse_uri(uri) |
|
292 |
+ return isuri |
|
293 |
+ |
|
294 |
+ def is_uri_bucket(self, uri): |
|
295 |
+ isuri, bucket, object = self.parse_uri(uri) |
|
296 |
+ return isuri and bool(bucket) |
|
297 |
+ |
|
298 |
+ def is_uri_object(self, uri): |
|
299 |
+ isuri, bucket, object = self.parse_uri(uri) |
|
300 |
+ return isuri and bool(bucket) and bool(object) |
... | ... |
@@ -25,7 +25,7 @@ def cmd_ls(args): |
25 | 25 |
s3 = S3(Config()) |
26 | 26 |
bucket = None |
27 | 27 |
if len(args) > 0: |
28 |
- isuri, bucket, object = s3.parse_s3_uri(args[0]) |
|
28 |
+ isuri, bucket, object = s3.parse_uri(args[0]) |
|
29 | 29 |
if not isuri: |
30 | 30 |
bucket = args[0] |
31 | 31 |
if bucket: |
... | ... |
@@ -53,7 +53,7 @@ def cmd_buckets_list_all_all(args): |
53 | 53 |
|
54 | 54 |
def cmd_bucket_list(args): |
55 | 55 |
s3 = S3(Config()) |
56 |
- isuri, bucket, object = s3.parse_s3_uri(args[0]) |
|
56 |
+ isuri, bucket, object = s3.parse_uri(args[0]) |
|
57 | 57 |
if not isuri: |
58 | 58 |
bucket = args[0] |
59 | 59 |
output("Bucket '%s':" % bucket) |
... | ... |
@@ -77,7 +77,7 @@ def cmd_bucket_list(args): |
77 | 77 |
|
78 | 78 |
def cmd_bucket_create(args): |
79 | 79 |
s3 = S3(Config()) |
80 |
- isuri, bucket, object = s3.parse_s3_uri(args[0]) |
|
80 |
+ isuri, bucket, object = s3.parse_uri(args[0]) |
|
81 | 81 |
if not isuri: |
82 | 82 |
bucket = args[0] |
83 | 83 |
try: |
... | ... |
@@ -92,7 +92,7 @@ def cmd_bucket_create(args): |
92 | 92 |
|
93 | 93 |
def cmd_bucket_delete(args): |
94 | 94 |
s3 = S3(Config()) |
95 |
- isuri, bucket, object = s3.parse_s3_uri(args[0]) |
|
95 |
+ isuri, bucket, object = s3.parse_uri(args[0]) |
|
96 | 96 |
if not isuri: |
97 | 97 |
bucket = args[0] |
98 | 98 |
try: |
... | ... |
@@ -106,7 +106,38 @@ def cmd_bucket_delete(args): |
106 | 106 |
output("Bucket '%s' removed" % bucket) |
107 | 107 |
|
108 | 108 |
def cmd_cp(args): |
109 |
- raise ParameterError("Not yet implemented") |
|
109 |
+ s3 = S3(Config()) |
|
110 |
+ xnor=lambda a, b:(a and b) or (not a and not b) |
|
111 |
+ cp_remote_local = s3.is_uri(args[0]) |
|
112 |
+ arg_copy = args[:] |
|
113 |
+ srcs = [] |
|
114 |
+ dest = [] |
|
115 |
+ index = 0 |
|
116 |
+ try: |
|
117 |
+ while(xnor(cp_remote_local, s3.is_uri(arg_copy[index]))): |
|
118 |
+ if cp_remote_local: |
|
119 |
+ if not s3.is_uri_object(arg_copy[index]): |
|
120 |
+ raise ParameterError("%s: source URI must include the object name!" % arg_copy[index]) |
|
121 |
+ else: |
|
122 |
+ if not os.path.isfile(arg_copy[index]): |
|
123 |
+ raise ParameterError("%s is not a regular file!" % arg_copy[index]) |
|
124 |
+ srcs.append(arg_copy[index]) |
|
125 |
+ index+=1 |
|
126 |
+ dest = arg_copy[index] |
|
127 |
+ if len(arg_copy) > index+1: |
|
128 |
+ raise ParameterError("Too many arguments for 'cp': %s" % (arg_copy[index + 1:])) |
|
129 |
+ except IndexError, e: |
|
130 |
+ raise ParameterError("Destination %s not set. See --help for details." % |
|
131 |
+ (cp_remote_local and "local directory" or "S3-URI")) |
|
132 |
+ if cp_remote_local and not os.path.isdir(dest): |
|
133 |
+ raise ParameterError("Destination path '%s' is not a directory." % |
|
134 |
+ (dest)) |
|
135 |
+ ## All checks done, now DO IT |
|
136 |
+ if cp_remote_local: |
|
137 |
+ for src in srcs: |
|
138 |
+ cmd_object_get([src, dest]) |
|
139 |
+ else: |
|
140 |
+ cmd_object_put(srcs + [dest]) |
|
110 | 141 |
|
111 | 142 |
def cmd_object_put(args): |
112 | 143 |
s3 = S3(Config()) |
... | ... |
@@ -114,7 +145,7 @@ def cmd_object_put(args): |
114 | 114 |
s3uri = args.pop() |
115 | 115 |
files = args[:] |
116 | 116 |
|
117 |
- isuri, bucket, object = s3.parse_s3_uri(s3uri) |
|
117 |
+ isuri, bucket, object = s3.parse_uri(s3uri) |
|
118 | 118 |
if not isuri: |
119 | 119 |
raise ParameterError("Expecting S3 URI instead of '%s'" % s3uri) |
120 | 120 |
|
... | ... |
@@ -140,7 +171,7 @@ def cmd_object_put(args): |
140 | 140 |
def cmd_object_get(args): |
141 | 141 |
s3 = S3(Config()) |
142 | 142 |
s3uri = args.pop(0) |
143 |
- isuri, bucket, object = s3.parse_s3_uri(s3uri) |
|
143 |
+ isuri, bucket, object = s3.parse_uri(s3uri) |
|
144 | 144 |
if not isuri or not bucket or not object: |
145 | 145 |
raise ParameterError("Expecting S3 object URI instead of '%s'" % s3uri) |
146 | 146 |
destination = len(args) > 0 and args.pop(0) or object |
... | ... |
@@ -155,7 +186,7 @@ def cmd_object_get(args): |
155 | 155 |
def cmd_object_del(args): |
156 | 156 |
s3 = S3(Config()) |
157 | 157 |
s3uri = args.pop(0) |
158 |
- isuri, bucket, object = s3.parse_s3_uri(s3uri) |
|
158 |
+ isuri, bucket, object = s3.parse_uri(s3uri) |
|
159 | 159 |
if not isuri or not bucket or not object: |
160 | 160 |
raise ParameterError("Expecting S3 object URI instead of '%s'" % s3uri) |
161 | 161 |
response = s3.object_delete(bucket, object) |