def test_pause_tracking_sessions(tracking_session, sample_tracking_sessions):
    import kano_profile.tracker.tracking_sessions as tracking_sessions
    from kano_profile.tracker.tracking_session import TrackingSession
    from kano_profile.paths import PAUSED_SESSIONS_FILE

    open_session = tracking_session.format_session('active-session', 987654321,
                                                   os.getpid(), 55, False)
    open_session_obj = TrackingSession(name=open_session['name'],
                                       pid=open_session['pid'])
    sample_tracking_sessions.append(open_session)
    tracking_session.setup_sessions(sample_tracking_sessions)
    tracking_session.setup_paused_sessions(None)

    assert tracking_sessions.get_open_sessions() == [open_session_obj]
    assert tracking_sessions.get_paused_sessions() == []

    tracking_sessions.pause_tracking_sessions()

    assert tracking_sessions.get_open_sessions() == []
    assert tracking_sessions.get_paused_sessions() == [open_session_obj]

    with open(PAUSED_SESSIONS_FILE, 'r') as paused_sessions_f:
        tracking_session = [line for line in paused_sessions_f if line]
        assert len(tracking_session) == 1
        assert TrackingSession.loads(tracking_session[0]) == open_session_obj
예제 #2
0
def get_open_sessions():
    open_sessions = []

    for session_file in list_sessions():
        session = TrackingSession(session_file=session_file)

        if not os.path.isfile(session.path):
            continue

        if not session.is_open():
            continue

        open_sessions.append(session)

    return open_sessions
예제 #3
0
def pause_tracking_session(session):
    '''
    Close session and make a note of the session if it is open so that it can
    be resumed.
    '''

    if session.is_open():
        try:
            sessions_f = open_locked(PAUSED_SESSIONS_FILE, 'a')
        except IOError as err:
            logger.error('Error opening the paused sessions file: {}'.format(err))
        else:
            with sessions_f:
                sessions_f.write(
                    '{}\n'.format(session.dumps())
                )

    session_end(session.path)

    closed_session = TrackingSession(name=session.name, pid=999999)
    shutil.move(
        session.path,
        '-{}'.format(time.time()).join(
            os.path.splitext(closed_session.path)
        )
    )
예제 #4
0
def test_unpause_tracking_sessions(tracking_session, sample_tracking_sessions):
    import kano_profile.tracker.tracking_sessions as tracking_sessions
    from kano_profile.tracker.tracking_session import TrackingSession
    from kano_profile.paths import tracker_dir

    sample_sessions = sample_tracking_sessions[:]
    open_session = tracking_session.format_session(
        'active-session', 987654321, os.getpid(), 55, False
    )
    open_session_obj = TrackingSession(
        name=open_session['name'], pid=open_session['pid']
    )
    sample_sessions.append(open_session)
    tracking_session.setup_sessions([])
    tracking_session.setup_paused_sessions(sample_sessions)

    tracking_sessions.unpause_tracking_sessions()

    assert tracking_sessions.get_open_sessions() == [open_session_obj]
    assert tracking_sessions.get_paused_sessions() == []

    for path, dummy_dirs, files in os.walk(tracker_dir):
        if path == tracker_dir:
            assert len(files) == 1
            assert files[0] == open_session_obj.file
예제 #5
0
def get_paused_sessions():
    if not os.path.exists(PAUSED_SESSIONS_FILE):
        return []

    try:
        sessions_f = open_locked(PAUSED_SESSIONS_FILE, 'r')
    except IOError as err:
        logger.error('Error opening the paused sessions file: {}'.format(err))
        return []
    else:
        with sessions_f:
            paused_sessions = []
            for session in sessions_f:
                if not session:
                    continue

                try:
                    new_session = TrackingSession.loads(session)
                except TypeError:
                    logger.warn(
                        'Failed to process session: {}'.format(session))
                    continue
                paused_sessions.append(new_session)

            return paused_sessions
def test_pause_tracking_sessions(tracking_session, sample_tracking_sessions):
    import kano_profile.tracker.tracking_sessions as tracking_sessions
    from kano_profile.tracker.tracking_session import TrackingSession
    from kano_profile.paths import PAUSED_SESSIONS_FILE

    open_session = tracking_session.format_session(
        'active-session', 987654321, os.getpid(), 55, False
    )
    open_session_obj = TrackingSession(
        name=open_session['name'], pid=open_session['pid']
    )
    sample_tracking_sessions.append(open_session)
    tracking_session.setup_sessions(sample_tracking_sessions)
    tracking_session.setup_paused_sessions(None)

    assert tracking_sessions.get_open_sessions() == [open_session_obj]
    assert tracking_sessions.get_paused_sessions() == []

    tracking_sessions.pause_tracking_sessions()

    assert tracking_sessions.get_open_sessions() == []
    assert tracking_sessions.get_paused_sessions() == [open_session_obj]

    with open(PAUSED_SESSIONS_FILE, 'r') as paused_sessions_f:
        tracking_session = [
            line for line in paused_sessions_f
            if line
        ]
        assert len(tracking_session) == 1
        assert TrackingSession.loads(tracking_session[0]) == open_session_obj
예제 #7
0
def session_start(name, pid=None, ignore_pause=False):
    if not pid:
        pid = os.getpid()
    pid = int(pid)

    if not ignore_pause and is_tracking_paused():
        session = TrackingSession(name=name, pid=pid)
        try:
            paused_sessions_f = open_locked(PAUSED_SESSIONS_FILE, 'a')
        except IOError as err:
            logger.error(
                'Error while opening the paused sessions file: {}'.format(err)
            )
        else:
            paused_sessions_f.write(
                '{}\n'.format(session.dumps())
            )

            return session.path

    data = {
        'pid': pid,
        'name': name,
        'started': int(time.time()),
        'elapsed': 0,
        'app_session_id': str(uuid5(uuid1(), name + str(pid))),
        'finished': False,
        'token-system': TOKEN
    }

    path = get_session_file_path(data['name'], data['pid'])

    try:
        f = open_locked(path, 'w')
    except IOError as e:
        logger.error("Error opening tracker session file {}".format(e))
    else:
        with f:
            json.dump(data, f)
        if 'SUDO_USER' in os.environ:
            chown_path(path)

    return path
def test_get_paused_sessions(tracking_session, sample_tracking_sessions):
    import kano_profile.tracker.tracking_sessions as tracking_sessions
    from kano_profile.tracker.tracking_session import TrackingSession

    tracking_session.setup_paused_sessions(sample_tracking_sessions)

    paused_sessions = tracking_sessions.get_paused_sessions()

    assert len(paused_sessions) == len(sample_tracking_sessions)

    for session in sample_tracking_sessions:
        session_obj = TrackingSession(name=session['name'], pid=session['pid'])
        assert session_obj in paused_sessions
예제 #9
0
def test_pid_name_init(tracking_session_file_data):
    from kano_profile.tracker.tracking_session import TrackingSession

    session_file = tracking_session_file_data['file']
    session_path = tracking_session_file_data['path']
    session_pid = tracking_session_file_data['pid']
    session_name = tracking_session_file_data['name']

    session = TrackingSession(pid=session_pid, name=session_name)

    assert session.file == session_file
    assert session.path == session_path
    assert session.pid == session_pid
    assert session.name == session_name
예제 #10
0
def tracking_session_check(ctx, proc_idx, secs):
    proc = ctx.procs[proc_idx]
    session = TrackingSession(name=proc.session_id, pid=proc.pid)

    for session_path in list_tracking_files():
        with open(session_path, 'r') as session_f:
            session_data = json.load(session_f)

        if session_data['name'] == session.name and \
                session_data['pid'] == session.pid:
            if session_data['elapsed'] == secs:
                assert True
                return

    assert False
예제 #11
0
    def setup_session_fixtures(cls, directory, sessions):
        from kano.utils.file_operations import ensure_dir
        from kano_profile.tracker.tracking_session import TrackingSession

        ensure_dir(directory)
        for session_f in os.listdir(directory):
            session_path = os.path.join(directory, session_f)
            if os.path.isfile(session_path):
                os.remove(session_path)

        for session in sessions:
            session_obj = TrackingSession(name=session['name'],
                                          pid=session['pid'])
            session_path = os.path.join(directory, session_obj.file)

            with open(session_path, 'w') as session_f:
                json.dump(session, session_f)
def test_session_start_no_pid(tracking_session):
    import kano_profile.tracker.tracking_sessions as tracking_sessions
    from kano_profile.tracker.tracking_session import TrackingSession

    tracking_session.setup_sessions([])
    tracking_session.setup_paused_sessions(None)

    test_session = TrackingSession(name='test', pid=os.getpid())
    session_path = tracking_sessions.session_start(test_session.name)

    assert os.path.abspath(session_path) == test_session.path

    with open(test_session.path, 'r') as test_session_f:
        session_data = json.load(test_session_f)

    assert test_session.name == session_data['name']
    assert test_session.pid == session_data['pid']
def get_paused_sessions():
    if not os.path.exists(PAUSED_SESSIONS_FILE):
        return []

    try:
        sessions_f = open_locked(PAUSED_SESSIONS_FILE, 'r')
    except IOError as err:
        logger.error('Error opening the paused sessions file: {}'.format(err))
        return []
    else:
        with sessions_f:
            paused_sessions = []
            for session in sessions_f:
                if not session:
                    continue

                try:
                    new_session = TrackingSession.loads(session)
                except TypeError:
                    logger.warn('Failed to process session: {}'.format(session))
                    continue
                paused_sessions.append(new_session)

            return paused_sessions