# Path: kano_profile/tracker/tracking_sessions.py
# 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 session_end(session_file):
#     if not os.path.exists(session_file):
#         msg = "Someone removed the tracker file, the runtime of this " \
#               "app will not be logged"
#         logger.warn(msg)
#         return
# 
#     try:
#         rf = open_locked(session_file, 'r')
#     except IOError as e:
#         logger.error("Error opening the tracker session file {}".format(e))
#     else:
#         with rf:
#             data = json.load(rf)
# 
#             data['elapsed'] = int(time.time()) - data['started']
#             data['finished'] = True
# 
#             try:
#                 wf = open(session_file, 'w')
#             except IOError as e:
#                 logger.error(
#                     "Error opening the tracker session file {}".format(e))
#             else:
#                 with wf:
#                     json.dump(data, wf)
#         if 'SUDO_USER' in os.environ:
#             chown_path(session_file)


import atexit
from kano.utils.processes import get_program_name
from kano_profile.tracker.tracking_sessions import session_start, session_end
#
# tracker.py
#
# Copyright (C) 2014 - 2017 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#
# Class for handling tracking sessions
#





class Tracker(object):
    """Tracker class, used for measuring program run-times,
    implemented via atexit hooks"""

    def __init__(self):
        self.path = session_start(get_program_name())
        atexit.register(self._write_times)

    def _write_times(self):

# Path: kano_profile/tracker/tracking_sessions.py
# 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 session_end(session_file):
#     if not os.path.exists(session_file):
#         msg = "Someone removed the tracker file, the runtime of this " \
#               "app will not be logged"
#         logger.warn(msg)
#         return
# 
#     try:
#         rf = open_locked(session_file, 'r')
#     except IOError as e:
#         logger.error("Error opening the tracker session file {}".format(e))
#     else:
#         with rf:
#             data = json.load(rf)
# 
#             data['elapsed'] = int(time.time()) - data['started']
#             data['finished'] = True
# 
#             try:
#                 wf = open(session_file, 'w')
#             except IOError as e:
#                 logger.error(
#                     "Error opening the tracker session file {}".format(e))
#             else:
#                 with wf:
#                     json.dump(data, wf)
#         if 'SUDO_USER' in os.environ:
#             chown_path(session_file)


# Path: kano_profile/tracker/tracker.py
import atexit
from kano.utils.processes import get_program_name
from kano_profile.tracker.tracking_sessions import session_start, session_end

#
# tracker.py
#
# Copyright (C) 2014 - 2017 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#
# Class for handling tracking sessions
#





class Tracker(object):
    """Tracker class, used for measuring program run-times,
    implemented via atexit hooks"""

    def __init__(self):
        self.path = session_start(get_program_name())
        atexit.register(self._write_times)

    def _write_times(self):

        session_end(self.path)