Skip to content

Commit daff861

Browse files
authored
Merge pull request #577 from offbyone/py2-to-3
Python 3 Upgrade (minimal)
2 parents e2ebd69 + aa13325 commit daff861

33 files changed

+1377
-7968
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/_cache
55
/_output
66
/.venv
7+
/data

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
FROM ubuntu:bionic
1+
FROM ubuntu:noble
22

33
RUN apt-get update \
44
&& apt-get install -y --no-install-recommends \
5-
python-pip python-setuptools python-wheel \
5+
python3-pip python3-setuptools python3-wheel \
66
locales tzdata \
77
ca-certificates \
88
strace gdb lsof locate net-tools htop iputils-ping dnsutils \
9-
python2.7-dbg python2.7 libpython2.7 python-dbg libpython-dbg \
9+
python3-dbg libpython3-dbg \
1010
curl nano vim tree less telnet patch \
1111
graphviz sqlite3 \
1212
dumb-init \
@@ -25,9 +25,9 @@ WORKDIR /planet
2525
ENTRYPOINT ["dumb-init"]
2626

2727
RUN echo "#!/bin/bash -eux \n\
28-
python2.7 code/planet.py config/config.ini \n\
28+
python3.12 code/planet.py config/config.ini \n\
2929
cd /srv/planetpython.org/ \n\
30-
python2.7 -mSimpleHTTPServer 8080 \n\
30+
python3.12 -m http.server 8080 \n\
3131
"> /start.sh
3232
RUN chmod +x /start.sh
3333
EXPOSE 8080

README.md

Whitespace-only changes.

code/planet-cache.py

Lines changed: 151 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,194 +1,228 @@
1-
#!/usr/bin/env python
2-
# -*- coding: UTF-8 -*-
3-
"""Planet cache tool.
1+
#!/usr/bin/env python3
2+
"""Planet cache tool."""
43

5-
"""
6-
7-
__authors__ = [ "Scott James Remnant <scott@netsplit.com>",
8-
"Jeff Waugh <jdub@perkypants.org>" ]
4+
__authors__ = [
5+
"Scott James Remnant <scott@netsplit.com>",
6+
"Jeff Waugh <jdub@perkypants.org>",
7+
]
98
__license__ = "Python"
109

1110

11+
import argparse
12+
import configparser
1213
import os
14+
import shelve
1315
import sys
1416
import time
15-
import dbhash
16-
import ConfigParser
1717

1818
import planet
1919

2020

2121
def usage():
22-
print "Usage: planet-cache [options] CACHEFILE [ITEMID]..."
23-
print
24-
print "Examine and modify information in the Planet cache."
25-
print
26-
print "Channel Commands:"
27-
print " -C, --channel Display known information on the channel"
28-
print " -L, --list List items in the channel"
29-
print " -K, --keys List all keys found in channel items"
30-
print
31-
print "Item Commands (need ITEMID):"
32-
print " -I, --item Display known information about the item(s)"
33-
print " -H, --hide Mark the item(s) as hidden"
34-
print " -U, --unhide Mark the item(s) as not hidden"
35-
print
36-
print "Other Options:"
37-
print " -h, --help Display this help message and exit"
22+
print("Usage: planet-cache [options] CACHEFILE [ITEMID]...")
23+
print()
24+
print("Examine and modify information in the Planet cache.")
25+
print()
26+
print("Channel Commands:")
27+
print(" -C, --channel Display known information on the channel")
28+
print(" -L, --list List items in the channel")
29+
print(" -K, --keys List all keys found in channel items")
30+
print()
31+
print("Item Commands (need ITEMID):")
32+
print(" -I, --item Display known information about the item(s)")
33+
print(" -H, --hide Mark the item(s) as hidden")
34+
print(" -U, --unhide Mark the item(s) as not hidden")
35+
print()
36+
print("Other Options:")
37+
print(" -h, --help Display this help message and exit")
3838
sys.exit(0)
3939

40+
4041
def usage_error(msg, *args):
41-
print >>sys.stderr, msg, " ".join(args)
42-
print >>sys.stderr, "Perhaps you need --help ?"
42+
print(msg, " ".join(args), file=sys.stderr)
43+
print("Perhaps you need --help ?", file=sys.stderr)
4344
sys.exit(1)
4445

46+
4547
def print_keys(item, title):
4648
keys = item.keys()
47-
keys.sort()
48-
key_len = max([ len(k) for k in keys ])
49+
key_len = max([len(k) for k in sorted(keys)])
4950

50-
print title + ":"
51-
for key in keys:
51+
print(title + ":")
52+
for key in sorted(keys):
5253
if item.key_type(key) == item.DATE:
5354
value = time.strftime(planet.TIMEFMT_ISO, item[key])
5455
else:
5556
value = str(item[key])
56-
print " %-*s %s" % (key_len, key, fit_str(value, 74 - key_len))
57+
print(" %-*s %s" % (key_len, key, fit_str(value, 74 - key_len)))
58+
5759

5860
def fit_str(string, length):
5961
if len(string) <= length:
6062
return string
6163
else:
62-
return string[:length-4] + " ..."
64+
return string[: length - 4] + " ..."
6365

6466

6567
if __name__ == "__main__":
66-
cache_file = None
67-
want_ids = 0
6868
ids = []
6969

70-
command = None
71-
72-
for arg in sys.argv[1:]:
73-
if arg == "-h" or arg == "--help":
74-
usage()
75-
elif arg == "-C" or arg == "--channel":
76-
if command is not None:
77-
usage_error("Only one command option may be supplied")
78-
command = "channel"
79-
elif arg == "-L" or arg == "--list":
80-
if command is not None:
81-
usage_error("Only one command option may be supplied")
82-
command = "list"
83-
elif arg == "-K" or arg == "--keys":
84-
if command is not None:
85-
usage_error("Only one command option may be supplied")
86-
command = "keys"
87-
elif arg == "-I" or arg == "--item":
88-
if command is not None:
89-
usage_error("Only one command option may be supplied")
90-
command = "item"
91-
want_ids = 1
92-
elif arg == "-H" or arg == "--hide":
93-
if command is not None:
94-
usage_error("Only one command option may be supplied")
95-
command = "hide"
96-
want_ids = 1
97-
elif arg == "-U" or arg == "--unhide":
98-
if command is not None:
99-
usage_error("Only one command option may be supplied")
100-
command = "unhide"
101-
want_ids = 1
102-
elif arg.startswith("-"):
103-
usage_error("Unknown option:", arg)
104-
else:
105-
if cache_file is None:
106-
cache_file = arg
107-
elif want_ids:
108-
ids.append(arg)
109-
else:
110-
usage_error("Unexpected extra argument:", arg)
111-
112-
if cache_file is None:
70+
parser = argparse.ArgumentParser(
71+
description="Examine and modify information in the Planet cache."
72+
)
73+
parser.add_argument(
74+
"-C",
75+
"--channel",
76+
action="store_const",
77+
const="channel",
78+
dest="command",
79+
help="Display known information on the channel",
80+
)
81+
parser.add_argument(
82+
"-L",
83+
"--list",
84+
action="store_const",
85+
const="list",
86+
dest="command",
87+
help="List items in the channel",
88+
)
89+
parser.add_argument(
90+
"-K",
91+
"--keys",
92+
action="store_const",
93+
const="keys",
94+
dest="command",
95+
help="List all keys found in channel items",
96+
)
97+
parser.add_argument(
98+
"-I",
99+
"--item",
100+
action="store_const",
101+
const="item",
102+
dest="command",
103+
help="Display known information about the item(s)",
104+
)
105+
parser.add_argument(
106+
"-H",
107+
"--hide",
108+
action="store_const",
109+
const="hide",
110+
dest="command",
111+
help="Mark the item(s) as hidden",
112+
)
113+
parser.add_argument(
114+
"-U",
115+
"--unhide",
116+
action="store_const",
117+
const="unhide",
118+
dest="command",
119+
help="Mark the item(s) as not hidden",
120+
)
121+
parser.add_argument("cache_file", help="Cache file to operate on")
122+
parser.add_argument(
123+
"item_ids",
124+
nargs="*",
125+
help="Item IDs to operate on when using item-related commands",
126+
)
127+
128+
args = parser.parse_args()
129+
130+
# Check if more than one command option was supplied
131+
if "command" not in args or args.command is None:
132+
usage_error("One command option must be supplied.")
133+
elif (
134+
len(
135+
{
136+
key
137+
for key, value in vars(args).items()
138+
if key == "command" and value is not None
139+
}
140+
)
141+
> 1
142+
):
143+
usage_error("Only one command option may be supplied")
144+
145+
# Handle missing cache_file
146+
if not args.cache_file:
113147
usage_error("Missing expected cache filename")
114-
elif want_ids and not len(ids):
148+
149+
# Handle commands that require item IDs
150+
if args.command in ["item", "hide", "unhide"] and not args.item_ids:
115151
usage_error("Missing expected entry ids")
116152

117153
# Open the cache file directly to get the URL it represents
118154
try:
119-
db = dbhash.open(cache_file)
120-
url = db["url"]
121-
db.close()
122-
except dbhash.bsddb._db.DBError, e:
123-
print >>sys.stderr, cache_file + ":", e.args[1]
124-
sys.exit(1)
155+
with shelve.open(args.cache_file, "r") as db:
156+
url = db["url"]
125157
except KeyError:
126-
print >>sys.stderr, cache_file + ": Probably not a cache file"
158+
print(f"{args.cache_file}: Probably not a cache file", file=sys.stderr)
159+
sys.exit(1)
160+
except Exception as e:
161+
print(f"{args.cache_file}: {e!s}", file=sys.stderr)
127162
sys.exit(1)
128163

129164
# Now do it the right way :-)
130-
my_planet = planet.Planet(ConfigParser.ConfigParser())
131-
my_planet.cache_directory = os.path.dirname(cache_file)
165+
my_planet = planet.Planet(configparser.ConfigParser())
166+
my_planet.cache_directory = os.path.dirname(args.cache_file)
132167
channel = planet.Channel(my_planet, url)
133168

134-
for item_id in ids:
169+
for item_id in args.item_ids:
135170
if not channel.has_item(item_id):
136-
print >>sys.stderr, item_id + ": Not in channel"
171+
print(item_id + ": Not in channel", file=sys.stderr)
137172
sys.exit(1)
138173

139174
# Do the user's bidding
140-
if command == "channel":
175+
if args.command == "channel":
141176
print_keys(channel, "Channel Keys")
142177

143-
elif command == "item":
144-
for item_id in ids:
178+
elif args.command == "item":
179+
for item_id in args.item_ids:
145180
item = channel.get_item(item_id)
146181
print_keys(item, "Item Keys for %s" % item_id)
147182

148-
elif command == "list":
149-
print "Items in Channel:"
150-
for item in channel.items(hidden=1, sorted=1):
151-
print " " + item.id
152-
print " " + time.strftime(planet.TIMEFMT_ISO, item.date)
183+
elif args.command == "list":
184+
print("Items in Channel:")
185+
for item in channel.items(hidden=True, sorted=True):
186+
print(" " + item.id)
187+
print(" " + time.strftime(planet.TIMEFMT_ISO, item.date))
153188
if hasattr(item, "title"):
154-
print " " + fit_str(item.title, 70)
189+
print(" " + fit_str(item.title, 70))
155190
if hasattr(item, "hidden"):
156-
print " (hidden)"
191+
print(" (hidden)")
157192

158-
elif command == "keys":
193+
elif args.command == "keys":
159194
keys = {}
160195
for item in channel.items():
161-
for key in item.keys():
196+
for key in item:
162197
keys[key] = 1
163198

164-
keys = keys.keys()
165-
keys.sort()
199+
keys = sorted(keys.keys())
166200

167-
print "Keys used in Channel:"
201+
print("Keys used in Channel:")
168202
for key in keys:
169-
print " " + key
170-
print
203+
print(" " + key)
204+
print()
171205

172-
print "Use --item to output values of particular items."
206+
print("Use --item to output values of particular items.")
173207

174-
elif command == "hide":
175-
for item_id in ids:
208+
elif args.command == "hide":
209+
for item_id in args.item_ids:
176210
item = channel.get_item(item_id)
177211
if hasattr(item, "hidden"):
178-
print item_id + ": Already hidden."
212+
print(item_id + ": Already hidden.")
179213
else:
180214
item.hidden = "yes"
181215

182216
channel.cache_write()
183-
print "Done."
217+
print("Done.")
184218

185-
elif command == "unhide":
186-
for item_id in ids:
219+
elif args.command == "unhide":
220+
for item_id in args.item_ids:
187221
item = channel.get_item(item_id)
188222
if hasattr(item, "hidden"):
189-
del(item.hidden)
223+
del item.hidden
190224
else:
191-
print item_id + ": Not hidden."
225+
print(item_id + ": Not hidden.")
192226

193227
channel.cache_write()
194-
print "Done."
228+
print("Done.")

0 commit comments

Comments
 (0)