From 1fcc6cd9261b8c3c1dd740ebb62f5384710c733b Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 22 Aug 2020 05:16:54 +0200 Subject: [PATCH] Add new package-lister module --- modules/pkg-lister/__init__.py | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 modules/pkg-lister/__init__.py diff --git a/modules/pkg-lister/__init__.py b/modules/pkg-lister/__init__.py new file mode 100644 index 0000000..9f71ae2 --- /dev/null +++ b/modules/pkg-lister/__init__.py @@ -0,0 +1,61 @@ +import argparse +import sqlite3 +import subprocess + +# cmd = """dpkg-query -f '\{"package": "${Package}", "version": "${Version}", "arch": "${Architecture}", "source": "${Source}", "desc": "${binary:Synopsis}"\}\\n' -W""" + +cmd = """dpkg-query -f '${Package}\\t${Version}\\t${Architecture}\\t${Source}\\t${binary:Synopsis}\\n' -W""" +db = sqlite3.connect("pkg-list.db") +cur = db.cursor() + +parser = argparse.ArgumentParser() +parser.add_argument("-s", "--search") +args = parser.parse_args() + + +def load_list(): + global cur + cur.execute("DROP TABLE IF EXISTS Packages") + cur.execute( + """CREATE TABLE + Packages( + pkg TEXT PRIMARY KEY, + version TEXT NOT NULL, + architecture TEXT NOT NULL, + parent TEXT, + description TEXT NOT NULL + )""" + ) + packages = subprocess.check_output(cmd, shell=True).decode("UTF-8") + for line in packages.splitlines(): + name, version, arch, source, desc = line.split("\t") + cur.execute( + "INSERT OR REPLACE INTO Packages VALUES (?,?,?,?,?)", + (name, version, arch, source, desc) + ) + db.commit() + + +def list_packages(): + global cur + results = cur.execute("SELECT * FROM Packages") + for res in results.fetchall(): + print(res) + + +def is_installed(pkg): + global cur + results = cur.execute("SELECT version FROM Packages WHERE pkg = ?", (pkg,)) + return len(results.fetchall()) > 0 + + +def what_does(pkg): + global cur + results = cur.execute("SELECT description FROM Packages WHERE pkg = ?", (pkg,)).fetchone() + return results[0] if results and len(results) > 0 else "Package was not found" + + +load_list() +# list_packages() +# print(is_installed("sed")) +print(f"{args.search} is installed") if is_installed(args.search) else print(f"{args.search} is not installed")