From 2275a5343281356d61e72cd164a0f4ebae0fada2 Mon Sep 17 00:00:00 2001 From: Mark Meyer Date: Fri, 13 Jul 2018 23:50:11 +0200 Subject: [PATCH] Add support for Python 3 --- dogpush/dogpush.py | 91 ++++++++++++++++++++++++---------------------- requirements.txt | 2 +- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/dogpush/dogpush.py b/dogpush/dogpush.py index 403eddd..7df0314 100755 --- a/dogpush/dogpush.py +++ b/dogpush/dogpush.py @@ -1,5 +1,8 @@ #!/usr/bin/env python +from __future__ import print_function +from six import string_types + import argparse import calendar import copy @@ -15,7 +18,7 @@ import pytz import yaml -import bcolors +from .bcolors import * PROGNAME = 'dogpush' @@ -97,8 +100,8 @@ def _canonical_monitor(original, default_team=None, **kwargs): m.pop(field, None) for field in IGNORE_OPTIONS: m.get('options', {}).pop(field, None) - option_defaults = (DATADOG_DEFAULT_OPTIONS.items() + - CONFIG['default_rule_options'].items()) + option_defaults = (list(DATADOG_DEFAULT_OPTIONS.items()) + + list(CONFIG['default_rule_options'].items())) for (field, value) in option_defaults: if m.get('options', {}).get(field) == value: del m['options'][field] @@ -114,7 +117,7 @@ def _canonical_monitor(original, default_team=None, **kwargs): original_team = original.get('team') team = original_team if original_team is not None else default_team if team: - if isinstance(team, basestring): + if isinstance(team, string_types): team = [team] m['message'] = m.get('message', '') dogpush_line = get_notify_string(team) @@ -181,14 +184,14 @@ def _check_monitor_names_unique(monitors): for name in names: counts[name] = counts.get(name, 0) + 1 if counts[name] > 1: - print >> sys.stderr, "Duplicate name: %s" % name + print("Duplicate name: %s" % name, file=sys.stderr) return False return True def _check_monitor(monitor, location): name = monitor.get('name', '') - if isinstance(name, basestring): + if isinstance(name, string_types): name = name.strip() if not name: raise DogPushException('%s: found monitor without a name' % location) @@ -244,9 +247,9 @@ def _is_changed(local, remote): def command_init(args): remote_monitors = [m['obj'] for m in get_datadog_monitors().values()] monitors = {'alerts': remote_monitors} - print '# team: TEAMNAME' - print - print _pretty_yaml(monitors) + print('# team: TEAMNAME') + print('') + print(_pretty_yaml(monitors)) def command_push(args): @@ -255,7 +258,7 @@ def command_push(args): only_local = set(local_monitors.keys()) - set(remote_monitors.keys()) if only_local: - print "Pushing %d new monitors." % len(only_local) + print("Pushing %d new monitors." % len(only_local)) for name in only_local: datadog.api.Monitor.create(**_prepare_monitor(local_monitors[name])) @@ -263,7 +266,7 @@ def command_push(args): changed = [name for name in common_names if _is_changed(local_monitors[name], remote_monitors[name])] if changed: - print "Updating %d modified monitors." % len(changed) + print("Updating %d modified monitors." % len(changed)) for name in changed: datadog.api.Monitor.update( remote_monitors[name]['id'], @@ -273,7 +276,7 @@ def command_push(args): remote_monitors = get_datadog_monitors() untracked = set(remote_monitors.keys()) - set(local_monitors.keys()) if untracked: - print "Deleting %d untracked monitors." % len(untracked) + print("Deleting %d untracked monitors." % len(untracked)) for monitor in untracked: datadog.api.Monitor.delete(remote_monitors[monitor]['id']) @@ -320,14 +323,14 @@ def command_mute(args): if monitor['mute_when'] and remote_monitors.has_key(monitor['name']): remote = remote_monitors[monitor['name']] if remote['is_silenced']: - print "Alert '%s' is already muted. Skipping." % monitor['name'] + print("Alert '%s' is already muted. Skipping." % monitor['name']) continue mute_until = mute_tags[monitor['mute_when']] if mute_until: id = remote['id'] datadog.api.Monitor.mute(id, end=mute_until['timestamp']) - print "Muting alert '%s' until %s" % (monitor['name'], - mute_until['datetime']) + print("Muting alert '%s' until %s" % (monitor['name'], + mute_until['datetime'])) def command_diff(args): @@ -342,23 +345,23 @@ def command_diff(args): remote_monitors[name])] if only_local: - sys.stdout.write(bcolors.WARNING) - print '---------------------------------------------------------' - print ' NEW MONITORS. These monitors are currently missing in' - print ' datadog and can be pushed using "%s push"' % PROGNAME - print '---------------------------------------------------------' - sys.stdout.write(bcolors.ENDC) + sys.stdout.write(WARNING) + print('---------------------------------------------------------') + print(' NEW MONITORS. These monitors are currently missing in') + print(' datadog and can be pushed using "%s push"' % PROGNAME) + print('---------------------------------------------------------') + sys.stdout.write(ENDC) monitors = [local_monitors[name]['obj'] for name in only_local] - print _pretty_yaml(monitors) + print(_pretty_yaml(monitors)) if changed: - sys.stdout.write(bcolors.WARNING) - print '---------------------------------------------------------' - print ' TO BE UPDATED. These monitors exist in datadog, but are' - print ' different than the local version. Use "%s push"' % PROGNAME - print ' to push them to datadog.' - print '---------------------------------------------------------' - print - sys.stdout.write(bcolors.ENDC) + sys.stdout.write(WARNING) + print('---------------------------------------------------------') + print(' TO BE UPDATED. These monitors exist in datadog, but are') + print(' different than the local version. Use "%s push"' % PROGNAME) + print(' to push them to datadog.') + print('---------------------------------------------------------') + print('') + sys.stdout.write(ENDC) for name in changed: remote_name = 'datadog:%s' % name local_name = '%s:%s' % (local_monitors[name]['filename'], name) @@ -367,26 +370,26 @@ def command_diff(args): _pretty_yaml(local_monitors[name]['obj']).splitlines(True), fromfile=remote_name, tofile=local_name): if line.startswith('---') or line.startswith('+++'): - sys.stdout.write(bcolors.BOLD + line + bcolors.ENDC) + sys.stdout.write(BOLD + line + ENDC) elif line.startswith('-'): - sys.stdout.write(bcolors.RED + line + bcolors.ENDC) + sys.stdout.write(RED + line + ENDC) elif line.startswith('+'): - sys.stdout.write(bcolors.GREEN + line + bcolors.ENDC) + sys.stdout.write(GREEN + line + ENDC) else: sys.stdout.write(line) if only_remote and not args.ignore_untracked: - sys.stdout.write(bcolors.WARNING) - print '------------------------------------------------------------' - print ' UNTRACKED MONITORS. These monitors are only in datadog ' - print ' and needed to be MANUALLY added to a local file or removed ' - print ' from datadog. ' - print '------------------------------------------------------------' - sys.stdout.write(bcolors.ENDC) + sys.stdout.write(WARNING) + print('------------------------------------------------------------') + print(' UNTRACKED MONITORS. These monitors are only in datadog ') + print(' and needed to be MANUALLY added to a local file or removed ') + print(' from datadog. ') + print('------------------------------------------------------------') + sys.stdout.write(ENDC) monitors = [remote_monitors[name]['obj'] for name in only_remote] - print _pretty_yaml(monitors) - sys.stdout.write(bcolors.FAIL) - print "*** FAILED *** Untracked monitors found." - sys.stdout.write(bcolors.ENDC) + print(_pretty_yaml(monitors)) + sys.stdout.write(FAIL) + print("*** FAILED *** Untracked monitors found.") + sys.stdout.write(ENDC) if args.exit_status and any((only_local, changed, only_remote and not args.ignore_untracked)): sys.exit(1) diff --git a/requirements.txt b/requirements.txt index e700ae8..d2e8796 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ PyYAML==3.11 datadog==0.10.0 pytz==2015.7 - +six