Path: blob/develop/awscli/customizations/history/__init__.py
1567 views
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import os13import sys14import logging1516from botocore.history import get_global_history_recorder17from botocore.exceptions import ProfileNotFound1819from awscli.compat import sqlite320from awscli.customizations.commands import BasicCommand21from awscli.customizations.history.constants import HISTORY_FILENAME_ENV_VAR22from awscli.customizations.history.constants import DEFAULT_HISTORY_FILENAME23from awscli.customizations.history.db import DatabaseConnection24from awscli.customizations.history.db import DatabaseRecordWriter25from awscli.customizations.history.db import RecordBuilder26from awscli.customizations.history.db import DatabaseHistoryHandler27from awscli.customizations.history.show import ShowCommand28from awscli.customizations.history.list import ListCommand293031LOG = logging.getLogger(__name__)32HISTORY_RECORDER = get_global_history_recorder()333435def register_history_mode(event_handlers):36event_handlers.register(37'session-initialized', attach_history_handler)383940def register_history_commands(event_handlers):41event_handlers.register(42"building-command-table.main", add_history_commands)434445def attach_history_handler(session, parsed_args, **kwargs):46if _should_enable_cli_history(session, parsed_args):47LOG.debug('Enabling CLI history')4849history_filename = os.environ.get(50HISTORY_FILENAME_ENV_VAR, DEFAULT_HISTORY_FILENAME)51if not os.path.isdir(os.path.dirname(history_filename)):52os.makedirs(os.path.dirname(history_filename))5354connection = DatabaseConnection(history_filename)55writer = DatabaseRecordWriter(connection)56record_builder = RecordBuilder()57db_handler = DatabaseHistoryHandler(writer, record_builder)5859HISTORY_RECORDER.add_handler(db_handler)60HISTORY_RECORDER.enable()616263def _should_enable_cli_history(session, parsed_args):64if parsed_args.command == 'history':65return False66try:67scoped_config = session.get_scoped_config()68except ProfileNotFound:69# If the profile does not exist, cli history is definitely not70# enabled, but don't let the error get propagated as commands down71# the road may handle this such as the configure set command with72# a --profile flag set.73return False74has_history_enabled = scoped_config.get('cli_history') == 'enabled'75if has_history_enabled and sqlite3 is None:76if has_history_enabled:77sys.stderr.write(78'cli_history is enabled but sqlite3 is unavailable. '79'Unable to collect CLI history.\n'80)81return False82return has_history_enabled838485def add_history_commands(command_table, session, **kwargs):86command_table['history'] = HistoryCommand(session)878889class HistoryCommand(BasicCommand):90NAME = 'history'91DESCRIPTION = (92'Commands to interact with the history of AWS CLI commands ran '93'over time. To record the history of AWS CLI commands set '94'``cli_history`` to ``enabled`` in the ``~/.aws/config`` file. '95'This can be done by running:\n\n'96'``$ aws configure set cli_history enabled``'97)98SUBCOMMANDS = [99{'name': 'show', 'command_class': ShowCommand},100{'name': 'list', 'command_class': ListCommand}101]102103def _run_main(self, parsed_args, parsed_globals):104if parsed_args.subcommand is None:105raise ValueError("usage: aws [options] <command> <subcommand> "106"[parameters]\naws: error: too few arguments")107108109