Skip to content

Commit

Permalink
update requirements (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldruschk authored Apr 23, 2021
1 parent 3a8c10b commit 66942a4
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 104 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
lint:
python3 -m isort -rc -c src/ tests/ example/
python3 -m isort -c src/ tests/ example/
python3 -m black --check src/ tests/ example/
python3 -m flake8 src/ tests/ example/
python3 -m mypy src/ tests/ example/
Expand All @@ -9,7 +9,7 @@ diff:
python3 -m black --diff src/ tests/ example/

format:
python3 -m isort -rc src/ tests/ example/
python3 -m isort src/ tests/ example/
python3 -m black src/ tests/ example/

test:
Expand Down
34 changes: 17 additions & 17 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
appdirs==1.4.4
atomicwrites==1.4.0
attrs==19.3.0
black==19.10b0
attrs==20.3.0
black==20.8b1
click==7.1.2
coverage==5.1
flake8==3.8.2
isort==4.3.21
coverage==5.5
flake8==3.9.1
iniconfig==1.1.1
isort==5.8.0
mccabe==0.6.1
more-itertools==8.3.0
mypy==0.780
mypy==0.812
mypy-extensions==0.4.3
pathspec==0.8.0
packaging==20.9
pathspec==0.8.1
pluggy==0.13.1
py==1.10.0
pycodestyle==2.6.0
pyflakes==2.2.0
pytest==4.4.2
regex==2020.5.14
six==1.15.0
toml==0.10.1
typed-ast==1.4.1
typing-extensions==3.7.4.2
pycodestyle==2.7.0
pyflakes==2.3.1
pyparsing==2.4.7
pytest==6.2.3
regex==2021.4.4
toml==0.10.2
typed-ast==1.4.3
typing-extensions==3.7.4.3
24 changes: 8 additions & 16 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
atomicwrites==1.4.0
attrs==19.3.0
blinker==1.4
certifi==2019.3.9
chardet==3.0.4
certifi==2020.12.5
chardet==4.0.0
click==7.1.2
colorama==0.4.1
enochecker-cli==0.6.0
enochecker-core==0.8.1
Flask==1.0.3
idna==2.8
Flask==1.1.2
idna==2.10
itsdangerous==1.1.0
Jinja2==2.11.3
jsons==1.4.2
MarkupSafe==1.1.1
psutil==5.6.6
pymongo==3.8.0
requests==2.21.0
six==1.15.0
pymongo==3.11.3
requests==2.25.1
typish==1.9.2
urllib3==1.24.3
uwsgidecorators==1.1.0
websocket-client==0.56.0
Werkzeug==0.15.5
urllib3==1.26.4
Werkzeug==1.0.1
6 changes: 4 additions & 2 deletions src/enochecker/enochecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ def __init__(

self.debug(
"Initialized checker for task {} in {} seconds".format(
jsons.dumps(task), datetime.datetime.now() - self.time_started_at,
jsons.dumps(task),
datetime.datetime.now() - self.time_started_at,
)
)

Expand Down Expand Up @@ -773,7 +774,8 @@ def http(


def run(
checker_cls: Type[BaseChecker], args: Optional[Sequence[str]] = None,
checker_cls: Type[BaseChecker],
args: Optional[Sequence[str]] = None,
) -> Optional[CheckerResult]:
"""
Run a checker, either from cmdline or as uwsgi script.
Expand Down
4 changes: 3 additions & 1 deletion src/enochecker/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class EnoException(Exception, ABC):
result: CheckerTaskResult = CheckerTaskResult.CHECKER_TASK_RESULT_INTERNAL_ERROR

def __init__(
self, message: Optional[str], internal_message: Optional[str] = None,
self,
message: Optional[str],
internal_message: Optional[str] = None,
):
self.message: Optional[str] = message
self.internal_message: Optional[str] = internal_message
Expand Down
72 changes: 11 additions & 61 deletions src/enochecker/storeddict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Backend for team_db based on a local filesystem directory."""

import atexit
import json
import logging
import os
Expand All @@ -11,11 +10,9 @@
from pathlib import Path
from typing import Any, Callable, Dict, Iterator, Optional, Set

from .utils import base64ify, debase64ify, ensure_valid_filename, start_daemon
from .utils import base64ify, debase64ify, ensure_valid_filename

logging.basicConfig(level=logging.DEBUG)
dictlogger = logging.Logger(__name__)
dictlogger.setLevel(logging.DEBUG)

DB_DEFAULT_DIR = os.path.join(
os.getcwd(), ".data"
Expand Down Expand Up @@ -86,7 +83,6 @@ def __init__(
self,
name: str = "default",
base_path: str = DB_DEFAULT_DIR,
persist_secs: int = 3,
ignore_locks: bool = False,
logger: Optional[logging.Logger] = None,
*args: Any,
Expand All @@ -99,61 +95,31 @@ def __init__(
:param name: name of this store
:param base_path: the base path
:param persist_secs: how often to persist dirty elements (0 means: never autostore. Call persist manually)
:param ignore_locks: We usually write and read lock files before accessing the data.
This flag seaves them out.
:param logger: The logger instance to log events to
"""
self._cache: Dict[str, Any] = {}
self._dirties: Set[str] = set()
self._cache: Dict[str, Any] = dict()
self._locks: Set[str] = set()
self._to_delete: Set[str] = set()
self._persist_thread: Optional[threading.Thread] = None
self.name = name
self.path: str = os.path.join(base_path, ensure_valid_filename(name))
self.persist_secs: int = persist_secs
self.ignore_locks: bool = ignore_locks

if logger:
self.logger = logger
else:
self.logger = dictlogger
self.logger = logging.Logger(__name__)
self.logger.setLevel(logging.DEBUG)

self._local_lock = threading.RLock()

makedirs(self.path, exist_ok=True)
atexit.register(self._cleanup)
self._stopping = False

self.update(
dict(*args, **kwargs)
) # In case we got initialized using a dict, make sure it's in sync.

@_locked
def _spawn_persist_thread(self) -> None:
"""Spawn a thread persisting all changes, if necessary."""
if self.persist_secs > 0 and not self._persist_thread:

def persist_async() -> None:
time.sleep(self.persist_secs)
self.logger.debug("Persisting db {} from background.".format(self.name))
self.persist()

self._persist_thread = start_daemon(persist_async)

@_locked
def _cleanup(self) -> None:
"""Clean up the db: persists and releases all locks currently held."""
self.logger.debug("StoredDict cleanup task running.")
self._stopping = True
self.persist()
for lock in self._locks:
self.release(lock)

def __del__(self) -> None:
"""Delete a key."""
self._cleanup()

def _dir(self, key: str) -> str:
"""
Return the path where data for this key is stored.
Expand Down Expand Up @@ -256,8 +222,7 @@ def reload(self) -> None:
Non persisted changes might be lost.
Only reason would be if another process fiddles with our data concurrently.
"""
self._cache = {}
self._dirties = set()
self._cache = dict()
self._to_delete = set()

@_locked
Expand All @@ -277,7 +242,7 @@ def persist(self) -> None:
self.logger.debug(f"Deleted {key} from db {self.name}")
self._to_delete = set()

for key in self._dirties:
for key in self._cache.keys():
locked = self.is_locked(key)
if not locked:
self.lock(key)
Expand All @@ -288,7 +253,7 @@ def persist(self) -> None:
if not locked:
self.release(key)
self.logger.debug(f"Set {key} in db {self.name}")
self._dirties = set()
self._cache = dict()

@_locked
def __getitem__(self, key: str) -> Any:
Expand All @@ -298,13 +263,6 @@ def __getitem__(self, key: str) -> Any:
:param key: the key to look up
:return: the value
"""
if key in self._to_delete:
raise KeyError("Key was marked for deletion: {}".format(key))
try:
return self._cache[key]
except KeyError:
self.logger.debug("Hitting disk for {}".format(key))

locked = self.is_locked(key) or self.ignore_locks
if not locked:
self.lock(key)
Expand All @@ -317,40 +275,35 @@ def __getitem__(self, key: str) -> Any:
if not locked:
self.release(key)

self._cache[key] = val
return val

@_locked
def __setitem__(self, key: str, value: Any) -> None:
"""
Set an item. It'll be stored to disk on the next persist.
Set an item.
:param key: Key to store
:param value: Value to store
"""
if key in self._to_delete:
self._to_delete.remove(key)
self._cache[key] = value
self._dirties.add(key)
self.persist()

@_locked
def __delitem__(self, key: str) -> None:
"""
Delete an item. It will be deleted from disk on the next .persist().
Delete an item.
:param key: the key to delete
"""
self._to_delete.add(key)
self.persist()

def __iter__(self) -> Iterator[str]:
"""
Return an iterator over the dict.
Implicitly persisting the data before reading.
:return: An iterator containing all keys to a dict.
"""
self.persist()
keys = [
debase64ify(x[len(DB_PREFIX) : -len(DB_EXTENSION)], b"+-")
for x in os.listdir(self.path)
Expand All @@ -362,11 +315,8 @@ def __len__(self) -> int:
"""
Calculate the length of the dict.
Implicitly calls persist.
:return: the the number of elements
"""
self.persist()
keys = [
x[len(DB_PREFIX) : -len(DB_EXTENSION)]
for x in os.listdir(self.path)
Expand Down
6 changes: 4 additions & 2 deletions tests/test_enochecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import time
from logging import DEBUG

import enochecker
import pytest
from enochecker_core import CheckerMethod, CheckerTaskMessage, CheckerTaskResult

import enochecker
from enochecker import (
BaseChecker,
BrokenServiceException,
Expand All @@ -18,7 +20,6 @@
serve_once,
snake_caseify,
)
from enochecker_core import CheckerMethod, CheckerTaskMessage, CheckerTaskResult

STORAGE_DIR: str = "/tmp/enochecker_test"

Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(
round_length=60000,
task_chain_id=task_chain_id,
),
use_db_cache=False,
**kwargs,
)
self.logger.setLevel(DEBUG)
Expand Down
10 changes: 7 additions & 3 deletions tests/test_run_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import pytest
import requests
from enochecker_core import CheckerMethod, CheckerTaskMessage, CheckerTaskResult

from enochecker import BaseChecker, BrokenServiceException, OfflineException
from enochecker.results import CheckerResult
from enochecker_core import CheckerMethod, CheckerTaskMessage, CheckerTaskResult


@pytest.fixture()
Expand All @@ -21,7 +22,9 @@ class CheckerExampleImpl(BaseChecker):
havoc_variants = 1

def __init__(
self, method=CheckerMethod.CHECKER_METHOD_PUTFLAG, **kwargs,
self,
method=CheckerMethod.CHECKER_METHOD_PUTFLAG,
**kwargs,
):
"""
An mocked implementation of a checker for testing purposes
Expand All @@ -42,7 +45,8 @@ def __init__(
timeout=30000,
round_length=60000,
task_chain_id="test",
)
),
use_db_cache=False,
)
self.logger.setLevel(DEBUG)

Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

import pytest

from enochecker import BrokenServiceException
from enochecker.utils import (
assert_equals,
Expand Down

0 comments on commit 66942a4

Please sign in to comment.