From 19fbb1356cf4873eee8af3ff5a60ca59aee32d21 Mon Sep 17 00:00:00 2001 From: James McClune Date: Sun, 31 Mar 2019 21:42:34 -0400 Subject: [PATCH] hack: refactored code in update_owners.py Introduced pathlib as an alternative to os.path. Updated the urllib2 module to urllib.request. Structured the code to align more with Python3. Signed-off-by: James McClune --- hack/update_owners.py | 50 +++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 26 deletions(-) mode change 100755 => 100644 hack/update_owners.py diff --git a/hack/update_owners.py b/hack/update_owners.py old mode 100755 new mode 100644 index 43a7bdedc0d..d9605237ca6 --- a/hack/update_owners.py +++ b/hack/update_owners.py @@ -14,22 +14,23 @@ # See the License for the specific language governing permissions and # limitations under the License. + import argparse import collections import csv import json -import os +import pathlib import random import re import subprocess import sys import time -import urllib2 +import urllib.request import zlib -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) -OWNERS_PATH = os.path.abspath( - os.path.join(BASE_DIR, '..', 'test', 'test_owners.csv')) + +BASE_DIR = pathlib.Path(__file__).resolve() +OWNERS_PATH = str(BASE_DIR.parents[1] / 'test') OWNERS_JSON_PATH = OWNERS_PATH.replace('.csv', '.json') GCS_URL_BASE = 'https://storage.googleapis.com/kubernetes-test-history/' SKIP_MAINTAINERS = { @@ -46,7 +47,7 @@ def normalize(name): def get_test_history(days_ago): url = time.strftime(GCS_URL_BASE + 'logs/%Y-%m-%d.json', time.gmtime(time.time() - days_ago * 24 * 60 * 60)) - resp = urllib2.urlopen(url) + resp = urllib.request.urlopen(url) content = resp.read() if resp.headers.get('content-encoding') == 'gzip': content = zlib.decompress(content, 15 | 16) @@ -62,7 +63,7 @@ def get_test_names_from_test_history(): def get_test_names_from_local_files(): - tests_json = subprocess.check_output(['go', 'run', 'test/list/main.go', '-json']) + tests_json = subprocess.check_output(['go', 'run', '../test/list/main.go', '-json']) tests = json.loads(tests_json) return {normalize(t['Name'] + (' ' + t['TestName'] if 'k8s.io/' not in t['Name'] else '')) for t in tests} @@ -70,7 +71,7 @@ def get_test_names_from_local_files(): def load_owners(fname): owners = {} - with open(fname) as f: + with pathlib.Path.open(fname) as f: for n, cols in enumerate(csv.reader(f)): if n == 0: continue # header @@ -84,7 +85,7 @@ def load_owners(fname): def write_owners(fname, owners): - with open(fname, 'w') as f: + with pathlib.Path.open(fname, 'w') as f: out = csv.writer(f, lineterminator='\n') out.writerow(['name', 'owner', 'auto-assigned', 'sig']) items = sorted(owners.items()) @@ -93,7 +94,7 @@ def write_owners(fname, owners): def get_maintainers(): - # Github doesn't seem to support team membership listing without a key with + # GitHub doesn't seem to support team membership listing without a key with # org admin privileges. Instead, we do it manually: # Open https://github.com/orgs/kubernetes/teams/kubernetes-maintainers # Run this in the js console: @@ -174,28 +175,26 @@ def main(): prefixes = sig_prefixes(owners) - with open(OWNERS_JSON_PATH, 'w') as f: + with pathlib.Path.open(OWNERS_JSON_PATH, 'w') as f: f.write(prefixes + '\n') if options.print_sig_prefixes: - print prefixes - return + print(prefixes) outdated_tests = sorted(set(owners) - set(test_names)) new_tests = sorted(set(test_names) - set(owners)) maintainers = get_maintainers() - print '# OUTDATED TESTS (%d):' % len(outdated_tests) - print '\n'.join('%s -- %s%s' % + print('# OUTDATED TESTS (%d):' % len(outdated_tests)) + print('\n'.join('%s -- %s%s' % (t, owners[t][0], ['', ' (random)'][owners[t][1]]) - for t in outdated_tests) - print '# NEW TESTS (%d):' % len(new_tests) - print '\n'.join(new_tests) + for t in outdated_tests)) + print('# NEW TESTS (%d):' % len(new_tests)) + print('\n'.join(new_tests)) if options.check: if new_tests or outdated_tests: - print - print 'ERROR: the test list has changed' + print('ERROR: The test list has changed') sys.exit(1) sys.exit(0) @@ -206,13 +205,12 @@ def main(): owners.pop(name) if not options.addonly: - print '# UNEXPECTED MAINTAINERS ', - print '(randomly assigned, but not in kubernetes-maintainers)' + print('# UNEXPECTED MAINTAINERS ',) + print('(randomly assigned, but not in kubernetes-maintainers)') for name, (owner, random_assignment, _) in sorted(owners.iteritems()): if random_assignment and owner not in maintainers: - print '%-16s %s' % (owner, name) + print('%-16s %s', (owner, name)) owners.pop(name) - print owner_counts = collections.Counter( owner for name, (owner, random, sig) in owners.iteritems() @@ -228,9 +226,9 @@ def main(): owners[test_name] = (new_owner, random_assignment, "") if options.user.lower() == 'random': - print '# Tests per maintainer:' + print('# Tests per maintainer:') for owner, count in owner_counts.most_common(): - print '%-20s %3d' % (owner, count) + print('%-20s %3d', (owner, count)) write_owners(OWNERS_PATH, owners)