Skip to content

Commit

Permalink
Remove Python 2 support from CLI (#5512)
Browse files Browse the repository at this point in the history
* Remove Python 2 support from CLI

* oops

* Update tox.ini

* test all

* .

* ok

* address

* nice
  • Loading branch information
ofek authored Jan 21, 2020
1 parent a6aa7e9 commit 4c22dea
Show file tree
Hide file tree
Showing 63 changed files with 501 additions and 610 deletions.
2 changes: 1 addition & 1 deletion datadog_checks_dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Using a virtual environment is recommended.
## Installation

`datadog-checks-dev` is distributed on [PyPI][6] as a universal wheel
and is available on Linux, macOS, and Windows, and supports Python 2.7/3.7+ and PyPy.
and is available on Linux, macOS, and Windows, and supports Python 3.7+ and PyPy.

```console
$ pip install "datadog-checks-dev[cli]"
Expand Down
9 changes: 3 additions & 6 deletions datadog_checks_dev/datadog_checks/dev/tooling/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import click

from ..compat import PermissionError
from ..utils import dir_exists
from .commands import ALL_COMMANDS
from .commands.console import CONTEXT_SETTINGS, echo_success, echo_waiting, echo_warning, set_color, set_debug
Expand Down Expand Up @@ -33,9 +32,7 @@ def ddev(ctx, core, extras, agent, here, color, quiet, debug):
# TODO: Remove IOError (and noqa: B014) when Python 2 is removed
# In Python 3, IOError have been merged into OSError
except (IOError, OSError, PermissionError): # noqa: B014
echo_warning(
'Unable to create config file located at `{}`. ' 'Please check your permissions.'.format(CONFIG_FILE)
)
echo_warning(f'Unable to create config file located at `{CONFIG_FILE}`. Please check your permissions.')

# Load and store configuration for sub-commands.
config = load_config()
Expand All @@ -52,8 +49,8 @@ def ddev(ctx, core, extras, agent, here, color, quiet, debug):
root = os.path.expanduser(config.get(repo_choice, ''))
if here or not dir_exists(root):
if not here and not quiet:
repo = 'datadog-agent' if repo_choice == 'agent' else 'integrations-{}'.format(repo_choice)
echo_warning('`{}` directory `{}` does not exist, defaulting to the current location.'.format(repo, root))
repo = 'datadog-agent' if repo_choice == 'agent' else f'integrations-{repo_choice}'
echo_warning(f'`{repo}` directory `{root}` does not exist, defaulting to the current location.')

root = os.getcwd()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
# Licensed under a 3-clause BSD style license (see LICENSE)
import json
import os
from collections import OrderedDict
from io import StringIO

import click
from six import StringIO, iteritems

from ....utils import read_file, write_file
from ...constants import get_agent_changelog, get_agent_release_requirements, get_root
Expand Down Expand Up @@ -41,7 +40,7 @@ def changelog(since, to, write, force):
agent_tags = get_agent_tags(since, to)

# store the changes in a mapping {agent_version --> {check_name --> current_version}}
changes_per_agent = OrderedDict()
changes_per_agent = {}

# to keep indexing easy, we run the loop off-by-one
for i in range(1, len(agent_tags)):
Expand All @@ -54,9 +53,9 @@ def changelog(since, to, write, force):
file_contents = git_show_file(req_file_name, agent_tags[i])
catalog_prev = parse_agent_req_file(file_contents)

changes_per_agent[current_tag] = OrderedDict()
changes_per_agent[current_tag] = {}

for name, ver in iteritems(catalog_now):
for name, ver in catalog_now.items():
# at some point in the git history, the requirements file erroneusly
# contained the folder name instead of the package name for each check,
# let's be resilient
Expand Down Expand Up @@ -86,27 +85,25 @@ def changelog(since, to, write, force):
check_changelog_url = 'https://github.com/DataDog/integrations-core/blob/master/{}/CHANGELOG.md'

# go through all the agent releases
for agent, version_changes in iteritems(changes_per_agent):
for agent, version_changes in changes_per_agent.items():
url = agent_changelog_url.format(agent.replace('.', '')) # Github removes dots from the anchor
changelog_contents.write('## Datadog Agent version [{}]({})\n\n'.format(agent, url))
changelog_contents.write(f'## Datadog Agent version [{agent}]({url})\n\n')

if not version_changes:
changelog_contents.write('* There were no integration updates for this version of the Agent.\n\n')
else:
for name, ver in iteritems(version_changes):
for name, ver in version_changes.items():
# get the "display name" for the check
manifest_file = os.path.join(get_root(), name, 'manifest.json')
if os.path.exists(manifest_file):
decoded = json.loads(read_file(manifest_file).strip(), object_pairs_hook=OrderedDict)
decoded = json.loads(read_file(manifest_file).strip())
display_name = decoded.get('display_name')
else:
display_name = name

breaking_notice = " **BREAKING CHANGE**" if ver[1] else ""
changelog_url = check_changelog_url.format(name)
changelog_contents.write(
'* {} [{}]({}){}\n'.format(display_name, ver[0], changelog_url, breaking_notice)
)
changelog_contents.write(f'* {display_name} [{ver[0]}]({changelog_url}){breaking_notice}\n')
# add an extra line to separate the release block
changelog_contents.write('\n')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
from io import StringIO

import click
from six import StringIO, iteritems

from ....utils import write_file
from ...constants import get_agent_integrations_file, get_agent_release_requirements
Expand Down Expand Up @@ -39,11 +39,11 @@ def integrations(since, to, write, force):

integrations_contents = StringIO()
for tag in agent_tags:
integrations_contents.write('## Datadog Agent version {}\n\n'.format(tag))
integrations_contents.write(f'## Datadog Agent version {tag}\n\n')
# Requirements for current tag
file_contents = git_show_file(req_file_name, tag)
for name, ver in iteritems(parse_agent_req_file(file_contents)):
integrations_contents.write('* {}: {}\n'.format(name, ver))
for name, ver in parse_agent_req_file(file_contents).items():
integrations_contents.write(f'* {name}: {ver}\n')
integrations_contents.write('\n')

# save the changelog on disk if --write was passed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ def requirements(ctx):
entries = []
for check in checks:
if check in AGENT_V5_ONLY:
echo_info('Check `{}` is only shipped with Agent 5, skipping'.format(check))
echo_info(f'Check `{check}` is only shipped with Agent 5, skipping')
continue

try:
version = get_version_string(check)
entries.append('{}\n'.format(get_agent_requirement_line(check, version)))
entries.append(f'{get_agent_requirement_line(check, version)}\n')
except Exception as e:
echo_failure('Error generating line: {}'.format(e))
echo_failure(f'Error generating line: {e}')
continue

lines = sorted(entries)

req_file = get_agent_release_requirements()
write_file_lines(req_file, lines)
echo_success('Successfully wrote to `{}`!'.format(req_file))
echo_success(f'Successfully wrote to `{req_file}`!')
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@click.command(context_settings=CONTEXT_SETTINGS, short_help="Remove a project's build artifacts")
@click.argument('check', required=False)
@click.option(
'--compiled-only', '-c', is_flag=True, help='Remove compiled files only ({}).'.format(', '.join(DELETE_EVERYWHERE)),
'--compiled-only', '-c', is_flag=True, help=f"Remove compiled files only ({', '.join(DELETE_EVERYWHERE)}).",
)
@click.option(
'--all',
Expand Down Expand Up @@ -54,7 +54,7 @@ def clean(ctx, check, compiled_only, all_matches, force, verbose):
path = os.getcwd()

if compiled_only:
echo_waiting('Cleaning compiled artifacts in `{}`...'.format(path))
echo_waiting(f'Cleaning compiled artifacts in `{path}`...')
removed_paths = remove_compiled_scripts(path, detect_project=not all_matches)
else:
force_clean_root = False
Expand All @@ -78,14 +78,14 @@ def clean(ctx, check, compiled_only, all_matches, force, verbose):
else:
target_description = 'artifacts (excluding those listed above)'

echo_waiting('Cleaning {} in `{}`...'.format(target_description, path))
echo_waiting(f'Cleaning {target_description} in `{path}`...')
removed_paths = clean_package(path, detect_project=not all_matches, force_clean_root=force_clean_root)

if verbose:
if removed_paths:
echo_success('Removed paths:')
for p in removed_paths:
echo_info(' {}'.format(p))
echo_info(f' {p}')

if removed_paths:
echo_success('Cleaned!')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def explore():
def find():
"""Show the location of the config file."""
if ' ' in CONFIG_FILE:
echo_info('"{}"'.format(CONFIG_FILE))
echo_info(f'"{CONFIG_FILE}"')
else:
echo_info(CONFIG_FILE)

Expand Down Expand Up @@ -86,7 +86,7 @@ def set_value(ctx, key, value):
scrubbing = False
if value is None:
scrubbing = any(fnmatch(key, pattern) for pattern in SECRET_KEYS)
value = click.prompt('Value for `{}`'.format(key), hide_input=scrubbing)
value = click.prompt(f'Value for `{key}`', hide_input=scrubbing)

if key in ('core', 'extras', 'agent') and not value.startswith('~'):
value = os.path.abspath(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def echo_debug(text, nl=True, cr=False, err=False, indent=None):
if not DEBUG_OUTPUT:
return

text = 'DEBUG: %s' % text
text = f'DEBUG: {text}'
if indent:
text = indent_text(text, indent)
if cr:
text = '\n%s' % text
text = f'\n{text}'
click.secho(text, bold=True, nl=nl, err=err, color=DISPLAY_COLOR)


Expand Down
20 changes: 9 additions & 11 deletions datadog_checks_dev/datadog_checks/dev/tooling/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ def tree():

def construct_output_info(path, depth, last, is_dir=False):
if depth == 0:
return u'', path, is_dir
return '', path, is_dir
else:
if depth == 1:
return (u'{}{} '.format(PIPE_END if last else PIPE_MIDDLE, HYPHEN), path, is_dir)
return (f'{PIPE_END if last else PIPE_MIDDLE}{HYPHEN} ', path, is_dir)
else:
return (
u'{} {}{}'.format(
PIPE, u' ' * 4 * (depth - 2), u'{}{} '.format(PIPE_END if last or is_dir else PIPE_MIDDLE, HYPHEN)
),
f"{PIPE} {' ' * 4 * (depth - 2)}{PIPE_END if last or is_dir else PIPE_MIDDLE}{HYPHEN} ",
path,
is_dir,
)
Expand Down Expand Up @@ -104,7 +102,7 @@ def create(ctx, name, integration_type, location, non_interactive, quiet, dry_ru

integration_dir = os.path.join(root, normalize_package_name(name))
if os.path.exists(integration_dir):
abort('Path `{}` already exists!'.format(integration_dir))
abort(f'Path `{integration_dir}` already exists!')

template_fields = {}
if repo_choice != 'core' and not non_interactive and not dry_run:
Expand All @@ -116,7 +114,7 @@ def create(ctx, name, integration_type, location, non_interactive, quiet, dry_ru
config = construct_template_fields(name, repo_choice, **template_fields)

files = create_template_files(integration_type, root, config, read=not dry_run)
file_paths = [file.file_path.replace('{}{}'.format(root, path_sep), '', 1) for file in files]
file_paths = [file.file_path.replace(f'{root}{path_sep}', '', 1) for file in files]

path_tree = tree()
for file_path in file_paths:
Expand All @@ -127,17 +125,17 @@ def create(ctx, name, integration_type, location, non_interactive, quiet, dry_ru

if dry_run:
if quiet:
echo_info('Will create `{}`'.format(integration_dir))
echo_info(f'Will create `{integration_dir}`')
else:
echo_info('Will create in `{}`:'.format(root))
echo_info(f'Will create in `{root}`:')
display_path_tree(path_tree)
return

for file in files:
file.write()

if quiet:
echo_info('Created `{}`'.format(integration_dir))
echo_info(f'Created `{integration_dir}`')
else:
echo_info('Created in `{}`:'.format(root))
echo_info(f'Created in `{root}`:')
display_path_tree(path_tree)
27 changes: 13 additions & 14 deletions datadog_checks_dev/datadog_checks/dev/tooling/commands/dep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os

import click
from six import itervalues

from ...utils import write_file_lines
from ..constants import REQUIREMENTS_IN, get_agent_requirements, get_root
Expand All @@ -30,22 +29,22 @@ def display_package_changes(pre_packages, post_packages, indent=''):
changed.append((pre_package_names[package_name], post_package_names[package_name]))

if not (added or removed or changed):
echo_info('{}No changes'.format(indent))
echo_info(f'{indent}No changes')

if added:
echo_success('{}Added packages:'.format(indent))
echo_success(f'{indent}Added packages:')
for package_name in sorted(added):
echo_info('{} {}'.format(indent, post_package_names[package_name]))
echo_info(f'{indent} {post_package_names[package_name]}')

if removed:
echo_failure('{}Removed packages:'.format(indent))
echo_failure(f'{indent}Removed packages:')
for package_name in sorted(removed):
echo_info('{} {}'.format(indent, pre_package_names[package_name]))
echo_info(f'{indent} {pre_package_names[package_name]}')

if changed:
echo_warning('{}Changed packages:'.format(indent))
echo_warning(f'{indent}Changed packages:')
for pre, post in changed:
echo_info('{} {} -> {}'.format(indent, pre, post))
echo_info(f'{indent} {pre} -> {post}')


@click.group(context_settings=CONTEXT_SETTINGS, short_help='Manage dependencies')
Expand All @@ -71,7 +70,7 @@ def resolve(checks, lazy, quiet):

if os.path.isfile(pinned_reqs_file):
if not quiet:
echo_info('Check `{}`:'.format(check_name))
echo_info(f'Check `{check_name}`:')

if not quiet:
echo_waiting(' Resolving dependencies...')
Expand Down Expand Up @@ -118,18 +117,18 @@ def pin(package, version, checks, marker, resolving, lazy, quiet):
if resolving:
pre_packages = list(read_packages(resolved_reqs_file))
else:
pre_packages = list(itervalues(pinned_packages))
pre_packages = list(pinned_packages.values())

if not quiet:
echo_info('Check `{}`:'.format(check_name))
echo_info(f'Check `{check_name}`:')

if version == 'none':
del pinned_packages[package_name]
else:
pinned_packages[package_name] = Package(package_name, version, marker)

package_list = sorted(itervalues(pinned_packages))
write_file_lines(pinned_reqs_file, ('{}\n'.format(package) for package in package_list))
package_list = sorted(pinned_packages.values())
write_file_lines(pinned_reqs_file, (f'{package}\n' for package in package_list))

if not quiet:
echo_waiting(' Resolving dependencies...')
Expand Down Expand Up @@ -158,7 +157,7 @@ def freeze():

static_file = get_agent_requirements()

echo_info('Static file: {}'.format(static_file))
echo_info(f'Static file: {static_file}')

pre_packages = list(read_packages(static_file))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ def check_run(check, env, rate, times, pause, delay, log_level, as_json, break_p
"""Run an Agent check."""
envs = get_configured_envs(check)
if not envs:
echo_failure('No active environments found for `{}`.'.format(check))
echo_info('See what is available to start via `ddev env ls {}`.'.format(check))
echo_failure(f'No active environments found for `{check}`.')
echo_info(f'See what is available to start via `ddev env ls {check}`.')
abort()

if not env:
if len(envs) > 1:
echo_failure('Multiple active environments found for `{}`, please specify one.'.format(check))
echo_failure(f'Multiple active environments found for `{check}`, please specify one.')
echo_info('See what is active via `ddev env ls`.')
abort()

env = envs[0]

if env not in envs:
echo_failure('`{}` is not an active environment.'.format(env))
echo_failure(f'`{env}` is not an active environment.')
echo_info('See what is active via `ddev env ls`.')
abort()

Expand Down
Loading

0 comments on commit 4c22dea

Please sign in to comment.