# Path: rash/utils/pathutils.py
# def mkdirp(path):
#     """
#     Make directory at `path` if it does not exist.
#     """
#     if not os.path.isdir(path):
#         os.makedirs(path)
# 
# Path: rash/utils/py3compat.py
# PY3 = (sys.version_info[0] >= 3)
#     def nested(*managers):
# 
# Path: rash/config.py
# class ConfigStore(object):
# 
#     """
#     Configuration and data file store.
# 
#     RASH stores data in the following directory in Linux::
# 
#       * ~/.config/               # $XDG_CONFIG_HOME
#       `--* rash/                 # base_path
#          |--* daemon.pid         # PID of daemon process
#          |--* daemon.log         # Log file for daemon
#          `--* data/              # data_path
#             |--* db.sqlite       # db_path ("indexed" record)
#             `--* record/         # record_path ("raw" record)
#                |--* command/     # command log
#                `--* init/        # initialization log
# 
#     In Mac OS and Windows, :attr:`base_path` may be different but
#     structure in the directory is the same.
# 
#     """
# 
#     def __init__(self, base_path=None):
#         self.base_path = base_path or get_config_directory('RASH')
#         """
#         Root directory for any RASH related data files (``~/.config/rash``).
#         """
# 
#         self.config_path = os.path.join(self.base_path, 'config.py')
#         """
#         File to store user configuration (``~/.config/rash/config.py``).
#         """
# 
#         self.data_path = os.path.join(self.base_path, 'data')
#         """
#         Directory to store data collected by RASH (``~/.config/rash/data``).
#         """
# 
#         self.record_path = os.path.join(self.data_path, 'record')
#         """
#         Shell history is stored in this directory at the first stage.
#         """
# 
#         self.db_path = os.path.join(self.data_path, 'db.sqlite')
#         """
#         Shell history is stored in the DB at this path.
#         """
# 
#         self.daemon_pid_path = os.path.join(self.base_path, 'daemon.pid')
#         """
#         A file to store daemon PID (``~/.config/rash/daemon.pid``).
#         """
# 
#         self.daemon_log_path = os.path.join(self.base_path, 'daemon.log')
#         """
#         Daemon log file (``~/.config/rash/daemon.log``).
#         """
# 
#         self.daemon_log_level = 'INFO'  # FIXME: make this configurable
#         """
#         Daemon log level.
#         """
# 
#         mkdirp(self.record_path)
# 
#     def get_config(self):
#         """
#         Load user configuration or return default when not found.
# 
#         :rtype: :class:`Configuration`
# 
#         """
#         if not self._config:
#             namespace = {}
#             if os.path.exists(self.config_path):
#                 execfile(self.config_path, namespace)
#             self._config = namespace.get('config') or Configuration()
#         return self._config
#     _config = None


import os
import time
import json
        import platform
from .utils.pathutils import mkdirp
from .utils.py3compat import getcwd
from .config import ConfigStore
        from .utils.termdetection import detect_terminal
    return ':'.join(map(str, [
        host, tty, os.getppid(), data['start']]))


def record_run(record_type, print_session_id, **kwds):
    """
    Record shell history.
    """
    if print_session_id and record_type != 'init':
        raise RuntimeError(
            '--print-session-id should be used with --record-type=init')

    cfstore = ConfigStore()
    # SOMEDAY: Pass a list of environment variables to shell by "rash
    # init" and don't read configuration in "rash record" command.  It
    # is faster.
    config = cfstore.get_config()
    envkeys = config.record.environ[record_type]
    json_path = os.path.join(cfstore.record_path,
                             record_type,
                             time.strftime('%Y-%m-%d-%H%M%S.json'))
    mkdirp(os.path.dirname(json_path))

    # Command line options directly map to record keys
    data = dict((k, v) for (k, v) in kwds.items() if v is not None)
    data.update(
        environ=get_environ(envkeys),
    )

    # Automatically set some missing variables:

# Path: rash/utils/pathutils.py
# def mkdirp(path):
#     """
#     Make directory at `path` if it does not exist.
#     """
#     if not os.path.isdir(path):
#         os.makedirs(path)
# 
# Path: rash/utils/py3compat.py
# PY3 = (sys.version_info[0] >= 3)
#     def nested(*managers):
# 
# Path: rash/config.py
# class ConfigStore(object):
# 
#     """
#     Configuration and data file store.
# 
#     RASH stores data in the following directory in Linux::
# 
#       * ~/.config/               # $XDG_CONFIG_HOME
#       `--* rash/                 # base_path
#          |--* daemon.pid         # PID of daemon process
#          |--* daemon.log         # Log file for daemon
#          `--* data/              # data_path
#             |--* db.sqlite       # db_path ("indexed" record)
#             `--* record/         # record_path ("raw" record)
#                |--* command/     # command log
#                `--* init/        # initialization log
# 
#     In Mac OS and Windows, :attr:`base_path` may be different but
#     structure in the directory is the same.
# 
#     """
# 
#     def __init__(self, base_path=None):
#         self.base_path = base_path or get_config_directory('RASH')
#         """
#         Root directory for any RASH related data files (``~/.config/rash``).
#         """
# 
#         self.config_path = os.path.join(self.base_path, 'config.py')
#         """
#         File to store user configuration (``~/.config/rash/config.py``).
#         """
# 
#         self.data_path = os.path.join(self.base_path, 'data')
#         """
#         Directory to store data collected by RASH (``~/.config/rash/data``).
#         """
# 
#         self.record_path = os.path.join(self.data_path, 'record')
#         """
#         Shell history is stored in this directory at the first stage.
#         """
# 
#         self.db_path = os.path.join(self.data_path, 'db.sqlite')
#         """
#         Shell history is stored in the DB at this path.
#         """
# 
#         self.daemon_pid_path = os.path.join(self.base_path, 'daemon.pid')
#         """
#         A file to store daemon PID (``~/.config/rash/daemon.pid``).
#         """
# 
#         self.daemon_log_path = os.path.join(self.base_path, 'daemon.log')
#         """
#         Daemon log file (``~/.config/rash/daemon.log``).
#         """
# 
#         self.daemon_log_level = 'INFO'  # FIXME: make this configurable
#         """
#         Daemon log level.
#         """
# 
#         mkdirp(self.record_path)
# 
#     def get_config(self):
#         """
#         Load user configuration or return default when not found.
# 
#         :rtype: :class:`Configuration`
# 
#         """
#         if not self._config:
#             namespace = {}
#             if os.path.exists(self.config_path):
#                 execfile(self.config_path, namespace)
#             self._config = namespace.get('config') or Configuration()
#         return self._config
#     _config = None


# Path: rash/record.py
import os
import time
import json
        import platform
from .utils.pathutils import mkdirp
from .utils.py3compat import getcwd
from .config import ConfigStore
        from .utils.termdetection import detect_terminal

    return ':'.join(map(str, [
        host, tty, os.getppid(), data['start']]))


def record_run(record_type, print_session_id, **kwds):
    """
    Record shell history.
    """
    if print_session_id and record_type != 'init':
        raise RuntimeError(
            '--print-session-id should be used with --record-type=init')

    cfstore = ConfigStore()
    # SOMEDAY: Pass a list of environment variables to shell by "rash
    # init" and don't read configuration in "rash record" command.  It
    # is faster.
    config = cfstore.get_config()
    envkeys = config.record.environ[record_type]
    json_path = os.path.join(cfstore.record_path,
                             record_type,
                             time.strftime('%Y-%m-%d-%H%M%S.json'))
    mkdirp(os.path.dirname(json_path))

    # Command line options directly map to record keys
    data = dict((k, v) for (k, v) in kwds.items() if v is not None)
    data.update(
        environ=get_environ(envkeys),
    )

    # Automatically set some missing variables:

    data.setdefault('cwd', getcwd())