Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Unified Diff: third_party/gsutil/boto/boto/file/key.py

Issue 10199002: Upgrade gsutil to 3.4 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed comments Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/gsutil/boto/boto/file/key.py
diff --git a/third_party/gsutil/20110627/boto/boto/file/key.py b/third_party/gsutil/boto/boto/file/key.py
similarity index 67%
rename from third_party/gsutil/20110627/boto/boto/file/key.py
rename to third_party/gsutil/boto/boto/file/key.py
index af801a5e96d978bd87ed1af9d8950b066a581d64..d39c8c65f19462275337ebbd9cbe5f09d39d7378 100755
--- a/third_party/gsutil/20110627/boto/boto/file/key.py
+++ b/third_party/gsutil/boto/boto/file/key.py
@@ -1,4 +1,5 @@
# Copyright 2010 Google Inc.
+# Copyright (c) 2011, Nexenta Systems Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
@@ -22,14 +23,31 @@
# File representation of key, for use with "file://" URIs.
import os, shutil, StringIO
+import sys
class Key(object):
- def __init__(self, bucket, name, fp=None):
+ KEY_STREAM_READABLE = 0x01
+ KEY_STREAM_WRITABLE = 0x02
+ KEY_STREAM = (KEY_STREAM_READABLE | KEY_STREAM_WRITABLE)
+ KEY_REGULAR_FILE = 0x00
+
+ def __init__(self, bucket, name, fp=None, key_type=KEY_REGULAR_FILE):
self.bucket = bucket
self.full_path = name
- self.name = name
- self.fp = fp
+ if name == '-':
+ self.name = None
+ else:
+ self.name = name
+ self.key_type = key_type
+ if key_type == self.KEY_STREAM_READABLE:
+ self.fp = sys.stdin
+ self.full_path = '<STDIN>'
+ elif key_type == self.KEY_STREAM_WRITABLE:
+ self.fp = sys.stdout
+ self.full_path = '<STDOUT>'
+ else:
+ self.fp = fp
def __str__(self):
return 'file://' + self.full_path
@@ -50,8 +68,16 @@ class Key(object):
:type cb: int
:param num_cb: ignored in this subclass.
"""
- key_file = open(self.full_path, 'rb')
- shutil.copyfileobj(key_file, fp)
+ if self.key_type & self.KEY_STREAM_READABLE:
+ raise BotoClientError('Stream is not Readable')
+ elif self.key_type & self.KEY_STREAM_WRITABLE:
+ key_file = self.fp
+ else:
+ key_file = open(self.full_path, 'rb')
+ try:
+ shutil.copyfileobj(key_file, fp)
+ finally:
+ key_file.close()
def set_contents_from_file(self, fp, headers=None, replace=True, cb=None,
num_cb=10, policy=None, md5=None):
@@ -88,11 +114,18 @@ class Key(object):
This is the same format returned by the compute_md5 method.
:param md5: ignored in this subclass.
"""
- if not replace and os.path.exists(self.full_path):
- return
- key_file = open(self.full_path, 'wb')
- shutil.copyfileobj(fp, key_file)
- key_file.close()
+ if self.key_type & self.KEY_STREAM_WRITABLE:
+ raise BotoClientError('Stream is not writable')
+ elif self.key_type & self.KEY_STREAM_READABLE:
+ key_file = self.fp
+ else:
+ if not replace and os.path.exists(self.full_path):
+ return
+ key_file = open(self.full_path, 'wb')
+ try:
+ shutil.copyfileobj(fp, key_file)
+ finally:
+ key_file.close()
def get_contents_as_string(self, headers=None, cb=None, num_cb=10,
torrent=False):
@@ -121,3 +154,15 @@ class Key(object):
fp = StringIO.StringIO()
self.get_contents_to_file(fp)
return fp.getvalue()
+
+ def is_stream(self):
+ return (self.key_type & self.KEY_STREAM)
+
+ def close(self):
+ """
+ Closes fp associated with underlying file.
+ Caller should call this method when done with this class, to avoid
+ using up OS resources (e.g., when iterating over a large number
+ of files).
+ """
+ self.fp.close()
« no previous file with comments | « third_party/gsutil/boto/boto/file/connection.py ('k') | third_party/gsutil/boto/boto/file/simpleresultset.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698