-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy path__init__.py
More file actions
64 lines (50 loc) · 1.77 KB
/
__init__.py
File metadata and controls
64 lines (50 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import logging
import random
import refinery
import string
import unittest
import contextlib
import os
from samples import SampleStore
@contextlib.contextmanager
def temporary_chwd(directory):
old = os.getcwd()
try:
os.chdir(directory)
yield directory
finally:
os.chdir(old)
class NameUnknownException(Exception):
def __init__(self, name):
super().__init__('could not resolve: {}'.format(name))
class TestBase(unittest.TestCase):
_STORE = SampleStore()
def ldu(self, name, *args, **kwargs):
import refinery.lib.loader
unit = refinery.lib.loader.load(name, *args, **kwargs)
if not unit.args.quiet:
unit.log_detach()
return unit
def generate_random_buffer(self, size):
return bytes(random.randrange(0, 0x100) for _ in range(size))
def generate_random_text(self, size):
return ''.join(string.printable[
random.randrange(0, len(string.printable))] for _ in range(size)).encode('UTF8')
def download_sample(self, sha256hash, key=None):
return self._STORE.get(sha256hash, key)
def setUp(self):
random.seed(0xBAADF00D) # guarantee deterministic 'random' buffers
logging.disable(logging.CRITICAL)
def assertContains(self, container, member, msg=None):
self.assertIn(member, container, msg)
@classmethod
def load_pipeline(cls, cmd: str, clear_cache=False) -> refinery.Unit:
from refinery.units import Unit, LogLevel
from refinery.lib.loader import load_pipeline
if clear_cache:
load_pipeline.cache_clear()
unit = pl = load_pipeline(cmd)
while isinstance(unit, Unit):
unit.log_level = LogLevel.DETACHED
unit = unit.source
return pl