acloud: fix py3 compatible issue: NameError: of name 'raw_input' is not defined
1 replace raw_input as six.moves.input.
2 replace __builtins__.raw_input as six.moves.input
3 replace __builtins__.open as six.moves.builtins.open.
BUG: 137195528
BUG: 144319256
Test: atest acloud_test --host &&
acloud-dev create &
acloud-dev setup &
acloud-dev reconnect &
acloud-dev setup
Change-Id: I1dd4e97a8c35e66639e7be8f5152f4713cd8fd35
diff --git a/internal/lib/gcompute_client.py b/internal/lib/gcompute_client.py
index 4389561..3c0b295 100755
--- a/internal/lib/gcompute_client.py
+++ b/internal/lib/gcompute_client.py
@@ -32,6 +32,7 @@
import logging
import os
import re
+
import six
from acloud import errors
diff --git a/internal/lib/gcompute_client_test.py b/internal/lib/gcompute_client_test.py
index 4571f11..d46a236 100644
--- a/internal/lib/gcompute_client_test.py
+++ b/internal/lib/gcompute_client_test.py
@@ -20,8 +20,8 @@
import os
import unittest
-import six
import mock
+import six
# pylint: disable=import-error
import apiclient.http
@@ -1249,7 +1249,7 @@
fake_ssh_rsa_path = "/path/to/test_rsa.pub"
self.Patch(os.path, "exists", return_value=True)
m = mock.mock_open(read_data=self.SSHKEY)
- with mock.patch("__builtin__.open", m):
+ with mock.patch.object(six.moves.builtins, "open", m):
result = gcompute_client.GetRsaKey(fake_ssh_rsa_path)
self.assertEqual(self.SSHKEY, result)
@@ -1381,7 +1381,7 @@
self.Patch(
gcompute_client.ComputeClient, "GetInstance",
return_value=instance_metadata_key_not_exist)
- with mock.patch("__builtin__.open", m):
+ with mock.patch.object(six.moves.builtins, "open", m):
self.compute_client.AddSshRsaInstanceMetadata(
fake_user,
"/path/to/test_rsa.pub",
@@ -1397,7 +1397,7 @@
self.Patch(
gcompute_client.ComputeClient, "GetInstance",
return_value=instance_metadata_key_exist)
- with mock.patch("__builtin__.open", m):
+ with mock.patch.object(six.moves.builtins, "open", m):
self.compute_client.AddSshRsaInstanceMetadata(
fake_user,
"/path/to/test_rsa.pub",
diff --git a/internal/lib/utils.py b/internal/lib/utils.py
index d038b78..d19d448 100755
--- a/internal/lib/utils.py
+++ b/internal/lib/utils.py
@@ -488,7 +488,7 @@
Returns:
String, input from user.
"""
- return str(raw_input(colors + question + TextColors.ENDC).strip())
+ return str(six.moves.input(colors + question + TextColors.ENDC).strip())
def GetUserAnswerYes(question):
@@ -871,7 +871,7 @@
while True:
try:
- choice = raw_input("Enter your choice[0-%d]: " % max_choice)
+ choice = six.moves.input("Enter your choice[0-%d]: " % max_choice)
choice = int(choice)
except ValueError:
print("'%s' is not a valid integer.", choice)
diff --git a/internal/lib/utils_test.py b/internal/lib/utils_test.py
index 4b3cfe8..480b4d2 100644
--- a/internal/lib/utils_test.py
+++ b/internal/lib/utils_test.py
@@ -26,6 +26,7 @@
import unittest
import mock
+import six
from acloud import errors
from acloud.internal.lib import driver_test_lib
@@ -181,7 +182,7 @@
mock_open = mock.mock_open(read_data=public_key)
self.Patch(subprocess, "check_output")
self.Patch(os, "rename")
- with mock.patch("__builtin__.open", mock_open):
+ with mock.patch.object(six.moves.builtins, "open", mock_open):
utils.CreateSshKeyPairIfNotExist(private_key, public_key)
self.assertEqual(subprocess.check_output.call_count, 1) #pylint: disable=no-member
subprocess.check_output.assert_called_with( #pylint: disable=no-member
@@ -252,7 +253,7 @@
mock.call(16)
])
- @mock.patch("__builtin__.raw_input")
+ @mock.patch.object(six.moves, "input")
def testGetAnswerFromList(self, mock_raw_input):
"""Test GetAnswerFromList."""
answer_list = ["image1.zip", "image2.zip", "image3.zip"]
diff --git a/setup/gcp_setup_runner.py b/setup/gcp_setup_runner.py
index 3cc22b9..14619c4 100644
--- a/setup/gcp_setup_runner.py
+++ b/setup/gcp_setup_runner.py
@@ -21,6 +21,8 @@
import re
import subprocess
+import six
+
from acloud import errors
from acloud.internal.lib import utils
from acloud.public import config
@@ -417,9 +419,9 @@
self.client_id = None
self.client_secret = None
while _InputIsEmpty(self.client_id):
- self.client_id = str(raw_input("Enter Client ID: ").strip())
+ self.client_id = str(six.moves.input("Enter Client ID: ").strip())
while _InputIsEmpty(self.client_secret):
- self.client_secret = str(raw_input("Enter Client Secret: ").strip())
+ self.client_secret = str(six.moves.input("Enter Client Secret: ").strip())
UpdateConfigFile(self.config_path, "client_id", self.client_id)
UpdateConfigFile(self.config_path, "client_secret", self.client_secret)
diff --git a/setup/gcp_setup_runner_test.py b/setup/gcp_setup_runner_test.py
index f469740..7fc1fbf 100644
--- a/setup/gcp_setup_runner_test.py
+++ b/setup/gcp_setup_runner_test.py
@@ -18,6 +18,7 @@
import unittest
import os
import mock
+import six
# pylint: disable=no-name-in-module,import-error,no-member
from acloud import errors
@@ -104,7 +105,7 @@
self.assertEqual(self.gcp_env_runner.project, "new_project")
self.assertEqual(self.gcp_env_runner.zone, "new_zone")
- @mock.patch("__builtin__.raw_input")
+ @mock.patch.object(six.moves, "input")
def testSetupClientIDSecret(self, mock_id):
"""Test setup client ID and client secret."""
self.gcp_env_runner.client_id = "fake_client_id"