src/modules/misc.py
changeset 556 1c3526ca7b9e
parent 551 233f0eeddd02
child 565 1fb4f05220b9
equal deleted inserted replaced
555:87c86da72fd9 556:1c3526ca7b9e
    30 import re
    30 import re
    31 import sha
    31 import sha
    32 import socket
    32 import socket
    33 import urllib
    33 import urllib
    34 import urllib2
    34 import urllib2
    35 import httplib
       
    36 import urlparse
    35 import urlparse
    37 import sys
    36 import sys
    38 import zlib
    37 import zlib
    39 import time
    38 import time
    40 import calendar
    39 import calendar
    79 
    78 
    80 _client_version = "pkg/%s (%s %s; %s %s; %%s)" % \
    79 _client_version = "pkg/%s (%s %s; %s %s; %%s)" % \
    81     (VERSION, portable.util.get_canonical_os_name(), platform.machine(),
    80     (VERSION, portable.util.get_canonical_os_name(), platform.machine(),
    82     portable.util.get_os_release(), platform.version())
    81     portable.util.get_os_release(), platform.version())
    83 
    82 
    84 def versioned_urlopen(base_uri, operation, versions = [], tail = None,
    83 def versioned_urlopen(base_uri, operation, versions = None, tail = None,
    85     data = None, headers = None, ssl_creds = None, imgtype = IMG_NONE,
    84     data = None, headers = None, ssl_creds = None, imgtype = IMG_NONE,
    86     uuid = None):
    85     method = "GET", uuid = None):
    87         """Open the best URI for an operation given a set of versions.
    86         """Open the best URI for an operation given a set of versions.
    88 
    87 
    89         Both the client and the server may support multiple versions of
    88         Both the client and the server may support multiple versions of
    90         the protocol of a particular operation.  The client will pass
    89         the protocol of a particular operation.  The client will pass
    91         this method an ordered array of versions it understands, along
    90         this method an ordered array of versions it understands, along
   113                 opener_dir = urllib2.build_opener(cert_handler)
   112                 opener_dir = urllib2.build_opener(cert_handler)
   114                 url_opener = opener_dir.open
   113                 url_opener = opener_dir.open
   115         else:
   114         else:
   116                 url_opener = urllib2.urlopen
   115                 url_opener = urllib2.urlopen
   117 
   116 
       
   117         if not versions:
       
   118                 versions = []
       
   119 
   118         if not headers:
   120         if not headers:
   119                 headers = {}
   121                 headers = {}
   120 
   122 
   121         for version in versions:
   123         for version in versions:
   122                 if base_uri[-1] != '/':
   124                 if base_uri[-1] != '/':
   132                 headers["User-Agent"] = \
   134                 headers["User-Agent"] = \
   133                     _client_version % img_type_names[imgtype]
   135                     _client_version % img_type_names[imgtype]
   134                 if uuid:
   136                 if uuid:
   135                         headers["X-IPkg-UUID"] = uuid
   137                         headers["X-IPkg-UUID"] = uuid
   136                 req = urllib2.Request(url = uri, headers = headers)
   138                 req = urllib2.Request(url = uri, headers = headers)
   137                 if data is not None:
   139                 if method == "HEAD":
       
   140                         # Must override urllib2's get_method since it doesn't
       
   141                         # natively support this operation.
       
   142                         req.get_method = lambda: "HEAD"
       
   143                 elif data is not None:
   138                         req.add_data(data)
   144                         req.add_data(data)
   139 
   145 
   140                 try:
   146                 try:
   141                         c = url_opener(req)
   147                         c = url_opener(req)
   142                 except urllib2.HTTPError, e:
   148                 except urllib2.HTTPError, e:
   430         def __init__(self, action, hashval):
   436         def __init__(self, action, hashval):
   431                 self.action = action
   437                 self.action = action
   432                 self.hashval = hashval
   438                 self.hashval = hashval
   433 
   439 
   434         def __str__(self):
   440         def __str__(self):
   435                 str = "Action with path %s should have hash %s. Computed hash %s instead." % \
   441                 str = "Action with path %s should have hash %s. Computed " \
   436                     (self.action.attrs["path"], self.action.attrs["chash"], 
   442                     "hash %s instead." % (self.action.attrs["path"],
   437                     self.hashval)
   443                     self.action.attrs["chash"], self.hashval)
   438                 return str
   444                 return str
   439 
   445 
   440 # Default maximum memory useage during indexing
   446 # Default maximum memory useage during indexing
   441 # This is a soft cap since memory usage is estimated.
   447 # This is a soft cap since memory usage is estimated.
   442 try:
   448 try: