Make mtedit Python 3 compatible

Due to the way relative imports work in Python 3, mtedit/main.py has to
be moved outside of the mtedit package, as when run using its hashbang
line it is package `__main__`, and therefore cannot use relative
imports.

The other manual changes are:

* to properly distinguish between strings and binary data in
  mtlib/util.py, mtlib/log.py, and mtedit/server.py;
* to switch between methods for getting headers in mtedit/server.py; and
* updating some `import` statements (as well as removing unused ones).

The rest of the changes were done using `futurize`:

    $ futurize --stage1 --all-imports --write --nobackups mtedit/**/*.py

Two features remain broken:

* retrieving logs over SSH fails to retrieve
  /var/log/xorg/cmt_input_events.dat, but that was broken already; and
* editing logs by adding the `-o` switch and clicking the shrink button
  in the Web UI fails to import `MTReplay`, presumably because I haven't
  updated that module yet.

TEST=run `mtedit` with the path of a downloaded ZIP file, choose a log
     to view, and check that you can view the data properly in the
     browser.
BUG=none

Cq-Depend: chromium:2024460
Change-Id: Ie59c191d3bc2827d3e94e45a9a8a38e3d524a1a2
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/mttools/+/2025612
Tested-by: Harry Cutts <hcutts@chromium.org>
Reviewed-by: Sean O'Brien <seobrien@chromium.org>
Commit-Queue: Harry Cutts <hcutts@chromium.org>
diff --git a/mtlib/log.py b/mtlib/log.py
index 2835d11..d0a6797 100644
--- a/mtlib/log.py
+++ b/mtlib/log.py
@@ -8,21 +8,15 @@
 
 from gzip import GzipFile
 from mtlib.feedback import FeedbackDownloader
-try:
-    from StringIO import StringIO
-except ImportError:
-    from io import StringIO
+from io import BytesIO
 from .cros_remote import CrOSRemote
 import base64
 import bz2
-import imghdr
 import os
 import os.path
 import re
-import subprocess
 import sys
 import tarfile
-import tempfile
 import zipfile
 
 # path for temporary screenshot
@@ -135,7 +129,7 @@
     self.downloader = downloader
 
     if id_or_filename.endswith('.zip') or id_or_filename.endswith('.bz2'):
-      self.report = open(id_or_filename).read()
+      self.report = open(id_or_filename, 'rb').read()
       screenshot_filename = id_or_filename[:-4] + '.jpg'
       try:
         self.image = open(screenshot_filename).read()
@@ -169,17 +163,19 @@
     return tar.extractfile(names[-1])
 
   def _ExtractSystemLog(self):
-    if self.report[0:2] == 'BZ':
-      self.system = bz2.decompress(self.report)
-    elif self.report[0:2] == 'PK':
-      io = StringIO(self.report)
-      zip = zipfile.ZipFile(io, 'r')
-      self.system = zip.read(zip.namelist()[0])
+    if self.report[0:2] == b'BZ':
+      system_log = bz2.decompress(self.report)
+    elif self.report[0:2] == b'PK':
+      bytes_io = BytesIO(self.report)
+      zip = zipfile.ZipFile(bytes_io, 'r')
+      system_log = zip.read(zip.namelist()[0])
       zip.extractall('./')
     else:
       print('Cannot download logs file')
       sys.exit(-1)
 
+    self.system = system_log.decode('utf-8', 'strict')
+
   def _ExtractLogFiles(self):
     # Find embedded and uuencoded activity.tar in system log
 
@@ -219,7 +215,7 @@
         return []
 
       # untar
-      activity_tar_file = tarfile.open(fileobj=StringIO(activity_tar_data))
+      activity_tar_file = tarfile.open(fileobj=BytesIO(activity_tar_data))
 
       def ExtractPadFiles(name):
         # find matching evdev file