# Path: kano_profile/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')
# PAUSED_SESSIONS_FILE = os.path.join(kanoprofile_dir, '.paused_sessions')
# 
# Path: kano_profile/tracker/tracking_utils.py
# class open_locked(file):
#     """ A version of open with an exclusive lock to be used within
#         controlled execution statements.
#     """
#     def __init__(self, *args, **kwargs):
#         super(open_locked, self).__init__(*args, **kwargs)
#         fcntl.flock(self, fcntl.LOCK_EX)


import os
import time
import hashlib
from kano.utils.file_operations import ensure_dir, chown_path
from kano.logging import logger
from kano_profile.paths import tracker_dir, tracker_token_file, \
    tracker_events_file
from kano_profile.tracker.tracking_utils import open_locked
        of the OS.

        :returns: The token.
        :rtype: str
    """

    if os.path.exists(tracker_token_file):
        try:
            f = open_locked(tracker_token_file, 'r')
        except IOError as e:
            logger.error("Error opening tracker token file {}".format(e))
        else:
            with f:
                return f.read().strip()
    else:
        return generate_tracker_token()


def generate_tracker_token():
    """
        Generating the token is a simple md5hash of the current time.

        The token is saved to the `tracker_token_file`.

        :returns: The token.
        :rtype: str
    """

    token = hashlib.md5(str(time.time())).hexdigest()


# Path: kano_profile/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')
# PAUSED_SESSIONS_FILE = os.path.join(kanoprofile_dir, '.paused_sessions')
# 
# Path: kano_profile/tracker/tracking_utils.py
# class open_locked(file):
#     """ A version of open with an exclusive lock to be used within
#         controlled execution statements.
#     """
#     def __init__(self, *args, **kwargs):
#         super(open_locked, self).__init__(*args, **kwargs)
#         fcntl.flock(self, fcntl.LOCK_EX)


# Path: kano_profile/tracker/tracker_token.py
import os
import time
import hashlib
from kano.utils.file_operations import ensure_dir, chown_path
from kano.logging import logger
from kano_profile.paths import tracker_dir, tracker_token_file, \
    tracker_events_file
from kano_profile.tracker.tracking_utils import open_locked

        of the OS.

        :returns: The token.
        :rtype: str
    """

    if os.path.exists(tracker_token_file):
        try:
            f = open_locked(tracker_token_file, 'r')
        except IOError as e:
            logger.error("Error opening tracker token file {}".format(e))
        else:
            with f:
                return f.read().strip()
    else:
        return generate_tracker_token()


def generate_tracker_token():
    """
        Generating the token is a simple md5hash of the current time.

        The token is saved to the `tracker_token_file`.

        :returns: The token.
        :rtype: str
    """

    token = hashlib.md5(str(time.time())).hexdigest()


    ensure_dir(tracker_dir)