diff options
author | Eric S. Raymond <esr@thyrsus.com> | 2016-02-18 09:07:59 -0500 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2016-02-18 09:07:59 -0500 |
commit | 9311a6d7b6c1522f338f010d3e1a6b741c031325 (patch) | |
tree | 2ec237502213f2bfc58cf5a7b3911b7a0fb2a4a7 | |
parent | Port to Python 3. (diff) | |
download | irker-9311a6d7b6c1522f338f010d3e1a6b741c031325.tar.xz |
Tweak code to run on either Python2 or Python3.
Reverts some 2to3 botches.
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | NEWS | 2 | ||||
-rwxr-xr-x | irk | 4 | ||||
-rwxr-xr-x | irkerd | 27 | ||||
-rwxr-xr-x | irkerhook.py | 11 |
5 files changed, 27 insertions, 19 deletions
@@ -61,7 +61,7 @@ clean: PYLINTOPTS = --rcfile=/dev/null --reports=n \ --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" \ --dummy-variables-rgx='^_' -SUPPRESSIONS = "C0103,C0111,C0301,C0302,C1001,R0201,R0902,R0903,R0912,R0913,R0914,R0915,E1101,W0142,W0201,W0212,W0621,W0702,W0703,W1201,F0401,E0611" +SUPPRESSIONS = "C0103,C0111,C0301,C0302,C0330,C1001,R0201,R0902,R0903,R0912,R0913,R0914,R0915,E1101,W0142,W0201,W0212,W0621,W0702,W0703,W1201,F0401,E0611" pylint: @pylint $(PYLINTOPTS) --disable=$(SUPPRESSIONS) irkerd @pylint $(PYLINTOPTS) --disable=$(SUPPRESSIONS) irkerhook.py @@ -1,7 +1,7 @@ irker history Repository head: - irkerd and irk have been moved to Python 3 (but irkerhook.py left in Python 2). + Code now runs under either Python 2 or Python 3 2.15: 2016-01-12 Emergency backout of getaddrinfo, it randomly hangs. @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python # Illustrates how to test irkerd. # # First argument must be a channel URL. If it does not begin with "irc", @@ -21,7 +21,7 @@ def connect(server = DEFAULT_SERVER): def send(s, target, message): data = {"to": target, "privmsg" : message} #print(json.dumps(data)) - s.sendall(bytes(json.dumps(data), "ascii")) + s.sendall(bytes(json.dumps(data, encoding="ascii"))) def irk(target, message, server = DEFAULT_SERVER): s = connect(server) @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python """ irkerd - a simple IRC multiplexer daemon @@ -17,6 +17,7 @@ resource page at <http://www.catb.org/~esr/irker/>. Requires Python 2.7, or: * 2.6 with the argparse package installed. +* Any 3.x """ # SPDX-License-Identifier: BSD-2-Clause @@ -53,7 +54,7 @@ import os.path try: # Python 3 import queue except ImportError: # Python 2 - import queue as queue + import Queue as queue import random import re import select @@ -62,7 +63,7 @@ import socket try: # Python 3 import socketserver except ImportError: # Python 2 - import socketserver as socketserver + import SocketServer as socketserver import ssl import sys import threading @@ -71,7 +72,7 @@ import traceback try: # Python 3 import urllib.parse as urllib_parse except ImportError: # Python 2 - import urllib.parse as urllib_parse + import urlparse as urllib_parse LOG = logging.getLogger(__name__) @@ -79,7 +80,7 @@ LOG.setLevel(logging.ERROR) LOG_LEVELS = ['critical', 'error', 'warning', 'info', 'debug'] try: # Python 2 - UNICODE_TYPE = str + UNICODE_TYPE = unicode except NameError: # Python 3 UNICODE_TYPE = str @@ -339,9 +340,9 @@ class IRCServerConnection(): command = None arguments = None self.handle_event(Event("every_raw_message", - self.real_server_name, - None, - [line])) + self.real_server_name, + None, + [line])) m = IRCServerConnection.command_re.match(line) if m.group("prefix"): @@ -745,7 +746,7 @@ class Dispatcher: # scavenged. ancients = [] for connection in connections: - for (chan, age) in list(connections.channels_joined.items()): + for (chan, age) in connections.channels_joined.items(): if age < time.time() - CHANNEL_TTL: ancients.append((connection, chan, age)) if ancients: @@ -848,13 +849,13 @@ class Irker: "Log all messages when in watcher mode." if self.logfile: with open(self.logfile, "ab") as logfp: - message = "%03f|%s|%s\n" % \ + message = u"%03f|%s|%s\n" % \ (time.time(), event.source, event.arguments[0]) logfp.write(message.encode('utf-8')) def pending(self): "Do we have any pending message traffic?" - return [k for (k, v) in list(self.servers.items()) if v.pending()] + return [k for (k, v) in self.servers.items() if v.pending()] def _parse_request(self, line): "Request-parsing helper for the handle() method" @@ -901,7 +902,7 @@ class Irker: self.servers[target.server()].dispatch( target.channel, message, target.key, quit_after=quit_after) # GC dispatchers with no active connections - servernames = list(self.servers.keys()) + servernames = self.servers.keys() for servername in servernames: if not self.servers[servername].live(): del self.servers[servername] @@ -918,7 +919,7 @@ class Irker: # that message activity is likely to be clumpy. if len(self.servers) >= CONNECTION_MAX: oldest = min( - list(self.servers.keys()), + self.servers.keys(), key=lambda name: self.servers[name].last_xmit()) del self.servers[oldest] except InvalidRequest as e: diff --git a/irkerhook.py b/irkerhook.py index 2795dc4..a5232c3 100755 --- a/irkerhook.py +++ b/irkerhook.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python # Copyright (c) 2012 Eric S. Raymond <esr@thyrsus.com> # Distributed under BSD terms. # @@ -43,13 +43,20 @@ version = "2.13" import os, sys, commands, socket, urllib, subprocess, locale, datetime, re from pipes import quote as shellquote + try: import simplejson as json # Faster, also makes us Python-2.5-compatible except ImportError: import json +try: + getstatusoutput = subprocess.getstatusoutput +except AttributeError: + import commands + getstatusoutput = commands.getstatusoutput + def do(command): - return unicode(commands.getstatusoutput(command)[1], locale.getlocale()[1] or 'UTF-8').encode(locale.getlocale()[1] or 'UTF-8') + return unicode(getstatusoutput(command)[1], locale.getlocale()[1] or 'UTF-8').encode(locale.getlocale()[1] or 'UTF-8') class Commit: def __init__(self, extractor, commit): |