11#!/usr/bin/env python
22"""Upload and download support for apitools."""
3+ from __future__ import print_function
34
45import email .generator as email_generator
56import email .mime .multipart as mime_multipart
67import email .mime .nonmultipart as mime_nonmultipart
7- import httplib
88import io
99import json
1010import mimetypes
1111import os
1212import StringIO
1313import threading
1414
15+ from six .moves import http_client
16+
1517from _gcloud_vendor .apitools .base .py import exceptions
1618from _gcloud_vendor .apitools .base .py import http_wrapper
1719from _gcloud_vendor .apitools .base .py import util
@@ -38,7 +40,7 @@ def __init__(self, stream, close_stream=False, chunksize=None,
3840 self .__url = None
3941
4042 self .auto_transfer = auto_transfer
41- self .chunksize = chunksize or 1048576L
43+ self .chunksize = chunksize or 1048576
4244
4345 def __repr__ (self ):
4446 return str (self )
@@ -121,10 +123,10 @@ class Download(_Transfer):
121123 chunksize: default chunksize to use for transfers.
122124 """
123125 _ACCEPTABLE_STATUSES = set ((
124- httplib .OK ,
125- httplib .NO_CONTENT ,
126- httplib .PARTIAL_CONTENT ,
127- httplib .REQUESTED_RANGE_NOT_SATISFIABLE ,
126+ http_client .OK ,
127+ http_client .NO_CONTENT ,
128+ http_client .PARTIAL_CONTENT ,
129+ http_client .REQUESTED_RANGE_NOT_SATISFIABLE ,
128130 ))
129131 _REQUIRED_SERIALIZATION_KEYS = set ((
130132 'auto_transfer' , 'progress' , 'total_size' , 'url' ))
@@ -242,13 +244,13 @@ def InitializeDownload(self, http_request, http=None, client=None):
242244 @staticmethod
243245 def _ArgPrinter (response , unused_download ):
244246 if 'content-range' in response .info :
245- print 'Received %s' % response .info ['content-range' ]
247+ print ( 'Received %s' % response .info ['content-range' ])
246248 else :
247- print 'Received %d bytes' % len (response )
249+ print ( 'Received %d bytes' % len (response ) )
248250
249251 @staticmethod
250252 def _CompletePrinter (* unused_args ):
251- print 'Download complete'
253+ print ( 'Download complete' )
252254
253255 def __NormalizeStartEnd (self , start , end = None ):
254256 if end is not None :
@@ -290,10 +292,10 @@ def __ProcessResponse(self, response):
290292 """Process this response (by updating self and writing to self.stream)."""
291293 if response .status_code not in self ._ACCEPTABLE_STATUSES :
292294 raise exceptions .TransferInvalidError (response .content )
293- if response .status_code in (httplib .OK , httplib .PARTIAL_CONTENT ):
295+ if response .status_code in (http_client .OK , http_client .PARTIAL_CONTENT ):
294296 self .stream .write (response .content )
295297 self .__progress += len (response )
296- elif response .status_code == httplib .NO_CONTENT :
298+ elif response .status_code == http_client .NO_CONTENT :
297299 # It's important to write something to the stream for the case
298300 # of a 0-byte download to a file, as otherwise python won't
299301 # create the file.
@@ -348,7 +350,7 @@ def StreamInChunks(self, callback=None, finish_callback=None,
348350 additional_headers = additional_headers )
349351 response = self .__ProcessResponse (response )
350352 self ._ExecuteCallback (callback , response )
351- if (response .status_code == httplib .OK or
353+ if (response .status_code == http_client .OK or
352354 self .progress >= self .total_size ):
353355 break
354356 self ._ExecuteCallback (finish_callback , response )
@@ -591,7 +593,7 @@ def _RefreshResumableUploadState(self):
591593 self .http , refresh_request , redirections = 0 )
592594 range_header = refresh_response .info .get (
593595 'Range' , refresh_response .info .get ('range' ))
594- if refresh_response .status_code in (httplib .OK , httplib .CREATED ):
596+ if refresh_response .status_code in (http_client .OK , http_client .CREATED ):
595597 self .__complete = True
596598 elif refresh_response .status_code == http_wrapper .RESUME_INCOMPLETE :
597599 if range_header is None :
@@ -619,7 +621,7 @@ def InitializeUpload(self, http_request, http=None, client=None):
619621 http_request .url = client .FinalizeTransferUrl (http_request .url )
620622 self .EnsureUninitialized ()
621623 http_response = http_wrapper .MakeRequest (http , http_request )
622- if http_response .status_code != httplib .OK :
624+ if http_response .status_code != http_client .OK :
623625 raise exceptions .HttpError .FromResponse (http_response )
624626
625627 self .__server_chunk_granularity = http_response .info .get (
@@ -651,11 +653,11 @@ def __ValidateChunksize(self, chunksize=None):
651653
652654 @staticmethod
653655 def _ArgPrinter (response , unused_upload ):
654- print 'Sent %s' % response .info ['range' ]
656+ print ( 'Sent %s' % response .info ['range' ])
655657
656658 @staticmethod
657659 def _CompletePrinter (* unused_args ):
658- print 'Upload complete'
660+ print ( 'Upload complete' )
659661
660662 def StreamInChunks (self , callback = None , finish_callback = None ,
661663 additional_headers = None ):
@@ -674,7 +676,7 @@ def StreamInChunks(self, callback=None, finish_callback=None,
674676 while not self .complete :
675677 response = self .__SendChunk (self .stream .tell (),
676678 additional_headers = additional_headers )
677- if response .status_code in (httplib .OK , httplib .CREATED ):
679+ if response .status_code in (http_client .OK , http_client .CREATED ):
678680 self .__complete = True
679681 break
680682 self .__progress = self .__GetLastByte (response .info ['range' ])
@@ -703,10 +705,10 @@ def __SendChunk(self, start, additional_headers=None, data=None):
703705 request .headers .update (additional_headers )
704706
705707 response = http_wrapper .MakeRequest (self .bytes_http , request )
706- if response .status_code not in (httplib .OK , httplib .CREATED ,
708+ if response .status_code not in (http_client .OK , http_client .CREATED ,
707709 http_wrapper .RESUME_INCOMPLETE ):
708710 raise exceptions .HttpError .FromResponse (response )
709- if response .status_code in (httplib .OK , httplib .CREATED ):
711+ if response .status_code in (http_client .OK , http_client .CREATED ):
710712 return response
711713 # TODO(craigcitro): Add retries on no progress?
712714 last_byte = self .__GetLastByte (response .info ['range' ])
0 commit comments