def test_get_path(self, config): storage_dir = '/tmp/storage' config.get = lambda s, p: {'server': {'storage_dir': storage_dir}}[s][p] unit = Mock(id='0123456789', type_id='ABC') # test path = FileStorage.get_path(unit) # validation self.assertEqual( path, os.path.join(storage_dir, 'content', 'units', unit.type_id, unit.id[0:4], unit.id))
def set_storage_path(self, filename=None): """ Set the storage path. This is a total hack to support existing single-file units with a _storage_path that includes the file name. :param filename: An optional filename to appended to the path. :rtype filename: str """ path = FileStorage.get_path(self) if filename: if not os.path.isabs(filename): path = os.path.join(path, filename) else: raise ValueError(_('must be relative path')) self._storage_path = path
def test_get_path(self, config): storage_dir = '/tmp/storage' config.get = lambda s, p: { 'server': { 'storage_dir': storage_dir } }[s][p] unit = Mock(id='0123456789', type_id='ABC') # test path = FileStorage.get_path(unit) # validation self.assertEqual( path, os.path.join(storage_dir, 'content', 'units', unit.type_id, unit.id[0:4], unit.id))
def test_get_path(self, config, sha256): storage_dir = '/tmp/storage' digest = '0123456789' config.get = lambda s, p: {'server': {'storage_dir': storage_dir}}[s][p] unit = Mock(type_id='ABC') unit.unit_key_as_digest.return_value = digest # test path = FileStorage.get_path(unit) # validation unit.unit_key_as_digest.assert_called_once_with(sha256.return_value) self.assertEqual( path, os.path.join(storage_dir, 'content', 'units', unit.type_id, digest[0:2], digest[2:]))
def download_succeeded(self, report): """ Marks the individual file for the unit as downloaded and moves it into its final storage location if its checksum value matches the value in the catalog entry (if present). Inherited from DownloadEventListener. :param report: the report associated with the download request. :type report: nectar.report.DownloadReport """ # Reload the content unit unit_model = plugin_api.get_unit_model_by_id(report.data[TYPE_ID]) unit_qs = unit_model.objects.filter(id=report.data[UNIT_ID]) content_unit = unit_qs.only('_content_type_id', 'id', '_last_updated').get() path_entry = report.data[UNIT_FILES][report.destination] # Validate the file and update the progress. catalog_entry = path_entry[CATALOG_ENTRY] try: self.validate_file(report.destination, catalog_entry.checksum_algorithm, catalog_entry.checksum) relative_path = os.path.relpath(catalog_entry.path, FileStorage.get_path(content_unit)) if len(report.data[UNIT_FILES]) == 1: # If the unit is single-file, update the storage path to point to the file content_unit.set_storage_path(relative_path) unit_qs.update_one( set___storage_path=content_unit._storage_path) content_unit.import_content(report.destination) else: content_unit.import_content(report.destination, location=relative_path) self.progress_successes += 1 path_entry[PATH_DOWNLOADED] = True except (InvalidChecksumType, VerificationException, IOError), e: _logger.debug( _('Download of {path} failed: {reason}.').format( path=catalog_entry.path, reason=str(e))) path_entry[PATH_DOWNLOADED] = False self.progress_failures += 1
def download_succeeded(self, report): """ Marks the individual file for the unit as downloaded and moves it into its final storage location if its checksum value matches the value in the catalog entry (if present). Inherited from DownloadEventListener. :param report: the report associated with the download request. :type report: nectar.report.DownloadReport """ # Reload the content unit unit_model = plugin_api.get_unit_model_by_id(report.data[TYPE_ID]) unit_qs = unit_model.objects.filter(id=report.data[UNIT_ID]) content_unit = unit_qs.only('_content_type_id', 'id', '_last_updated').get() path_entry = report.data[UNIT_FILES][report.destination] # Validate the file and update the progress. catalog_entry = path_entry[CATALOG_ENTRY] try: self.validate_file( report.destination, catalog_entry.checksum_algorithm, catalog_entry.checksum ) relative_path = os.path.relpath( catalog_entry.path, FileStorage.get_path(content_unit) ) if len(report.data[UNIT_FILES]) == 1: # If the unit is single-file, update the storage path to point to the file content_unit.set_storage_path(relative_path) unit_qs.update_one(set___storage_path=content_unit._storage_path) content_unit.import_content(report.destination) else: content_unit.import_content(report.destination, location=relative_path) self.progress_successes += 1 path_entry[PATH_DOWNLOADED] = True except (InvalidChecksumType, VerificationException, IOError), e: _logger.debug(_('Download of {path} failed: {reason}.').format( path=catalog_entry.path, reason=str(e))) path_entry[PATH_DOWNLOADED] = False self.progress_failures += 1
def test_get_path(self, config, sha256): storage_dir = '/tmp/storage' digest = '0123456789' config.get = lambda s, p: { 'server': { 'storage_dir': storage_dir } }[s][p] unit = Mock(type_id='ABC') unit.unit_key_as_digest.return_value = digest # test path = FileStorage.get_path(unit) # validation unit.unit_key_as_digest.assert_called_once_with(sha256.return_value) self.assertEqual( path, os.path.join(storage_dir, 'content', 'units', unit.type_id, digest[0:2], digest[2:]))