Compare commits
3 Commits
master
...
24-decorat
Author | SHA1 | Date | |
---|---|---|---|
d457cbacc2 | |||
1fcc6cd926 | |||
7c48004aaa |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ __pycache__
|
||||
.vscode
|
||||
devenv
|
||||
your_system_information.txt
|
||||
*.db
|
43
decorators.py
Normal file
43
decorators.py
Normal file
@ -0,0 +1,43 @@
|
||||
from functools import wraps
|
||||
|
||||
installed_packages = []
|
||||
|
||||
|
||||
def install_package(pkg):
|
||||
if pkg not in installed_packages:
|
||||
installed_packages.append(pkg)
|
||||
print(f"Installed: {pkg}")
|
||||
else:
|
||||
print(f"Already installed: {pkg}")
|
||||
|
||||
|
||||
def needs_packages(packages):
|
||||
def inner_decorator(function):
|
||||
@wraps(function)
|
||||
def execution_wrapper(*args, **kwargs):
|
||||
[install_package(package) for package in packages]
|
||||
return function(*args, **kwargs)
|
||||
|
||||
return execution_wrapper
|
||||
|
||||
return inner_decorator
|
||||
|
||||
|
||||
@needs_packages(['vim', 'calc'])
|
||||
def add(z1, z2):
|
||||
return z1 + z2
|
||||
|
||||
|
||||
@needs_packages(['calc', 'sed'])
|
||||
def sub(z1, z2):
|
||||
return z1 - z2
|
||||
|
||||
|
||||
@needs_packages(['calc'])
|
||||
def bla(z1, z2):
|
||||
return z1 / z2
|
||||
|
||||
|
||||
print(add(1, 2))
|
||||
print(sub(1, 2))
|
||||
print(bla(1, 2))
|
61
modules/pkg-lister/__init__.py
Normal file
61
modules/pkg-lister/__init__.py
Normal file
@ -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")
|
Loading…
Reference in New Issue
Block a user