def test_calculate_size(self): test_file = open(self.test_file) try: file_size = file_utils.calculate_size(test_file) finally: test_file.close() self.assertEquals(file_size, 1675)
def calculate_size(file_handle): """ Return the size of the given file-like object in Bytes. :param file_handle: A handle to an open file-like object :type file_handle: file-like object :return: The file's size, in Bytes :rtype: int """ # Calculate the size by seeking to the end to find the file size with tell() return file_utils.calculate_size(file_handle)
def generate_unit_key_and_metadata(filepath, **kwargs): """ Analyze the ISO found at the path specified by filepath, and return its unit_key and metadata as a 2-tuple. Since ISOs don't have metadata, this just amounts to the unit key and an empty metadata dict. :param filepath: The path of the file that we need the unit key and metadata for :type filepath: str :param kwargs: Unused keyword arguments :type kwargs: dict :return: A two tuple of (unit_key, metadata), both dicts :rtype: tuple """ iso = open(filepath) try: size = file_utils.calculate_size(iso) checksum = file_utils.calculate_checksum(iso) finally: iso.close() return {'name': os.path.basename(filepath), 'size': size, 'checksum': checksum}, {}