Exemple #1
0
def init_persistent_system(obj, basedir=None):
    # a system object is needed in order to coopy work
    if not obj:
        raise Exception(CORE_LOG_PREFIX +
                "Must input a valid object if there's no snapshot files")

    # if obj is a class, change obj to an insance
    if isinstance(obj, type):
        obj = obj()

    validate_system(obj)

    # first step is to check basedir argument. if isn't defined
    # coopy will create a directory name based on system class
    if not basedir:
        basedir = fileutils.obj_to_dir_name(obj)

    # convert some string to a valid directory name
    basedir = fileutils.name_to_dir(basedir)

    system_data_path = os.getcwd()

    # check if basedir exists, if not, create it
    try:
        os.listdir(basedir)
    except os.error:
        os.mkdir(basedir)

    # if no snapshot files, create first one with a 'empty' system
    if not fileutils.last_snapshot_file(basedir):
        logger.info(CORE_LOG_PREFIX + "No snapshot files..")
        SnapshotManager(basedir).take_snapshot(obj)

    # measure restore time
    start = datetime.utcnow()
    logger.info(CORE_LOG_PREFIX + "coopy init....")

    # inject clock on system object
    inject(obj, '_clock', RecordClock())
    inject(obj, '_system_data_path', system_data_path)

    # RestoreHelper will recover a system state based on snapshots and
    # transations files extracting actions executed previously and
    # re-executing them again
    obj = restore(obj, basedir)

    end = datetime.utcnow()
    delta = end - start
    logger.info(CORE_LOG_PREFIX + "spent " + str(delta) + "microseconds")

    journal = DiskJournal(basedir, system_data_path)
    journal.setup()

    snapshot_manager = SnapshotManager(basedir)

    return CoopyProxy(obj, [journal], snapshot_manager=snapshot_manager)
Exemple #2
0
def test_validate_system():
    from datetime import datetime

    class BadSystem(object):
        def __init__(self):
            self.data = []

        def bad_method(self):
            now = datetime.now()
            self.data.append(now)

    with pytest.raises(PrevalentError) as error:
        validate_system(BadSystem())
Exemple #3
0
def test_validate_system_method_with_subscript():
    class GoodSystem(object):
        def __init__(self):
            self.data = []

        def ok_method(self):
            temp = []
            temp_el = temp[0]
            element = self.data[0]

        def ok2_method(self):
            os.urandom(16).encode('hex')
            return ''.join([])

    assert validate_system(GoodSystem())