Skip to content

Commit 0c8fb45

Browse files
vapierstephenfin
authored andcommitted
pwclient: basic python3 support
This fixes a few random issues to make the script work at least somewhat under python 3: - set the default encoding to utf-8 - handle xmlrpclib/xmlrpc.client module renames - handle ConfigParser/configparser module renames - add a unicode() stub for python 3 - fix old style class definition w/Filter - use list comprehension instead of map() - drop the unused version= keyword w/argparse The code still runs under python 2 the same as before, and now works for the most part under python 3 -- the handling of encoded content still needs some work, but that'll require more surgery, and is best left to another commit after this. Signed-off-by: Mike Frysinger <[email protected]> Reviewed-by: Stephen Finucane <[email protected]>
1 parent f9c7cce commit 0c8fb45

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

patchwork/bin/pwclient

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
23
#
34
# Patchwork command line client
45
# Copyright (C) 2008 Nate Case <[email protected]>
@@ -23,24 +24,39 @@ from __future__ import print_function
2324

2425
import os
2526
import sys
26-
import xmlrpclib
27+
try:
28+
import xmlrpclib
29+
except ImportError:
30+
# Python 3 has merged/renamed things.
31+
import xmlrpc.client as xmlrpclib
2732
import argparse
2833
import string
2934
import tempfile
3035
import subprocess
3136
import base64
32-
import ConfigParser
37+
try:
38+
import ConfigParser
39+
except ImportError:
40+
# Python 3 has renamed things.
41+
import configparser as ConfigParser
3342
import shutil
3443
import re
3544

45+
# Add a shim for Python 2's unicode() helper.
46+
try:
47+
unicode
48+
except NameError:
49+
# Python 3 does everything by unicode now.
50+
unicode = str
51+
3652
# Default Patchwork remote XML-RPC server URL
3753
# This script will check the PW_XMLRPC_URL environment variable
3854
# for the URL to access. If that is unspecified, it will fallback to
3955
# the hardcoded default value specified here.
4056
DEFAULT_URL = "http://patchwork/xmlrpc/"
4157
CONFIG_FILE = os.path.expanduser('~/.pwclientrc')
4258

43-
class Filter:
59+
class Filter(object):
4460
"""Filter for selecting patches."""
4561
def __init__(self):
4662
# These fields refer to specific objects, so they are special
@@ -135,7 +151,7 @@ def person_ids_by_name(rpc, name):
135151
if len(name) == 0:
136152
return []
137153
people = rpc.person_list(name, 0)
138-
return map(lambda x: x['id'], people)
154+
return [x['id'] for x in people]
139155

140156
def list_patches(patches, format_str=None):
141157
"""Dump a list of patches to stdout."""
@@ -356,7 +372,7 @@ class _RecursiveHelpAction(argparse._HelpAction):
356372
parser.exit()
357373

358374
def main():
359-
hash_parser = argparse.ArgumentParser(add_help=False, version=False)
375+
hash_parser = argparse.ArgumentParser(add_help=False)
360376
hash_parser.add_argument(
361377
'-h', metavar='HASH', dest='hash', action='store',
362378
help='''Lookup by patch hash'''
@@ -370,7 +386,7 @@ def main():
370386
help='''Lookup patch in project'''
371387
)
372388

373-
filter_parser = argparse.ArgumentParser(add_help=False, version=False)
389+
filter_parser = argparse.ArgumentParser(add_help=False)
374390
filter_parser.add_argument(
375391
'-s', metavar='STATE',
376392
help='''Filter by patch state (e.g., 'New', 'Accepted', etc.)'''
@@ -409,7 +425,7 @@ def main():
409425
'patch_name', metavar='STR', nargs='?',
410426
help='substring to search for patches by name',
411427
)
412-
help_parser = argparse.ArgumentParser(add_help=False, version=False)
428+
help_parser = argparse.ArgumentParser(add_help=False)
413429
help_parser.add_argument(
414430
'--help', action='help', help=argparse.SUPPRESS,
415431
#help='''show this help message and exit'''
@@ -418,7 +434,6 @@ def main():
418434
action_parser = argparse.ArgumentParser(
419435
prog='pwclient',
420436
add_help=False,
421-
version=False,
422437
formatter_class=argparse.RawDescriptionHelpFormatter,
423438
epilog='''(apply | get | info | view | update) (-h HASH | ID [ID ...])''',
424439
)

0 commit comments

Comments
 (0)