From d39b7a24c2b5a75aa1b5228f17f5f91fe6f30f38 Mon Sep 17 00:00:00 2001 From: Gregoire Bellon Gervais Date: Thu, 12 Mar 2020 16:27:27 +0100 Subject: [PATCH] [python3] Migrated spreaper python file to python3 style --- src/packaging/bin/spreaper | 63 +++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/packaging/bin/spreaper b/src/packaging/bin/spreaper index 6f2266f07..9b8fd6cd9 100755 --- a/src/packaging/bin/spreaper +++ b/src/packaging/bin/spreaper @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Copyright 2014-2017 Spotify AB # Copyright 2016-2019 The Last Pickle Ltd @@ -22,7 +22,8 @@ import logging import os import subprocess import urllib -import urlparse +from urllib.parse import urlparse +from urllib.parse import urljoin import sys import requests @@ -92,7 +93,7 @@ class ReaperCaller(object): printq("Access to this operation seems to be restricted. Please login before making any call to the API:") printq("spreaper login ") if not str(r.status_code).startswith("2"): - print r.text + print(r.text) r.raise_for_status() if 'Location' in r.headers: # any non 303/307 response with a 'Location' header, is a successful mutation pointing to the resource @@ -103,23 +104,23 @@ class ReaperCaller(object): return r.text def get(self, endpoint): - the_url = urlparse.urljoin(self.base_url, endpoint) + the_url = urllib.parse.urljoin(self.base_url, endpoint) return self._http_req("GET", the_url, cookies=COOKIES) def post(self, endpoint, **params): - the_url = urlparse.urljoin(self.base_url, endpoint) + the_url = urllib.parse.urljoin(self.base_url, endpoint) return self._http_req("POST", the_url, params=params) def postFormData(self, endpoint, payload): - the_url = urlparse.urljoin(self.base_url, endpoint) + the_url = urllib.parse.urljoin(self.base_url, endpoint) return self._http_req("POST_FORM", the_url, params=payload) def put(self, endpoint, **params): - the_url = urlparse.urljoin(self.base_url, endpoint) + the_url = urllib.parse.urljoin(self.base_url, endpoint) return self._http_req("PUT", the_url, params=params) def delete(self, endpoint, **params): - the_url = urlparse.urljoin(self.base_url, endpoint) + the_url = urllib.parse.urljoin(self.base_url, endpoint) return self._http_req("DELETE", the_url, params=params) @@ -405,22 +406,22 @@ class ReaperCLI(object): def __init__(self): if len(sys.argv) < 2: - print REAPER_USAGE + print(REAPER_USAGE) exit(1) commands = [arg for arg in sys.argv[1:] if not arg[0].startswith('-')] if len(commands) < 1: - print REAPER_USAGE + print(REAPER_USAGE) exit(1) command = commands[0].replace('-', '_') if not hasattr(self, command): - print 'Unrecognized command: {0}'.format(command) - print REAPER_USAGE + print('Unrecognized command: {0}'.format(command)) + print(REAPER_USAGE) exit(1) # use dispatch pattern to invoke method with same name as given command try: getattr(self, command)() - except requests.exceptions.HTTPError, err: - print "" + except requests.exceptions.HTTPError as err: + print("") print("# HTTP request failed with err: {}".format(err)) exit(2) @@ -464,12 +465,12 @@ class ReaperCLI(object): def save_jwt(jwt): try: os.makedirs(os.path.expanduser('~/.reaper')) - os.chmod(os.path.expanduser('~/.reaper'),0700) + os.chmod(os.path.expanduser('~/.reaper'),0o700) except OSError: pass with open(os.path.expanduser('~/.reaper/jwt'),'w+') as f: f.write(jwt) - os.chmod(os.path.expanduser('~/.reaper/jwt'),0600) + os.chmod(os.path.expanduser('~/.reaper/jwt'),0o600) printq("# JWT saved") def login(self): @@ -513,7 +514,7 @@ class ReaperCLI(object): if cluster_names: printq("# Found {0} clusters:".format(len(cluster_names))) for cluster_name in cluster_names: - print cluster_name + print(cluster_name) else: printq("# No registered clusters found") @@ -529,7 +530,7 @@ class ReaperCLI(object): endpoint = '{0}?{1}'.format(endpoint, urllib.urlencode({'state': args.state, 'cluster_name': args.cluster, 'keyspace_name': args.keyspace})) repair_runs = json.loads(reaper.get(endpoint)) printq("# Found {0} repair runs".format(len(repair_runs))) - print json.dumps(repair_runs, indent=2, sort_keys=True) + print(json.dumps(repair_runs, indent=2, sort_keys=True)) def list_segments(self): reaper, args = ReaperCLI.prepare_reaper( @@ -540,7 +541,7 @@ class ReaperCLI(object): printq("# Listing segments for repair run '{0}'".format(args.run_id)) segments = json.loads(reaper.get("repair_run/{0}/segments".format(args.run_id))) printq("# Found {0} segments".format(len(segments))) - print json.dumps(segments, indent=2, sort_keys=True) + print(json.dumps(segments, indent=2, sort_keys=True)) def list_schedules(self): reaper, args = ReaperCLI.prepare_reaper( @@ -555,7 +556,7 @@ class ReaperCLI(object): printq("# Listing repair schedules for all clusters") data = json.loads(reaper.get("repair_schedule")) printq("# Found {0} schedules:".format(len(data))) - print json.dumps(data, indent=2, sort_keys=True) + print(json.dumps(data, indent=2, sort_keys=True)) def list_snapshots(self): reaper, args = ReaperCLI.prepare_reaper( @@ -567,12 +568,12 @@ class ReaperCLI(object): printq("# Listing snapshots for cluster '{0}'".format(args.cluster)) snapshots = json.loads(reaper.get("snapshot/cluster/{0}".format(args.cluster))) printq("# Found {0} snapshots".format(len(snapshots))) - print json.dumps(snapshots, indent=2, sort_keys=True) + print(json.dumps(snapshots, indent=2, sort_keys=True)) else: printq("# Listing snapshots for cluster '{0}' and node '{1}'".format(args.cluster, args.node)) snapshots = json.loads(reaper.get("snapshot/{0}/{1}".format(args.cluster, args.node))) printq("# Found {0} snapshots".format(len(snapshots))) - print json.dumps(snapshots, indent=2, sort_keys=True) + print(json.dumps(snapshots, indent=2, sort_keys=True)) def take_snapshot(self): reaper, args = ReaperCLI.prepare_reaper( @@ -617,7 +618,7 @@ class ReaperCLI(object): snapshot = json.loads(reply) printq("# Snapshot taken") - print json.dumps(snapshot, indent=2, sort_keys=True) + print(json.dumps(snapshot, indent=2, sort_keys=True)) def delete_snapshot(self): reaper, args = ReaperCLI.prepare_reaper( @@ -649,7 +650,7 @@ class ReaperCLI(object): ) printq("# Cluster '{0}':".format(args.cluster_name)) cluster_data = reaper.get("cluster/{0}".format(args.cluster_name)) - print json.dumps(json.loads(cluster_data), indent=2, sort_keys=True) + print(json.dumps(json.loads(cluster_data), indent=2, sort_keys=True)) def status_keyspace(self): reaper, args = ReaperCLI.prepare_reaper( @@ -659,7 +660,7 @@ class ReaperCLI(object): ) printq("# Cluster '{0}', keyspace '{1}':".format(args.cluster_name, args.keyspace_name)) keyspace_data = reaper.get("cluster/{0}/{1}".format(args.cluster_name, args.keyspace_name)) - print json.dumps(json.loads(keyspace_data), indent=2, sort_keys=True) + print(json.dumps(json.loads(keyspace_data), indent=2, sort_keys=True)) def status_repair(self): reaper, args = ReaperCLI.prepare_reaper( @@ -669,7 +670,7 @@ class ReaperCLI(object): ) printq("# Repair run with id '{0}':".format(args.run_id)) repair_run_data = reaper.get("repair_run/{0}".format(args.run_id)) - print json.dumps(json.loads(repair_run_data), indent=2, sort_keys=True) + print(json.dumps(json.loads(repair_run_data), indent=2, sort_keys=True)) def status_schedule(self): reaper, args = ReaperCLI.prepare_reaper( @@ -679,7 +680,7 @@ class ReaperCLI(object): ) printq("# Repair schedule with id '{0}':".format(args.schedule_id)) repair_schedule_data = reaper.get("repair_schedule/{0}".format(args.schedule_id)) - print json.dumps(json.loads(repair_schedule_data), indent=2, sort_keys=True) + print(json.dumps(json.loads(repair_schedule_data), indent=2, sort_keys=True)) def add_cluster(self): reaper, args = ReaperCLI.prepare_reaper( @@ -692,7 +693,7 @@ class ReaperCLI(object): cluster_data = reaper.postFormData("cluster/auth", payload=payload) printq("# Registration succeeded:") - print json.dumps(json.loads(cluster_data), indent=2, sort_keys=True) + print(json.dumps(json.loads(cluster_data), indent=2, sort_keys=True)) def delete_cluster(self): reaper, args = ReaperCLI.prepare_reaper( @@ -755,7 +756,7 @@ class ReaperCLI(object): printq("# Starting repair run with id={0}".format(repair_run.get('id'))) reply = reaper.put("repair_run/{0}/state/{1}".format(repair_run.get('id'), "RUNNING")) repair_run = json.loads(reply) - print json.dumps(repair_run, indent=2, sort_keys=True) + print(json.dumps(repair_run, indent=2, sort_keys=True)) def schedule(self): reaper, args = ReaperCLI.prepare_reaper( @@ -810,7 +811,7 @@ class ReaperCLI(object): repairThreadCount=args.repair_threads) repair_schedule = json.loads(reply) printq("# Repair schedule with id={0} created:".format(repair_schedule.get('id'))) - print json.dumps(repair_schedule, indent=2, sort_keys=True) + print(json.dumps(repair_schedule, indent=2, sort_keys=True)) def resume_repair(self): reaper, args = ReaperCLI.prepare_reaper( @@ -894,7 +895,7 @@ class ReaperCLI(object): def printq(message): if not quiet: - print message + print(message) if __name__ == '__main__': log.info("# ------------------------------------------------------------------------------------")