Exemple #1
0
def Size(uri):
  """Return size of file at URI in bytes.

  Args:
    uri: URI to consider

  Returns:
    Size of file at given URI in bytes.

  Raises:
    MissingURLError if uri is a URL and cannot be found.
  """

  uri_type = GetUriType(uri)

  if TYPE_GS == uri_type:
    return gslib.FileSize(uri)

  if TYPE_LOCAL == uri_type:
    return filelib.Size(uri)

  if TYPE_HTTP == uri_type or TYPE_HTTPS == uri_type:
    try:
      response = urllib2.urlopen(uri)
      if response.getcode() == 200:
        return int(response.headers.getheader('Content-Length'))

    except urllib2.HTTPError as e:
      # Interpret 4** errors as our own MissingURLError.
      if e.code < 400 or e.code >= 500:
        raise

    raise MissingURLError('No such file at URL %s' % uri)

  raise NotSupportedForType(uri_type)
Exemple #2
0
    def testSize(self):
        gs_path = 'gs://bucket/some/path'
        local_path = '/some/local/path'
        http_path = 'http://host.domain/some/path'
        ftp_path = 'ftp://host.domain/some/path'

        result = 100
        http_response = FakeHttpResponse(200, {'Content-Length': str(result)})

        self.mox.StubOutWithMock(gslib, 'FileSize')
        self.mox.StubOutWithMock(filelib, 'Size')
        self.mox.StubOutWithMock(urilib.urllib2, 'urlopen')

        # Set up the test replay script.
        # Run 1, local.
        filelib.Size(local_path).AndReturn(result)
        # Run 2, GS.
        gslib.FileSize(gs_path).AndReturn(result)
        # Run 4, HTTP.
        urilib.urllib2.urlopen(http_path).AndReturn(http_response)
        # Run 5, FTP.
        self.mox.ReplayAll()

        # Run the test verification.
        self.assertEquals(result, urilib.Size(local_path))
        self.assertEquals(result, urilib.Size(gs_path))
        self.assertEquals(result, urilib.Size(http_path))
        self.assertRaises(urilib.NotSupportedForType, urilib.Size, ftp_path)
        self.mox.VerifyAll()
  def testSize(self):
    path = '/some/local/path'
    size = 100

    self.mox.StubOutWithMock(filelib.os.path, 'isfile')
    self.mox.StubOutWithMock(filelib.os, 'stat')

    # Set up the test replay script.
    # Run 1, success.
    filelib.os.path.isfile(path).AndReturn(True)
    filelib.os.stat(path).AndReturn(cros_test_lib.EasyAttr(st_size=size))
    # Run 2, file not found.
    filelib.os.path.isfile(path).AndReturn(False)
    self.mox.ReplayAll()

    # Run the test verification.
    self.assertEqual(size, filelib.Size(path))
    self.assertRaises(filelib.MissingFileError, filelib.Size, path)
    self.mox.VerifyAll()