nebraska: Remove six.moves modules
This CL completely migrates nebraska to Python 3.
BUG=b:186156191, b:186491865
TEST=./nebraska_unittest.py
TEST=python3 nebraska.py --runtime-root /tmp
Change-Id: I3d0aedae03157254d9323f1ba01986c96dc7eadd
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/2853774
Tested-by: Amin Hassani <[email protected]>
Reviewed-by: Jae Hoon Kim <[email protected]>
Reviewed-by: Congbin Guo <[email protected]>
Commit-Queue: Amin Hassani <[email protected]>
diff --git a/nebraska/nebraska.py b/nebraska/nebraska.py
index 9340ced..df99c67 100755
--- a/nebraska/nebraska.py
+++ b/nebraska/nebraska.py
@@ -14,6 +14,7 @@
import copy
import datetime
import errno
+import http
import json
import logging
import os
@@ -23,14 +24,12 @@
import sys
import threading
import traceback
+import urllib
+from http import server
from xml.dom import minidom
from xml.etree import ElementTree
-from six.moves import BaseHTTPServer
-from six.moves import http_client
-from six.moves import urllib
-
# '5' and '7' are just default values for testing.
_FIRMWARE_VER = '5'
@@ -910,10 +909,10 @@
self._server_thread = None
self._created_runtime_root = False
- class NebraskaHandler(BaseHTTPServer.BaseHTTPRequestHandler):
+ class NebraskaHandler(server.BaseHTTPRequestHandler):
"""HTTP request handler for Omaha requests."""
- def _SendResponse(self, content_type, response, code=http_client.OK):
+ def _SendResponse(self, content_type, response, code=http.client.OK):
"""Sends a given response back to the client.
Args:
@@ -956,7 +955,7 @@
data = self.rfile.read(request_len)
except Exception as err:
logging.error('Failed to read request in do_POST %s', str(err))
- self.send_error(http_client.BAD_REQUEST, 'Invalid request (header).')
+ self.send_error(http.client.BAD_REQUEST, 'Invalid request (header).')
return
parsed_path, parsed_query = self._ParseURL(self.path)
@@ -978,12 +977,12 @@
else:
error_str = 'The requested path "%s" was not found!' % parsed_path
logging.error(error_str)
- self.send_error(http_client.BAD_REQUEST, error_str)
+ self.send_error(http.client.BAD_REQUEST, error_str)
except Exception as err:
logging.error('Failed to handle request (%s)', str(err))
logging.error(traceback.format_exc())
- self.send_error(http_client.INTERNAL_SERVER_ERROR,
+ self.send_error(http.client.INTERNAL_SERVER_ERROR,
traceback.format_exc())
def do_GET(self):
@@ -1001,13 +1000,13 @@
self._SendResponse('text/plain', 'Nebraska is alive!')
else:
logging.error('The requested path "%s" was not found!', parsed_path)
- self.send_error(http_client.BAD_REQUEST,
+ self.send_error(http.client.BAD_REQUEST,
'The requested path "%s" was not found!' % parsed_path)
def Start(self):
"""Starts the nebraska server."""
- self._httpd = BaseHTTPServer.HTTPServer(('', self.GetPort()),
- NebraskaServer.NebraskaHandler)
+ self._httpd = server.HTTPServer(
+ ('', self.GetPort()), NebraskaServer.NebraskaHandler)
self._port = self._httpd.server_port
if self._runtime_root:
diff --git a/nebraska/nebraska_unittest.py b/nebraska/nebraska_unittest.py
index 4428109..28885b2 100755
--- a/nebraska/nebraska_unittest.py
+++ b/nebraska/nebraska_unittest.py
@@ -10,7 +10,9 @@
# pylint: disable=cros-logging-import
import base64
+import builtins
import collections
+import http
import json
import logging
import os
@@ -21,9 +23,6 @@
from xml.etree import ElementTree
import mock
-from six.moves import builtins
-from six.moves import http_client
-
import nebraska
@@ -245,7 +244,7 @@
nebraska_handler.do_POST()
nebraska_handler.send_error.assert_called_once_with(
- http_client.BAD_REQUEST,
+ http.client.BAD_REQUEST,
'The requested path "invalid-path" was not found!')
@mock.patch.object(nebraska, 'traceback')
@@ -259,7 +258,7 @@
self.assertEqual(traceback_mock.format_exc.call_count, 2)
nebraska_handler.send_error.assert_called_once_with(
- http_client.INTERNAL_SERVER_ERROR, traceback_mock.format_exc())
+ http.client.INTERNAL_SERVER_ERROR, traceback_mock.format_exc())
@mock.patch.object(nebraska, 'traceback')
@mock.patch.object(nebraska, 'Response')
@@ -274,7 +273,7 @@
self.assertEqual(traceback_mock.format_exc.call_count, 2)
nebraska_handler.send_error.assert_called_once_with(
- http_client.INTERNAL_SERVER_ERROR, traceback_mock.format_exc())
+ http.client.INTERNAL_SERVER_ERROR, traceback_mock.format_exc())
def testDoPostUpdateConfig(self):
"""Tests do_POST success for update_config API."""
@@ -299,7 +298,7 @@
nebraska_handler.path = 'http://test.com/invalid-path'
nebraska_handler.do_GET()
- nebraska_handler.send_error(http_client.BAD_REQUEST, mock.ANY)
+ nebraska_handler.send_error(http.client.BAD_REQUEST, mock.ANY)
def testDoGetHealthCheck(self):
"""Tests do_GET with health_check path."""
@@ -319,8 +318,7 @@
nebraska_instance.UpdateConfig(update_payloads_address=_PAYLOADS_ADDRESS)
server = nebraska.NebraskaServer(nebraska_instance, port=_NEBRASKA_PORT)
- with mock.patch.object(nebraska.BaseHTTPServer,
- 'HTTPServer') as server_mock:
+ with mock.patch.object(http.server, 'HTTPServer') as server_mock:
with mock.patch.object(nebraska.threading, 'Thread') as thread_mock:
server.Start()
@@ -358,7 +356,7 @@
port_file = os.path.join(runtime_root, 'port')
pid_file = os.path.join(runtime_root, 'pid')
- with mock.patch.object(nebraska.BaseHTTPServer, 'HTTPServer'):
+ with mock.patch.object(http.server, 'HTTPServer'):
with mock.patch.object(nebraska.threading, 'Thread'):
server.Start()