Example #1
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()
Example #2
0
    def testFileSize(self):
        self.mox.StubOutWithMock(utils, 'RunCommand')
        gs_uri = '%s/%s' % (self.bucket_uri, 'some/file/path')

        # Set up the test replay script.
        cmd = [self.gsutil, '-d', 'stat', gs_uri]
        size = 96
        output = '\n'.join([
            'header: x-goog-generation: 1386322968237000',
            'header: x-goog-metageneration: 1',
            'header: x-goog-stored-content-encoding: identity',
            'header: x-goog-stored-content-length: %d' % size,
            'header: Content-Type: application/octet-stream'
        ])

        utils.RunCommand(cmd,
                         redirect_stdout=True,
                         redirect_stderr=True,
                         return_result=True).AndReturn(
                             cros_test_lib.EasyAttr(output=output))
        self.mox.ReplayAll()

        # Run the test verification.
        result = gslib.FileSize(gs_uri)
        self.assertEqual(size, result)
        self.mox.VerifyAll()
Example #3
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)