Skip to content

Commit

Permalink
Simple is better than complex
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinMind committed Nov 26, 2024
1 parent 7cee05b commit 3c32be4
Showing 1 changed file with 18 additions and 40 deletions.
58 changes: 18 additions & 40 deletions src/olympia/blocklist/mlbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,46 +310,24 @@ def generate_and_write_stash(self, previous_mlbf: 'MLBF' = None):
stash_json = {key: [] for key in [UNBLOCKED_STASH_KEY, *STASH_KEYS.values()]}

diffs = self.generate_diffs(previous_mlbf)
soft_block_enabled = waffle.switch_is_active('enable-soft-blocking')
set_blocked_any_type = set()
# Build the stash json from the diff of each block type
for block_type in BlockType:
skip_no_changes = not self.should_upload_stash(block_type, previous_mlbf)
skip_soft_block = (
block_type == BlockType.SOFT_BLOCKED and not soft_block_enabled
)

# If there are no additions or removals, or if soft blocking is disabled.
if any([skip_no_changes, skip_soft_block]):
continue

added, removed, _ = diffs[block_type]
# Keep track of items added to all block types
# regardless if we will end up writing to the stash or not.
# This is used to filter out items from the unblocked list.
set_blocked_any_type.update(added)

# If we are not going to upload a filter for this block type,
# include the added items in the stash for this block type.
if not self.should_upload_filter(block_type, previous_mlbf):
stash_json[STASH_KEYS[block_type]] = added
# For now, add all items removed from the block type to the
# unblocked list. Items that are also added to another block list
# will be filtered out after we have collected all added items.
stash_json[UNBLOCKED_STASH_KEY].extend(removed)

# Filter out any items that are removed from one block type
# and added to another. We do not support transitions from one
# block type to another so we should not include those items in the
# unblocked list.
stash_json[UNBLOCKED_STASH_KEY] = [
item
for item in stash_json[UNBLOCKED_STASH_KEY]
if item not in set_blocked_any_type
]

# Write the stash json to the stash file, even if there are no changes.
# We should be able to inspect that the changes even if there are none.
blocked_added, blocked_removed, _ = diffs[BlockType.BLOCKED]

if not self.should_upload_filter(BlockType.BLOCKED):
stash_json[STASH_KEYS[BlockType.BLOCKED]] = blocked_added
stash_json[UNBLOCKED_STASH_KEY] = blocked_removed

if waffle.switch_is_active('enable-soft-blocking'):
soft_blocked_added, soft_blocked_removed, _ = diffs[BlockType.SOFT_BLOCKED]
if not self.should_upload_filter(BlockType.SOFT_BLOCKED):
stash_json[STASH_KEYS[BlockType.SOFT_BLOCKED]] = soft_blocked_added
added_items = set(blocked_added + soft_blocked_added)
stash_json[UNBLOCKED_STASH_KEY] = [
unblocked
for unblocked in (blocked_removed + soft_blocked_removed)
if unblocked not in added_items
]

# write stash
stash_path = self.stash_path
with self.storage.open(stash_path, 'w') as json_file:
log.info(f'Writing to file {stash_path}')
Expand Down

0 comments on commit 3c32be4

Please sign in to comment.