Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinMind committed Sep 27, 2024
1 parent 07e91ff commit 9056990
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
8 changes: 6 additions & 2 deletions src/olympia/amo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,12 @@ def _open(self, name, mode='rb'):
raise
return super()._open(name, mode=mode)

def path(self, name):
return os.path.normpath(super().path(force_str(name)))
def path(self, *names):
"""
Returns the absolute path to the file, joining multiple path components if provided.
"""
combined_path = os.path.join(*names)
return os.path.normpath(super().path(force_str(combined_path)))

def walk(self, path):
"""
Expand Down
52 changes: 18 additions & 34 deletions src/olympia/blocklist/mlbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import os
import secrets

from django.conf import settings
from django.utils.functional import cached_property

from filtercascade import FilterCascade
from filtercascade.fileformats import HashAlgorithm

from olympia.blocklist.models import BlockType
import olympia.core.logger
from olympia.amo.utils import SafeStorage
from olympia.constants.blocklist import BASE_REPLACE_THRESHOLD
Expand Down Expand Up @@ -46,12 +46,13 @@ def generate_mlbf(stats, blocked, not_blocked):
return cascade


def fetch_blocked_from_db():
def fetch_blocked_from_db(block_type: BlockType):
from olympia.blocklist.models import BlockVersion

qs = BlockVersion.objects.filter(version__file__is_signed=True).values_list(
qs = BlockVersion.objects.by_block_type(block_type).values_list(
'block__guid', 'version__version', 'version_id', named=True
)

all_versions = {
block_version.version_id: (
block_version.block__guid,
Expand All @@ -70,6 +71,9 @@ def fetch_all_versions_from_db(excluding_version_ids=None):
)
return list(qs)

class MLBFType(BlockType):
STASH = 'stash'
NOTBLOCKED = 'notblocked'

class MLBF:
KEY_FORMAT = '{guid}:{version}'
Expand All @@ -87,41 +91,16 @@ def hash_filter_inputs(cls, input_list):
for (guid, version) in input_list
}

@property
def _blocked_path(self):
return os.path.join(settings.MLBF_STORAGE_PATH, self.id, 'blocked.json')

@cached_property
def blocked_items(self):
raise NotImplementedError

def write_blocked_items(self):
blocked_path = self._blocked_path
with self.storage.open(blocked_path, 'w') as json_file:
log.info(f'Writing to file {blocked_path}')
json.dump(self.blocked_items, json_file)

@property
def _not_blocked_path(self):
return os.path.join(settings.MLBF_STORAGE_PATH, self.id, 'notblocked.json')

@cached_property
def not_blocked_items(self):
raise NotImplementedError

def write_not_blocked_items(self):
not_blocked_path = self._not_blocked_path
with self.storage.open(not_blocked_path, 'w') as json_file:
log.info(f'Writing to file {not_blocked_path}')
json.dump(self.not_blocked_items, json_file)
def dump_json(self, items, block_type: MLBFType):
path = self.storage.path(self.id, f'{block_type}.json')
with self.storage.open(path, 'w') as json_file:
log.info(f'Writing to file {path}')
json.dump(items, json_file)

@property
def filter_path(self):
return os.path.join(settings.MLBF_STORAGE_PATH, self.id, 'filter')
return self.storage.path(self.id, 'filter')

@property
def _stash_path(self):
return os.path.join(settings.MLBF_STORAGE_PATH, self.id, 'stash.json')

@cached_property
def stash_json(self):
Expand Down Expand Up @@ -167,6 +146,7 @@ def generate_and_write_stash(self, previous_mlbf):
'blocked': list(extras),
'unblocked': list(deletes),
}
self.dump_json(self.stash_json, ext='json')
# write stash
stash_path = self._stash_path
with self.storage.open(stash_path, 'w') as json_file:
Expand Down Expand Up @@ -225,6 +205,10 @@ def blocked_items(self):
self._version_excludes = blocked_ids_to_versions.keys()
return list(self.hash_filter_inputs(blocked))

@cached_property
def soft_blocked_items(self):
pass

@cached_property
def not_blocked_items(self):
# see blocked_items - we need self._version_excludes populated
Expand Down
5 changes: 5 additions & 0 deletions src/olympia/blocklist/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,17 @@ def get_blocks_from_guids(cls, guids):
class BlockType(Enum):
SOFT = 'soft'
HARD = 'hard'
class BlockVersionQuerySet(models.QuerySet):
def by_block_type(self, block_type: BlockType):
return self.filter(soft=block_type == BlockType.SOFT)

class BlockVersion(ModelBase):
version = models.OneToOneField(Version, on_delete=models.CASCADE)
block = models.ForeignKey(Block, on_delete=models.CASCADE)
soft = models.BooleanField(default=False)

objects = BlockVersionQuerySet().as_manager()

def __str__(self) -> str:
return f'Block.id={self.block_id} ({self.block_type}) -> Version.id={self.version_id}'

Expand Down

0 comments on commit 9056990

Please sign in to comment.