Skip to content

Commit

Permalink
Add helper methods for updating user agent
Browse files Browse the repository at this point in the history
- Adds a wrapper method to add a metadata component
- Changes the behavior when adding the command lineage
metadata to not assume that it comes at the end of the
string.
  • Loading branch information
kdaily committed May 6, 2024
1 parent f5fa323 commit c319aa0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
40 changes: 15 additions & 25 deletions awscli/clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sys
import copy
import logging
import re

import distro
import botocore.session
Expand All @@ -27,7 +28,6 @@
from botocore.configprovider import ScopedConfigProvider
from botocore.configprovider import ConstantProvider
from botocore.configprovider import ChainProvider
from botocore.useragent import UserAgentComponent, RawStringUserAgentComponent

from awscli import __version__
from awscli.compat import (
Expand Down Expand Up @@ -56,7 +56,10 @@
set_stream_logger, remove_stream_logger, enable_crt_logging,
disable_crt_logging,
)
from awscli.utils import add_component_to_user_agent_extra
from awscli.utils import (
add_metadata_component_to_user_agent_extra,
add_command_lineage_to_user_agent_extra
)
from awscli.utils import emit_top_level_args_parsed_event
from awscli.utils import OutputStreamFactory
from awscli.utils import IMDSRegionProvider
Expand Down Expand Up @@ -137,20 +140,19 @@ def _get_linux_distribution():


def _add_distribution_source_to_user_agent(session):
add_component_to_user_agent_extra(
add_metadata_component_to_user_agent_extra(
session,
UserAgentComponent(
'md',
'installer',
_get_distribution_source())
'installer',
_get_distribution_source()
)


def _add_linux_distribution_to_user_agent(session):
if linux_distribution := _get_distribution():
add_component_to_user_agent_extra(
add_metadata_component_to_user_agent_extra(
session,
UserAgentComponent('md', 'distrib', linux_distribution)
'distrib',
linux_distribution,
)


Expand Down Expand Up @@ -189,9 +191,10 @@ def main(self, args):
return rc

def _add_autoprompt_to_user_agent(self, driver, prompt_mode):
add_component_to_user_agent_extra(
add_metadata_component_to_user_agent_extra(
driver.session,
UserAgentComponent("md", "prompt", prompt_mode)
"prompt",
prompt_mode,
)

def _run_driver(self, driver, args, prompt_mode):
Expand Down Expand Up @@ -918,20 +921,7 @@ def _create_operation_parser(self, arg_table, subcommand_table):
return parser

def _add_customization_to_user_agent(self):
if ' md/command#' in self._session.user_agent_extra:
add_component_to_user_agent_extra(
self._session,
RawStringUserAgentComponent(f".{self.lineage_names[-1]}")
)
else:
add_component_to_user_agent_extra(
self._session,
UserAgentComponent(
"md",
"command",
'.'.join(self.lineage_names)
)
)
add_command_lineage_to_user_agent_extra(self._session, self.lineage_names)


class CLIOperationCaller(object):
Expand Down
18 changes: 2 additions & 16 deletions awscli/customizations/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from botocore import model
from botocore.compat import OrderedDict
from botocore.validate import validate_parameters
from botocore.useragent import UserAgentComponent, RawStringUserAgentComponent

import awscli
from awscli.argparser import ArgTableArgParser, SubCommandArgParser
Expand All @@ -16,7 +15,7 @@
from awscli.bcdoc import docevents
from awscli.help import HelpCommand
from awscli.schema import SchemaTransformer
from awscli.utils import add_component_to_user_agent_extra
from awscli.utils import add_command_lineage_to_user_agent_extra
from awscli.customizations.exceptions import ParamValidationError

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -326,20 +325,7 @@ def _raise_usage_error(self):
raise ParamValidationError(error_msg)

def _add_customization_to_user_agent(self):
if ' md/command#' in self._session.user_agent_extra:
add_component_to_user_agent_extra(
self._session,
RawStringUserAgentComponent(f".{self.lineage_names[-1]}")
)
else:
add_component_to_user_agent_extra(
self._session,
UserAgentComponent(
"md",
"command",
'.'.join(self.lineage_names)
)
)
add_command_lineage_to_user_agent_extra(self._session, self.lineage_names)


class BasicHelp(HelpCommand):
Expand Down
22 changes: 22 additions & 0 deletions awscli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from awscli.compat import get_stdout_text_writer
from awscli.compat import get_popen_kwargs_for_pager_cmd
from awscli.compat import StringIO
from botocore.useragent import UserAgentComponent
from botocore.utils import resolve_imds_endpoint_mode
from botocore.utils import IMDSFetcher
from botocore.configprovider import BaseProvider
Expand Down Expand Up @@ -545,3 +546,24 @@ def add_component_to_user_agent_extra(session, component):
if session.user_agent_extra and not session.user_agent_extra.endswith(" "):
session.user_agent_extra += " "
session.user_agent_extra += f"{component.to_string()}"

def add_metadata_component_to_user_agent_extra(session, name, value=None):
add_component_to_user_agent_extra(
session,
UserAgentComponent("md", name, value)
)


_LINEAGE_COMMAND_METADATA_REGEX = re.compile(r'md\/command#[\w\.]*')


def add_command_lineage_to_user_agent_extra(session, lineage):
# Find existing command metadata and append the last item in the lineage to
# it
if comp := _LINEAGE_COMMAND_METADATA_REGEX.search(session.user_agent_extra):
session.user_agent_extra = \
_LINEAGE_COMMAND_METADATA_REGEX.sub(
f"{comp.group()}.{lineage[0]}", session.user_agent_extra
)
else:
add_metadata_component_to_user_agent_extra(session, "command", ".".join(lineage))

0 comments on commit c319aa0

Please sign in to comment.