port mitmproxy.scripts to py3

This commit is contained in:
Maximilian Hils 2016-03-19 19:53:27 +01:00
parent 4be9074b49
commit c52c59f858
10 changed files with 37 additions and 24 deletions

View File

@ -20,9 +20,9 @@ matrix:
os: osx
osx_image: xcode7.1
- python: 3.5
env: SCOPE="netlib"
env: SCOPE="netlib ./test/mitmproxy/script"
- python: 3.5
env: SCOPE="netlib" NO_ALPN=1
env: SCOPE="netlib ./test/mitmproxy/script" NO_ALPN=1
- python: 2.7
env: DOCS=1
script: 'cd docs && make html'

View File

@ -13,7 +13,7 @@ requests, the query parameters are passed as the ``query`` keyword argument.
"""
from __future__ import (absolute_import, print_function, division)
import cStringIO
from six.moves import cStringIO as StringIO
import json
import logging
import subprocess
@ -397,7 +397,7 @@ class ViewImage(View):
def __call__(self, data, **metadata):
try:
img = Image.open(cStringIO.StringIO(data))
img = Image.open(StringIO(data))
except IOError:
return None
parts = [

View File

@ -27,7 +27,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from ASWBXML import ASWBXML
from .ASWBXML import ASWBXML
import logging
class ASCommandResponse:

View File

@ -30,10 +30,10 @@ THE SOFTWARE.
import xml.dom.minidom
import logging
from ASWBXMLCodePage import ASWBXMLCodePage
from ASWBXMLByteQueue import ASWBXMLByteQueue
from GlobalTokens import GlobalTokens
from InvalidDataException import InvalidDataException
from .ASWBXMLCodePage import ASWBXMLCodePage
from .ASWBXMLByteQueue import ASWBXMLByteQueue
from .GlobalTokens import GlobalTokens
from .InvalidDataException import InvalidDataException
class ASWBXML:
versionByte = 0x03

View File

@ -27,7 +27,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from Queue import Queue
from six.moves.queue import Queue
import logging
class ASWBXMLByteQueue(Queue):

View File

@ -1,5 +1,5 @@
from __future__ import absolute_import
import Queue
from six.moves import queue
import threading
@ -27,7 +27,7 @@ class Reply:
def __init__(self, obj):
self.obj = obj
self.q = Queue.Queue()
self.q = queue.Queue()
self.acked = False
def __call__(self, msg=None):
@ -56,7 +56,7 @@ class Channel:
try:
# The timeout is here so we can handle a should_exit event.
g = m.reply.q.get(timeout=0.5)
except Queue.Empty: # pragma: no cover
except queue.Empty: # pragma: no cover
continue
return g
@ -98,7 +98,7 @@ class Master(object):
server may be None if no server is needed.
"""
self.server = server
self.masterq = Queue.Queue()
self.masterq = queue.Queue()
self.should_exit = threading.Event()
def tick(self, q, timeout):
@ -113,7 +113,7 @@ class Master(object):
self.handle(*msg)
q.task_done()
changed = True
except Queue.Empty:
except queue.Empty:
pass
return changed

View File

@ -1,5 +1,5 @@
from __future__ import (absolute_import, print_function, division)
import Cookie
from six.moves import http_cookies as Cookie
import copy
import warnings
from email.utils import parsedate_tz, formatdate, mktime_tz

View File

@ -47,7 +47,7 @@ class ScriptThread(threading.Thread):
def concurrent(fn):
if fn.func_name in (
if fn.__name__ in (
"request",
"response",
"error",
@ -60,4 +60,4 @@ def concurrent(fn):
return _concurrent
raise NotImplementedError(
"Concurrent decorator not supported for '%s' method." % fn.func_name)
"Concurrent decorator not supported for '%s' method." % fn.__name__)

View File

@ -8,6 +8,9 @@ import os
import shlex
import traceback
import sys
import six
from ..exceptions import ScriptException
@ -40,7 +43,8 @@ class Script(object):
def parse_command(command):
if not command or not command.strip():
raise ScriptException("Empty script command.")
if os.name == "nt": # Windows: escape all backslashes in the path.
# Windows: escape all backslashes in the path.
if os.name == "nt": # pragma: no cover
backslashes = shlex.split(command, posix=False)[0].count("\\")
command = command.replace("\\", "\\\\", backslashes)
args = shlex.split(command) # pragma: nocover
@ -71,10 +75,15 @@ class Script(object):
self.ns = {'__file__': os.path.abspath(self.args[0])}
sys.path.append(script_dir)
try:
execfile(self.args[0], self.ns, self.ns)
with open(self.filename) as f:
code = compile(f.read(), self.filename, 'exec')
exec (code, self.ns, self.ns)
except Exception as e:
# Python 3: use exception chaining, https://www.python.org/dev/peps/pep-3134/
raise ScriptException(traceback.format_exc(e))
six.reraise(
ScriptException,
ScriptException(str(e)),
sys.exc_info()[2]
)
finally:
sys.path.pop()
return self.run("start", self.args)
@ -103,6 +112,10 @@ class Script(object):
try:
return f(self.ctx, *args, **kwargs)
except Exception as e:
raise ScriptException(traceback.format_exc(e))
six.reraise(
ScriptException,
ScriptException(str(e)),
sys.exc_info()[2]
)
else:
return None

View File

@ -3,7 +3,7 @@ import shutil
import tempfile
import argparse
import sys
from cStringIO import StringIO
from six.moves import cStringIO as StringIO
from contextlib import contextmanager
from unittest.case import SkipTest