[Autotest][PY3] Migrating site_utils (server_manager_actions to suite_enumerator)
BUG=chromium:990593
TEST=dummy_pass, suite:dummy
Change-Id: I0ba5fd851d25dd0032de5b7e318a22dca46ee485
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2551385
Tested-by: Derek Beckett <dbeckett@chromium.org>
Commit-Queue: Derek Beckett <dbeckett@chromium.org>
Reviewed-by: Greg Edelston <gredelston@google.com>
diff --git a/site_utils/server_manager_actions.py b/site_utils/server_manager_actions.py
index f323038..f48f585 100644
--- a/site_utils/server_manager_actions.py
+++ b/site_utils/server_manager_actions.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
# Copyright 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -11,6 +12,10 @@
are required to be executed and what actions to executed on which servers.
"""
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
import subprocess
import sys
@@ -85,18 +90,18 @@
servers = server_manager_utils.get_servers(
role=role, status=server_models.Server.STATUS.PRIMARY)
if not servers:
- print >> sys.stderr, ('WARNING! Action %s failed to be applied. No '
- 'server with given role %s was found.' %
- (action, role))
+ print('WARNING! Action %s failed to be applied. No '
+ 'server with given role %s was found.' % (action, role),
+ file=sys.stderr)
return
for server in servers:
- print 'Run command `%s` on server %s' % (command, server.hostname)
+ print('Run command `%s` on server %s' % (command, server.hostname))
try:
infra.execute_command(server.hostname, command)
except subprocess.CalledProcessError as e:
- print >> sys.stderr, ('Failed to check server %s, error: %s' %
- (server.hostname, e))
+ print('Failed to check server %s, error: %s' %
+ (server.hostname, e), file=sys.stderr)
def try_execute(server, roles, enable, post_change,
@@ -147,4 +152,4 @@
message = ('WARNING! Action %s is skipped. Please manually '
'execute the action to make your change effective.' %
str(action))
- print >> sys.stderr, message
+ print(message, file=sys.stderr)
diff --git a/site_utils/server_manager_utils.py b/site_utils/server_manager_utils.py
index a626dd5..b164318 100644
--- a/site_utils/server_manager_utils.py
+++ b/site_utils/server_manager_utils.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
# Copyright 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
@@ -7,6 +8,10 @@
"""
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
import json
import socket
import subprocess
@@ -51,7 +56,7 @@
'removed from server %s. Autotest will not function '
'normally without any server in role %s.' %
(role, exclude_server.hostname, role))
- print >> sys.stderr, message
+ print(message, file=sys.stderr)
def get_servers(hostname=None, role=None, status=None):
@@ -143,8 +148,8 @@
infra.execute_command(hostname, 'true')
return True
except subprocess.CalledProcessError as e:
- print >> sys.stderr, ('Failed to check server %s, error: %s' %
- (hostname, e))
+ print('Failed to check server %s, error: %s' %
+ (hostname, e), file=sys.stderr)
return False
@@ -216,8 +221,8 @@
raise ServerActionError('Server %s does not have attribute %s' %
(server.hostname, attribute))
attributes[0].delete()
- print 'Attribute %s is deleted from server %s.' % (attribute,
- server.hostname)
+ print('Attribute %s is deleted from server %s.' % (attribute,
+ server.hostname))
def change_attribute(server, attribute, value):
@@ -240,13 +245,13 @@
old_value = attributes[0].value
attributes[0].value = value
attributes[0].save()
- print ('Attribute `%s` of server %s is changed from %s to %s.' %
- (attribute, server.hostname, old_value, value))
+ print('Attribute `%s` of server %s is changed from %s to %s.' %
+ (attribute, server.hostname, old_value, value))
else:
server_models.ServerAttribute.objects.create(
server=server, attribute=attribute, value=value)
- print ('Attribute `%s` of server %s is set to %s.' %
- (attribute, server.hostname, value))
+ print('Attribute `%s` of server %s is set to %s.' %
+ (attribute, server.hostname, value))
def get_shards():
diff --git a/site_utils/set_tree_status.py b/site_utils/set_tree_status.py
index 9a9c302..4ddc813 100755
--- a/site_utils/set_tree_status.py
+++ b/site_utils/set_tree_status.py
@@ -9,17 +9,21 @@
# Example usage:
# ./set_tree_status.py [options] "a quoted space separated message."
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
import getpass
import optparse
import os
import sys
-import urllib
+from six.moves import urllib
CHROMEOS_STATUS_SERVER = 'https://chromiumos-status.appspot.com'
def get_status():
- response = urllib.urlopen(CHROMEOS_STATUS_SERVER + '/current?format=raw')
+ response = urllib.request.urlopen(
+ CHROMEOS_STATUS_SERVER + '/current?format=raw')
return response.read()
@@ -35,15 +39,17 @@
if not force:
status = get_status()
if 'tree is closed' in status.lower():
- print >> sys.stderr, 'Tree is already closed for some other reason.'
- print >> sys.stderr, status
+ print('Tree is already closed for some other reason.',
+ file=sys.stderr)
+ print(status, file=sys.stderr)
return -1
data = {
'message': message,
'username': getpass.getuser(),
'password': get_pwd(),
}
- urllib.urlopen(CHROMEOS_STATUS_SERVER + '/status', urllib.urlencode(data))
+ urllib.request.urlopen(CHROMEOS_STATUS_SERVER + '/status',
+ urllib.parse.urlencode(data))
return 0
@@ -55,5 +61,5 @@
help='Dont force to close tree if it is already closed.')
options, args = parser.parse_args()
if not args:
- print >> sys.stderr, 'missing tree close message.'
+ print('missing tree close message.', file=sys.stderr)
sys.exit(post_status(options.force, args[0]))
diff --git a/site_utils/suite_enumerator.py b/site_utils/suite_enumerator.py
index ab352ed..37e949f 100755
--- a/site_utils/suite_enumerator.py
+++ b/site_utils/suite_enumerator.py
@@ -19,6 +19,10 @@
dynamic suite infrastructure in server/cros/dynamic_suite.py.
"""
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
import logging
import optparse, os, sys
@@ -55,7 +59,7 @@
parser, options, args = parse_options()
if options.listall:
if args:
- print 'Cannot use suite_name with --listall'
+ print('Cannot use suite_name with --listall')
parser.print_help()
elif not args or len(args) != 1:
parser.print_help()
@@ -65,7 +69,7 @@
devserver = cros_lib.dev_server.ImageServer('')
if options.listall:
for suite in suite_lib.list_all_suites('', devserver, fs_getter):
- print suite
+ print(suite)
return
suite = suite_lib.Suite.create_from_name(args[0], {}, '', devserver,
@@ -74,19 +78,19 @@
# NOTE: the test.name value can be *different* from the directory
# name that appears in test.path
PRETEST_LIST = ['firmware_FAFTSetup',]
- for test in filter(lambda test: test.name in \
- PRETEST_LIST, suite.tests):
- print test.path
- for test in filter(lambda test: test.name not in \
- PRETEST_LIST, suite.tests):
- print test.path
+ for test in [test for test in suite.tests if test.name in
+ PRETEST_LIST]:
+ print(test.path)
+ for test in [test for test in suite.tests if test.name not in
+ PRETEST_LIST]:
+ print(test.path)
# Check if test_suites/control.suite_name exists.
control_path = os.path.join(options.autotest_dir, 'test_suites',
'control.' + args[0])
if not os.path.exists(control_path):
- print >> sys.stderr, ('Warning! control file is missing: %s' %
- control_path)
+ print('Warning! control file is missing: %s' %
+ control_path, file=sys.stderr)
if __name__ == "__main__":