Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: python2 to 3 #569

Closed
wants to merge 10 commits into from
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM ubuntu:bionic
FROM ubuntu:noble

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

RUN echo "#!/bin/bash -eux \n\
python2.7 code/planet.py config/config.ini \n\
python3.12 code/planet.py config/config.ini \n\
cd /srv/planetpython.org/ \n\
python2.7 -mSimpleHTTPServer 8080 \n\
python3.12 -m http.server 8080 \n\
"> /start.sh
RUN chmod +x /start.sh
EXPOSE 8080
Expand Down
120 changes: 58 additions & 62 deletions code/planet-cache.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""Planet cache tool.
#!/usr/bin/env python3
"""Planet cache tool."""

"""

__authors__ = [ "Scott James Remnant <scott@netsplit.com>",
"Jeff Waugh <jdub@perkypants.org>" ]
__authors__ = ["Scott James Remnant <scott@netsplit.com>", "Jeff Waugh <jdub@perkypants.org>"]
__license__ = "Python"


import configparser
import shelve
import os
import sys
import time
import dbhash
import ConfigParser

import planet


def usage():
print "Usage: planet-cache [options] CACHEFILE [ITEMID]..."
print
print "Examine and modify information in the Planet cache."
print
print "Channel Commands:"
print " -C, --channel Display known information on the channel"
print " -L, --list List items in the channel"
print " -K, --keys List all keys found in channel items"
print
print "Item Commands (need ITEMID):"
print " -I, --item Display known information about the item(s)"
print " -H, --hide Mark the item(s) as hidden"
print " -U, --unhide Mark the item(s) as not hidden"
print
print "Other Options:"
print " -h, --help Display this help message and exit"
print("Usage: planet-cache [options] CACHEFILE [ITEMID]...")
print()
print("Examine and modify information in the Planet cache.")
print()
print("Channel Commands:")
print(" -C, --channel Display known information on the channel")
print(" -L, --list List items in the channel")
print(" -K, --keys List all keys found in channel items")
print()
print("Item Commands (need ITEMID):")
print(" -I, --item Display known information about the item(s)")
print(" -H, --hide Mark the item(s) as hidden")
print(" -U, --unhide Mark the item(s) as not hidden")
print()
print("Other Options:")
print(" -h, --help Display this help message and exit")
sys.exit(0)


def usage_error(msg, *args):
print >>sys.stderr, msg, " ".join(args)
print >>sys.stderr, "Perhaps you need --help ?"
print(msg, " ".join(args), file=sys.stderr)
print("Perhaps you need --help ?", file=sys.stderr)
sys.exit(1)


def print_keys(item, title):
keys = item.keys()
keys.sort()
key_len = max([ len(k) for k in keys ])
key_len = max([len(k) for k in keys])

print title + ":"
print(title + ":")
for key in keys:
if item.key_type(key) == item.DATE:
value = time.strftime(planet.TIMEFMT_ISO, item[key])
else:
value = str(item[key])
print " %-*s %s" % (key_len, key, fit_str(value, 74 - key_len))
print(" %-*s %s" % (key_len, key, fit_str(value, 74 - key_len)))


def fit_str(string, length):
if len(string) <= length:
return string
else:
return string[:length-4] + " ..."
return string[: length - 4] + " ..."


if __name__ == "__main__":
Expand Down Expand Up @@ -101,13 +100,12 @@ def fit_str(string, length):
want_ids = 1
elif arg.startswith("-"):
usage_error("Unknown option:", arg)
elif cache_file is None:
cache_file = arg
elif want_ids:
ids.append(arg)
else:
if cache_file is None:
cache_file = arg
elif want_ids:
ids.append(arg)
else:
usage_error("Unexpected extra argument:", arg)
usage_error("Unexpected extra argument:", arg)

if cache_file is None:
usage_error("Missing expected cache filename")
Expand All @@ -116,24 +114,23 @@ def fit_str(string, length):

# Open the cache file directly to get the URL it represents
try:
db = dbhash.open(cache_file)
url = db["url"]
db.close()
except dbhash.bsddb._db.DBError, e:
print >>sys.stderr, cache_file + ":", e.args[1]
with shelve.open(cache_file, "r") as db:
url = db[b"url"].decode("utf-8")
except shelve.error as e:
print(f"{cache_file}: {e!s}", file=sys.stderr)
sys.exit(1)
except KeyError:
print >>sys.stderr, cache_file + ": Probably not a cache file"
print(f"{cache_file}: Probably not a cache file", file=sys.stderr)
sys.exit(1)

# Now do it the right way :-)
my_planet = planet.Planet(ConfigParser.ConfigParser())
my_planet = planet.Planet(configparser.ConfigParser())
my_planet.cache_directory = os.path.dirname(cache_file)
channel = planet.Channel(my_planet, url)

for item_id in ids:
if not channel.has_item(item_id):
print >>sys.stderr, item_id + ": Not in channel"
print(item_id + ": Not in channel", file=sys.stderr)
sys.exit(1)

# Do the user's bidding
Expand All @@ -146,49 +143,48 @@ def fit_str(string, length):
print_keys(item, "Item Keys for %s" % item_id)

elif command == "list":
print "Items in Channel:"
print("Items in Channel:")
for item in channel.items(hidden=1, sorted=1):
print " " + item.id
print " " + time.strftime(planet.TIMEFMT_ISO, item.date)
print(" " + item.id)
print(" " + time.strftime(planet.TIMEFMT_ISO, item.date))
if hasattr(item, "title"):
print " " + fit_str(item.title, 70)
print(" " + fit_str(item.title, 70))
if hasattr(item, "hidden"):
print " (hidden)"
print(" (hidden)")

elif command == "keys":
keys = {}
for item in channel.items():
for key in item.keys():
for key in item:
keys[key] = 1

keys = keys.keys()
keys.sort()
keys = sorted(keys.keys())

print "Keys used in Channel:"
print("Keys used in Channel:")
for key in keys:
print " " + key
print
print(" " + key)
print()

print "Use --item to output values of particular items."
print("Use --item to output values of particular items.")

elif command == "hide":
for item_id in ids:
item = channel.get_item(item_id)
if hasattr(item, "hidden"):
print item_id + ": Already hidden."
print(item_id + ": Already hidden.")
else:
item.hidden = "yes"

channel.cache_write()
print "Done."
print("Done.")

elif command == "unhide":
for item_id in ids:
item = channel.get_item(item_id)
if hasattr(item, "hidden"):
del(item.hidden)
del item.hidden
else:
print item_id + ": Not hidden."
print(item_id + ": Not hidden.")

channel.cache_write()
print "Done."
print("Done.")
Loading