# 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/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')


import os
import time
import json
from uuid import uuid1, uuid5
from kano.utils.file_operations import chown_path, touch
from kano.logging import logger
from kano_profile.tracker.tracking_utils import open_locked
from kano_profile.paths import TRACKER_UUIDS_PATH

    with uuids_file:
        if key in data:
            data.pop(key)
            uuids_file.seek(0)
            uuids_file.truncate(0)
            uuids_file.write(json.dumps(data))

    if 'SUDO_USER' in os.environ:
        chown_path(TRACKER_UUIDS_PATH)

    return True


def _open_uuids():
    """Helper function to open the uuids file and load the JSON inside.

    Returns:
        file, dict: The opened uuids file and its JSON data.
    """

    newly_created = False
    uuids_file = None
    data = dict()

    if not os.path.exists(TRACKER_UUIDS_PATH):
        touch(TRACKER_UUIDS_PATH)
        newly_created = True

    try:

# 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/paths.py
# TRACKER_UUIDS_PATH = os.path.join(kanoprofile_dir, 'tracker', 'uuids')


# Path: kano_profile/tracker/tracking_uuids.py
import os
import time
import json
from uuid import uuid1, uuid5
from kano.utils.file_operations import chown_path, touch
from kano.logging import logger
from kano_profile.tracker.tracking_utils import open_locked
from kano_profile.paths import TRACKER_UUIDS_PATH


    with uuids_file:
        if key in data:
            data.pop(key)
            uuids_file.seek(0)
            uuids_file.truncate(0)
            uuids_file.write(json.dumps(data))

    if 'SUDO_USER' in os.environ:
        chown_path(TRACKER_UUIDS_PATH)

    return True


def _open_uuids():
    """Helper function to open the uuids file and load the JSON inside.

    Returns:
        file, dict: The opened uuids file and its JSON data.
    """

    newly_created = False
    uuids_file = None
    data = dict()

    if not os.path.exists(TRACKER_UUIDS_PATH):
        touch(TRACKER_UUIDS_PATH)
        newly_created = True

    try:

        uuids_file = open_locked(TRACKER_UUIDS_PATH, 'r+')