From 848f2c254735c7c64d88a4b988680b1534655af6 Mon Sep 17 00:00:00 2001
From: DeX77 <dex77@frugalware.org>
Date: Mon, 28 Aug 2023 11:21:32 +0200
Subject: [PATCH 1/3] * update to python3

---
 bindings/python/Makefile.in |  8 +++----
 doc/examples/example.py     |  4 ++--
 pactest/pactest.py          |  2 +-
 pactest/pmdb.py             |  6 ++---
 pactest/pmenv.py            | 44 ++++++++++++++++++-------------------
 pactest/pmfile.py           |  8 +++----
 pactest/pmpkg.py            |  4 ++--
 pactest/pmtest.py           | 28 +++++++++++------------
 pactest/tests/add003.py     |  2 +-
 pactest/tests/add004.py     |  2 +-
 pactest/tests/add012.py     |  2 +-
 pactest/tests/add013.py     |  2 +-
 pactest/tests/add042.py     |  2 +-
 pactest/tests/upgrade030.py |  2 +-
 pactest/tests/upgrade040.py |  2 +-
 pactest/tests/upgrade041.py |  2 +-
 pactest/util.py             | 14 ++++++------
 scripts/copyright.py        |  4 ++--
 scripts/mirrors.py          |  2 +-
 19 files changed, 70 insertions(+), 70 deletions(-)

diff --git a/bindings/python/Makefile.in b/bindings/python/Makefile.in
index 32730a90..8384eccb 100644
--- a/bindings/python/Makefile.in
+++ b/bindings/python/Makefile.in
@@ -2,13 +2,13 @@ top_srcdir = @top_srcdir@
 prefix = @prefix@
 
 CFLAGS = $(subst -Werror,,@CFLAGS@)
-CFLAGS += $(shell python -c 'from distutils import sysconfig; print "-I" + sysconfig.get_python_inc()')
+CFLAGS += $(shell python3 -c 'import sysconfig; print("-I" + sysconfig.get_path("include"))')
 CFLAGS += -I$(top_srcdir)/lib/libpacman
 ifeq ($(shell arch),x86_64)
 CFLAGS += -fPIC
 endif
 LDFLAGS += -L$(top_srcdir)/lib/libpacman/.libs -lpacman
-LIBDIR += $(shell python -c 'from distutils import sysconfig; print sysconfig.get_python_lib()'|sed 's|/usr|$(prefix)|')
+LIBDIR += $(shell python3 -c 'import sysconfig; print(sysconfig.get_path("platlib"))'|sed 's|/usr|$(prefix)|')
 
 all: _pacman.so pacman.pyc
 
@@ -21,12 +21,12 @@ pacman_wrap.o: pacman_wrap.c
 pacman_wrap.c:
 	cp $(top_srcdir)/lib/libpacman/pacman.h ./
 	cp $(top_srcdir)/bindings/pacman.i ./
-	swig -python pacman.i
+	swig -python -py3 -O pacman.i
 	# strip the unnecessary prefixes
 	sed -i 's/^pacman_//;s/^PM_//' pacman.py
 
 pacman.pyc: pacman.py
-	python -c "import compileall; compileall.compile_dir('.',1,'.')"
+	python3 -c "import compileall; compileall.compile_dir('.',1,'.',legacy=True)"
 
 pacman.py: pacman_wrap.c
 
diff --git a/doc/examples/example.py b/doc/examples/example.py
index 39f9049d..c0286083 100644
--- a/doc/examples/example.py
+++ b/doc/examples/example.py
@@ -1,6 +1,6 @@
 import pacman
 
 # this will return with success (0)
-print pacman.initialize("/")
+print(pacman.initialize("/"))
 # this will return with failure (-1) as the lib is already initialized
-print pacman.initialize("/")
+print(pacman.initialize("/"))
diff --git a/pactest/pactest.py b/pactest/pactest.py
index 5d3976b5..503055c0 100644
--- a/pactest/pactest.py
+++ b/pactest/pactest.py
@@ -34,7 +34,7 @@ __version__ = "0.3"
 def usage(retcode):
 	"""
 	"""
-	print "Usage: %s [options] [[--test=<path/to/testfile.py>] ...]\n\n" % __file__
+	print("Usage: %s [options] [[--test=<path/to/testfile.py>] ...]\n\n" % __file__)
 	sys.exit(retcode)
 
 if __name__ == "__main__":
diff --git a/pactest/pmdb.py b/pactest/pmdb.py
index 0b8b815f..e72e9c4a 100644
--- a/pactest/pmdb.py
+++ b/pactest/pmdb.py
@@ -315,7 +315,7 @@ class pmdb:
 		# install
 		if self.treename == "local":
 			empty = 1
-			for value in pkg.install.values():
+			for value in list(pkg.install.values()):
 				if value:
 					empty = 0
 			if not empty:
@@ -351,7 +351,7 @@ class pmdb:
 		dbg("oldpkg.checksum : %s" % oldpkg.checksum)
 		dbg("oldpkg.mtime    : %s" % oldpkg.mtime)
 
-		for key in pkg.mtime.keys():
+		for key in list(pkg.mtime.keys()):
 			if key == "install" \
 			   and oldpkg.mtime[key] == (0, 0, 0) \
 			   and pkg.mtime[key] == (0, 0, 0):
@@ -364,4 +364,4 @@ class pmdb:
 
 if __name__ == "__main__":
 	db = pmdb("local")
-	print db
+	print(db)
diff --git a/pactest/pmenv.py b/pactest/pmenv.py
index 1a18862c..0c0ddc6a 100644
--- a/pactest/pmenv.py
+++ b/pactest/pmenv.py
@@ -57,12 +57,12 @@ class pmenv:
 		"""
 
 		for t in self.testcases:
-			print "=========="*8
-			print "Running '%s'" % t.name.strip(".py")
+			print("=========="*8)
+			print("Running '%s'" % t.name.strip(".py"))
 
 			t.load()
-			print t.description
-			print "----------"*8
+			print(t.description)
+			print("----------"*8)
 
 			t.generate()
 			# Hack for mtimes consistency
@@ -76,42 +76,42 @@ class pmenv:
 			t.run(self.pacman)
 
 			t.check()
-			print "==> Test result"
+			print("==> Test result")
 			if t.result["ko"] == 0:
-				print "\tPASSED"
+				print("\tPASSED")
 			else:
-				print "\tFAILED"
-			print
+				print("\tFAILED")
+			print()
 
 	def results(self):
 		"""
 		"""
 		passed = 0
-		print "=========="*8
-		print "Results"
-		print "----------"*8
+		print("=========="*8)
+		print("Results")
+		print("----------"*8)
 		for test in self.testcases:
 			ok = test.result["ok"]
 			ko = test.result["ko"]
 			rules = len(test.rules)
 			if ko == 0:
-				print "[PASSED]",
+				print("[PASSED]", end=' ')
 				passed += 1
 			else:
-				print "[FAILED]",
-			print test.name.strip(".py").ljust(38),
-			print "Rules:",
-			print "OK = %2u KO = %2u SKIP = %2u" % (ok, ko, rules-(ok+ko))
-		print "----------"*8
+				print("[FAILED]", end=' ')
+			print(test.name.strip(".py").ljust(38), end=' ')
+			print("Rules:", end=' ')
+			print("OK = %2u KO = %2u SKIP = %2u" % (ok, ko, rules-(ok+ko)))
+		print("----------"*8)
 		total = len(self.testcases)
 		failed = total - passed
-		print "TOTAL  = %3u" % total
+		print("TOTAL  = %3u" % total)
 		if total:
-			print "PASSED = %3u (%6.2f%%)" % (passed, float(passed)*100/total)
-			print "FAILED = %3u (%6.2f%%)" % (failed, float(failed)*100/total)
-		print
+			print("PASSED = %3u (%6.2f%%)" % (passed, float(passed)*100/total))
+			print("FAILED = %3u (%6.2f%%)" % (failed, float(failed)*100/total))
+		print()
 
 
 if __name__ == "__main__":
 	env = pmenv("/tmp")
-	print env
+	print(env)
diff --git a/pactest/pmfile.py b/pactest/pmfile.py
index a11c0118..ce10dfd5 100644
--- a/pactest/pmfile.py
+++ b/pactest/pmfile.py
@@ -49,9 +49,9 @@ class pmfile:
 		mtime = getmtime(filename)
 
 		if debug:
-			print "ismodified(%s)" % self.name
-			print "old: %s / %s" % (self.checksum, self.mtime)
-			print "new: %s / %s" % (checksum, mtime)
+			print("ismodified(%s)" % self.name)
+			print("old: %s / %s" % (self.checksum, self.mtime))
+			print("new: %s / %s" % (checksum, mtime))
 
 		if not self.checksum == checksum \
 		   or not (self.mtime[1], self.mtime[2]) == (mtime[1], mtime[2]):
@@ -62,4 +62,4 @@ class pmfile:
 
 if __name__ == "__main__":
 	f = pmfile("/tmp", "foobar")
-	print f
+	print(f)
diff --git a/pactest/pmpkg.py b/pactest/pmpkg.py
index 88171ff3..e555b6c3 100644
--- a/pactest/pmpkg.py
+++ b/pactest/pmpkg.py
@@ -167,7 +167,7 @@ class pmpkg:
 
 		# .INSTALL
 		empty = 1
-		for value in self.install.values():
+		for value in list(self.install.values()):
 			if value:
 				empty = 0
 		if not empty:
@@ -188,4 +188,4 @@ class pmpkg:
 
 if __name__ == "__main__":
 	pkg = pmpkg("dummy")
-	print pkg
+	print(pkg)
diff --git a/pactest/pmtest.py b/pactest/pmtest.py
index 2b03d732..61699dbb 100644
--- a/pactest/pmtest.py
+++ b/pactest/pmtest.py
@@ -88,7 +88,7 @@ class pmtest:
 		self.files = []
 		
 		if os.path.isfile(self.name):
-			execfile(self.name)
+			exec(compile(open(self.name, "rb").read(), self.name, 'exec'))
 		else:
 			err("file %s does not exist!" % self.name)
 
@@ -96,7 +96,7 @@ class pmtest:
 		"""
 		"""
 
-		print "==> Generating test environment"
+		print("==> Generating test environment")
 
 		# Cleanup leftover files from a previous test session
 		if os.path.isdir(self.root):
@@ -114,7 +114,7 @@ class pmtest:
 		for dir in [dbdir, cachedir, syncdir, tmpdir, logdir, etcdir]:
 			if not os.path.isdir(dir):
 				vprint("\t%s" % dir[len(self.root)+1:])
-				os.makedirs(dir, 0755)
+				os.makedirs(dir, 0o755)
 
 		# Configuration file
 		vprint("    Creating configuration file")
@@ -126,7 +126,7 @@ class pmtest:
 		for pkg in self.localpkgs:
 			vprint("\t%s" % os.path.join(TMPDIR, pkg.filename()))
 			pkg.makepkg(tmpdir)
-		for key, value in self.db.iteritems():
+		for key, value in self.db.items():
 			if key == "local":
 				continue
 			for pkg in value.pkgs:
@@ -138,7 +138,7 @@ class pmtest:
 
 		# Populating databases
 		vprint("    Populating databases")
-		for key, value in self.db.iteritems():
+		for key, value in self.db.items():
 			for pkg in value.pkgs:
 				vprint("\t%s/%s" % (key, pkg.fullname()))
 				if key == "local":
@@ -147,7 +147,7 @@ class pmtest:
 
 		# Creating sync database archives
 		vprint("    Creating sync database archives")
-		for key, value in self.db.iteritems():
+		for key, value in self.db.items():
 			if key == "local":
 				continue
 			vprint("\t" + value.treename)
@@ -179,10 +179,10 @@ class pmtest:
 		"""
 
 		if os.path.isfile(PM_LOCK):
-			print "\tERROR: another pacman-g2 session is on-going -- skipping"
+			print("\tERROR: another pacman-g2 session is on-going -- skipping")
 			return
 
-		print "==> Running test"
+		print("==> Running test")
 		vprint("\tpacman-g2 %s" % self.args)
 
 		if "FAKEROOTKEY" in os.environ:
@@ -226,20 +226,20 @@ class pmtest:
 		if self.retcode \
 		   and grep(os.path.join(self.root, LOGFILE),
 		            "you cannot perform this operation unless you are root"):
-			print "\tERROR: pacman-g2 support for fakeroot is not disabled"
+			print("\tERROR: pacman-g2 support for fakeroot is not disabled")
 		# Check if the lock is still there
 		if os.path.isfile(PM_LOCK):
-			print "\tERROR: %s not removed" % PM_LOCK
+			print("\tERROR: %s not removed" % PM_LOCK)
 			os.unlink(PM_LOCK)
 		# Look for a core file
 		if os.path.isfile(os.path.join(self.root, TMPDIR, "core")):
-			print "\tERROR: pacman-g2 dumped a core file"
+			print("\tERROR: pacman-g2 dumped a core file")
 
 	def check(self):
 		"""
 		"""
 
-		print "==> Checking rules"
+		print("==> Checking rules")
 
 		for i in self.rules:
 			success = i.check(self.root, self.retcode, self.db["local"], self.files)
@@ -251,10 +251,10 @@ class pmtest:
 				self.result["ko"] += 1
 			else:
 				msg = "SKIP"
-			print "\t[%s] %s" % (msg, i.rule)
+			print("\t[%s] %s" % (msg, i.rule))
 			i.result = success
 
 
 if __name__ == "__main__":
 	test = pmtest("test1", "./root")
-	print test
+	print(test)
diff --git a/pactest/tests/add003.py b/pactest/tests/add003.py
index bde87649..0a3a41fb 100644
--- a/pactest/tests/add003.py
+++ b/pactest/tests/add003.py
@@ -14,7 +14,7 @@ p3.files = ["bin/pkg3", "usr/man/man1/pkg3.1"]
 for p in p1, p2, p3:
 	self.addpkg(p)
 
-self.args = "-A %s" % " ".join([p.filename() for p in p1, p2, p3])
+self.args = "-A %s" % " ".join([p.filename() for p in (p1, p2, p3)])
 
 self.addrule("PACMAN_RETCODE=0")
 for p in p1, p2, p3:
diff --git a/pactest/tests/add004.py b/pactest/tests/add004.py
index 59ba45c6..31c5c639 100644
--- a/pactest/tests/add004.py
+++ b/pactest/tests/add004.py
@@ -6,7 +6,7 @@ p3 = pmpkg("dummy")
 for p in p1, p2, p3:
 	self.addpkg(p)
 
-self.args = "-A %s" % " ".join([p.filename() for p in p1, p2, p3])
+self.args = "-A %s" % " ".join([p.filename() for p in (p1, p2, p3)])
 
 self.addrule("PACMAN_RETCODE=0")
 self.addrule("PKG_VERSION=dummy|2.0-1")
diff --git a/pactest/tests/add012.py b/pactest/tests/add012.py
index 291d1fba..eb5d9c05 100644
--- a/pactest/tests/add012.py
+++ b/pactest/tests/add012.py
@@ -13,7 +13,7 @@ p2.files = ["bin/foobar",
 for p in p1, p2:
 	self.addpkg(p)
 
-self.args = "-A %s" % " ".join([p.filename() for p in p1, p2])
+self.args = "-A %s" % " ".join([p.filename() for p in (p1, p2)])
 
 self.addrule("PACMAN_RETCODE=1")
 self.addrule("!PKG_EXIST=dummy")
diff --git a/pactest/tests/add013.py b/pactest/tests/add013.py
index 547ce001..728c7bee 100644
--- a/pactest/tests/add013.py
+++ b/pactest/tests/add013.py
@@ -13,7 +13,7 @@ p2.files = ["bin/foobar",
 for p in p1, p2:
 	self.addpkg(p)
 
-self.args = "-Af %s" % " ".join([p.filename() for p in p1, p2])
+self.args = "-Af %s" % " ".join([p.filename() for p in (p1, p2)])
 
 self.addrule("PACMAN_RETCODE=0")
 for p in p1, p2:
diff --git a/pactest/tests/add042.py b/pactest/tests/add042.py
index a9b17c0f..94fd22be 100644
--- a/pactest/tests/add042.py
+++ b/pactest/tests/add042.py
@@ -15,7 +15,7 @@ p3.files = ["bin/dep2"]
 for p in p1, p2, p3:
 	self.addpkg(p)
 
-self.args = "-A %s" % " ".join([p.filename() for p in p1, p2, p3])
+self.args = "-A %s" % " ".join([p.filename() for p in (p1, p2, p3)])
 
 self.addrule("PACMAN_RETCODE=0")
 self.addrule("PKG_VERSION=dummy|1.0-2")
diff --git a/pactest/tests/upgrade030.py b/pactest/tests/upgrade030.py
index 1082c32b..e283d488 100644
--- a/pactest/tests/upgrade030.py
+++ b/pactest/tests/upgrade030.py
@@ -14,7 +14,7 @@ p2 = pmpkg("pkg2", "1.0-2")
 for p in p1, p2:
 	self.addpkg(p)
 
-self.args = "-U %s" % " ".join([p.filename() for p in p1, p2])
+self.args = "-U %s" % " ".join([p.filename() for p in (p1, p2)])
 #self.args = "-Qi %s" % " ".join([p.name for p in lp1, lp2])
 
 self.addrule("PACMAN_RETCODE=0")
diff --git a/pactest/tests/upgrade040.py b/pactest/tests/upgrade040.py
index 6946882e..1bb17805 100644
--- a/pactest/tests/upgrade040.py
+++ b/pactest/tests/upgrade040.py
@@ -20,6 +20,6 @@ p2.files = ["bin/foobar",
 for p in p1, p2:
 	self.addpkg(p)
 
-self.args = "-U %s" % " ".join([p.filename() for p in p1, p2])
+self.args = "-U %s" % " ".join([p.filename() for p in (p1, p2)])
 
 self.addrule("PACMAN_RETCODE=0")
diff --git a/pactest/tests/upgrade041.py b/pactest/tests/upgrade041.py
index 347c2c4b..261a5aac 100644
--- a/pactest/tests/upgrade041.py
+++ b/pactest/tests/upgrade041.py
@@ -20,7 +20,7 @@ p2.files = ["bin/foobar"]
 for p in p1, p2:
 	self.addpkg(p)
 
-self.args = "-U %s" % " ".join([p.filename() for p in p1, p2])
+self.args = "-U %s" % " ".join([p.filename() for p in (p1, p2)])
 
 self.addrule("PACMAN_RETCODE=0")
 for p in p1, p2:
diff --git a/pactest/util.py b/pactest/util.py
index 2b37a31c..d028bde0 100644
--- a/pactest/util.py
+++ b/pactest/util.py
@@ -50,16 +50,16 @@ debug = 0
 
 
 def err(msg):
-	print "error: " + msg
+	print("error: " + msg)
 	sys.exit(1)
 
 def vprint(msg):
 	if verbose:
-		print msg
+		print(msg)
 
 def dbg(msg):
 	if debug:
-		print msg
+		print(msg)
 
 
 #
@@ -106,7 +106,7 @@ def mkfile(name, data = ""):
 		path = os.path.dirname(filename)
 	try:
 		if path and not os.path.isdir(path):
-			os.makedirs(path, 0755)
+			os.makedirs(path, 0o755)
 	except:
 		error("mkfile: could not create directory hierarchy '%s'" % path)
 
@@ -175,7 +175,7 @@ def mkinstallfile(filename, install):
 	"""
 	"""
 	data = []
-	for key, value in install.iteritems():
+	for key, value in install.items():
 		if value:
 			data.append("%s() {\n%s\n}" % (key, value))
 			
@@ -186,14 +186,14 @@ def mkcfgfile(filename, root, option, db):
 	"""
 	# Options
 	data = ["[options]"]
-	for key, value in option.iteritems():
+	for key, value in option.items():
 		data.extend(["%s = %s" % (key, j) for j in value])
 
 	# Repositories
 	data.extend(["[%s]\n" \
 	             "server = file://%s\n" \
 	             % (value.treename, os.path.join(root, SYNCREPO, value.treename)) \
-	             for key, value in db.iteritems() if not key == "local"])
+	             for key, value in db.items() if not key == "local"])
 
 	mkfile(os.path.join(root, filename), "\n".join(data))
 
diff --git a/scripts/copyright.py b/scripts/copyright.py
index dafe2231..bde51d77 100644
--- a/scripts/copyright.py
+++ b/scripts/copyright.py
@@ -68,7 +68,7 @@ class authors:
 				else:
 					sys.stdout.write("%s" % j)
 			sys.stdout.write(" by ")
-			if i.name in self.aliases.keys():
+			if i.name in list(self.aliases.keys()):
 				i.name = self.aliases[i.name]
 			sys.stdout.write("%s\n" % i.name)
 
@@ -91,5 +91,5 @@ for i in ["lib/libpacman"]:
 	for root, dirs, files in os.walk(i):
 		for file in files:
 			if file[-2:] == ".h":
-				print "==> %s/%s:" % (root, file)
+				print(("==> %s/%s:" % (root, file)))
 				gencopy("%s/%s" % (root, file))
diff --git a/scripts/mirrors.py b/scripts/mirrors.py
index 65c7e4bb..d9e2bff1 100644
--- a/scripts/mirrors.py
+++ b/scripts/mirrors.py
@@ -42,7 +42,7 @@ for i in xmldoc.getElementsByTagName('mirror'):
 	except KeyError:
 		mirrors[m.country] = []
 	mirrors[m.country].append(m)
-countries = mirrors.keys()
+countries = list(mirrors.keys())
 countries.sort()
 sock = open(out, "w")
 sock.write("""#
-- 
2.41.0

