# Path: gslib/exception.py
# class CommandException(StandardError):
#   """Exception raised when a problem is encountered running a gsutil command.
# 
#   This exception should be used to signal user errors or system failures
#   (like timeouts), not bugs (like an incorrect param value). For the
#   latter you should raise Exception so we can see where/how it happened
#   via gsutil -D (which will include a stack trace for raised Exceptions).
#   """
# 
#   def __init__(self, reason, informational=False):
#     """Instantiate a CommandException.
# 
#     Args:
#       reason: Text describing the problem.
#       informational: Indicates reason should be printed as FYI, not a failure.
#     """
#     StandardError.__init__(self)
#     self.reason = reason
#     self.informational = informational
# 
#   def __repr__(self):
#     return str(self)
# 
#   def __str__(self):
#     return 'CommandException: %s' % self.reason
# 
# Path: gslib/storage_url.py
# def StorageUrlFromString(url_str):
#   """Static factory function for creating a StorageUrl from a string."""
# 
#   scheme = GetSchemeFromUrlString(url_str)
# 
#   if not IsKnownUrlScheme(scheme):
#     raise InvalidUrlError('Unrecognized scheme "%s"' % scheme)
#   if scheme == 'file':
#     path = _GetPathFromUrlString(url_str)
#     is_stream = (path == '-')
#     is_fifo = False
#     try:
#       is_fifo = stat.S_ISFIFO(os.stat(path).st_mode)
#     except OSError:
#       pass
#     return _FileUrl(url_str, is_stream=is_stream, is_fifo=is_fifo)
#   return _CloudUrl(url_str)


import hashlib
import os
import pkgutil
import gslib.tests.testcase as testcase
from unittest import mock
from gslib.exception import CommandException
from gslib.storage_url import StorageUrlFromString
from gslib.utils.constants import TRANSFER_BUFFER_SIZE
from gslib.utils.hashing_helper import CalculateMd5FromContents
from gslib.utils.hashing_helper import GetMd5
from gslib.utils.hashing_helper import HashingFileUploadWrapper
      wrapper.read(initial_read)
      self.assertEqual(wrapper.tell(), initial_read)
      wrapper.seek(0, os.SEEK_END)
      self.assertEqual(wrapper.tell(), tmp_file_len)
      wrapper.seek(initial_read, os.SEEK_SET)
      data = wrapper.read()
      self.assertEqual(len(data), tmp_file_len - initial_read)
    with open(tmp_file, 'rb') as stream:
      actual = CalculateMd5FromContents(stream)
    self.assertEqual(actual, digesters['md5'].hexdigest())

  def testValidSeekAway(self):
    for initial_read in (0, TRANSFER_BUFFER_SIZE - 1, TRANSFER_BUFFER_SIZE,
                         TRANSFER_BUFFER_SIZE + 1, TRANSFER_BUFFER_SIZE * 2 - 1,
                         TRANSFER_BUFFER_SIZE * 2,
                         TRANSFER_BUFFER_SIZE * 2 + 1):
      self._testSeekAway(initial_read)

  def testInvalidSeekAway(self):
    """Tests seeking to EOF and then reading without first doing a SEEK_SET."""
    tmp_file = self._GetTestFile()
    digesters = {'md5': GetMd5()}
    with open(tmp_file, 'rb') as stream:
      wrapper = HashingFileUploadWrapper(stream, digesters, {'md5': GetMd5},
                                         self._dummy_url, self.logger)
      wrapper.read(TRANSFER_BUFFER_SIZE)
      wrapper.seek(0, os.SEEK_END)
      try:
        wrapper.read()
        self.fail('Expected CommandException for invalid seek.')

# Path: gslib/exception.py
# class CommandException(StandardError):
#   """Exception raised when a problem is encountered running a gsutil command.
# 
#   This exception should be used to signal user errors or system failures
#   (like timeouts), not bugs (like an incorrect param value). For the
#   latter you should raise Exception so we can see where/how it happened
#   via gsutil -D (which will include a stack trace for raised Exceptions).
#   """
# 
#   def __init__(self, reason, informational=False):
#     """Instantiate a CommandException.
# 
#     Args:
#       reason: Text describing the problem.
#       informational: Indicates reason should be printed as FYI, not a failure.
#     """
#     StandardError.__init__(self)
#     self.reason = reason
#     self.informational = informational
# 
#   def __repr__(self):
#     return str(self)
# 
#   def __str__(self):
#     return 'CommandException: %s' % self.reason
# 
# Path: gslib/storage_url.py
# def StorageUrlFromString(url_str):
#   """Static factory function for creating a StorageUrl from a string."""
# 
#   scheme = GetSchemeFromUrlString(url_str)
# 
#   if not IsKnownUrlScheme(scheme):
#     raise InvalidUrlError('Unrecognized scheme "%s"' % scheme)
#   if scheme == 'file':
#     path = _GetPathFromUrlString(url_str)
#     is_stream = (path == '-')
#     is_fifo = False
#     try:
#       is_fifo = stat.S_ISFIFO(os.stat(path).st_mode)
#     except OSError:
#       pass
#     return _FileUrl(url_str, is_stream=is_stream, is_fifo=is_fifo)
#   return _CloudUrl(url_str)


# Path: gslib/tests/test_hashing_helper.py
import hashlib
import os
import pkgutil
import gslib.tests.testcase as testcase
from unittest import mock
from gslib.exception import CommandException
from gslib.storage_url import StorageUrlFromString
from gslib.utils.constants import TRANSFER_BUFFER_SIZE
from gslib.utils.hashing_helper import CalculateMd5FromContents
from gslib.utils.hashing_helper import GetMd5
from gslib.utils.hashing_helper import HashingFileUploadWrapper

      wrapper.read(initial_read)
      self.assertEqual(wrapper.tell(), initial_read)
      wrapper.seek(0, os.SEEK_END)
      self.assertEqual(wrapper.tell(), tmp_file_len)
      wrapper.seek(initial_read, os.SEEK_SET)
      data = wrapper.read()
      self.assertEqual(len(data), tmp_file_len - initial_read)
    with open(tmp_file, 'rb') as stream:
      actual = CalculateMd5FromContents(stream)
    self.assertEqual(actual, digesters['md5'].hexdigest())

  def testValidSeekAway(self):
    for initial_read in (0, TRANSFER_BUFFER_SIZE - 1, TRANSFER_BUFFER_SIZE,
                         TRANSFER_BUFFER_SIZE + 1, TRANSFER_BUFFER_SIZE * 2 - 1,
                         TRANSFER_BUFFER_SIZE * 2,
                         TRANSFER_BUFFER_SIZE * 2 + 1):
      self._testSeekAway(initial_read)

  def testInvalidSeekAway(self):
    """Tests seeking to EOF and then reading without first doing a SEEK_SET."""
    tmp_file = self._GetTestFile()
    digesters = {'md5': GetMd5()}
    with open(tmp_file, 'rb') as stream:
      wrapper = HashingFileUploadWrapper(stream, digesters, {'md5': GetMd5},
                                         self._dummy_url, self.logger)
      wrapper.read(TRANSFER_BUFFER_SIZE)
      wrapper.seek(0, os.SEEK_END)
      try:
        wrapper.read()
        self.fail('Expected CommandException for invalid seek.')

      except CommandException as e: