From 0f69be7e425d39094a5281479c6366f251e3d09c Mon Sep 17 00:00:00 2001 From: B Stack Date: Sat, 6 Jul 2019 21:44:17 -0400 Subject: add apt: list, list available, list installed --- ssp | 54 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 7 deletions(-) diff --git a/ssp b/ssp index f852051..0cf11a9 100755 --- a/ssp +++ b/ssp @@ -11,11 +11,12 @@ # custom tostring for list https://stackoverflow.com/questions/727761/python-str-and-lists # https://stackoverflow.com/questions/4426663/how-to-remove-the-first-item-from-a-list # https://stackoverflow.com/questions/1770209/run-child-processes-as-different-user-from-a-long-running-python-process +# iterate through files https://stackoverflow.com/questions/5733419/how-to-iterate-over-the-file-in-python#5733487 # Improve: # Documentation: # ssp commands.xlsx -sspversion="2019-05-20a" +sspversion="2019-07-06a" import argparse, sys, os, subprocess, re sys.path.append("/usr/share/bgscripts/py") @@ -43,8 +44,12 @@ elif args.debug: debuglevel = args.debug # global variables -package_manager = "yum" -allowed_repos = [ "hosting" ] +if os.path.isfile("/usr/bin/yum"): + package_manager = "yum" + allowed_repos = [ "hosting" ] +elif os.path.isfile("/usr/bin/apt"): + package_manager = "apt" + allowed_repos = [ "download.opensuse.org_repositories_home:_bgstack15_Debian%5fUnstable_Packages" ] #allowed_repos_string = ",".join(map(str, allowed_repos)) params = args.positionals assume = True if args.assumeyes else False @@ -57,6 +62,7 @@ assume = True if args.assumeyes else False # wrap around yum list [*], yum list installed [*], yum list available [*] def list_(action, pkg_mgr, args, repos): + command_array=["yum","-q","list"] __return=[] # add either installed or available @@ -78,9 +84,43 @@ def list_(action, pkg_mgr, args, repos): for line in __output.splitlines(): if regex.match(str(line)): __return.append(line.decode('UTF-8','backslashreplace')) + elif pkg_mgr == "apt": - eprint("list_ for " + pkg_mgr + "not yet implemented.") - sys.exit(1) + + # if action is undefined, so equivalent of yum list for apt + if action != "installed": + regex = re.compile("^Package:") + for item in allowed_repos: + with open("/var/lib/apt/lists/" + item, 'r') as f: + for g in f: + if regex.match(str(g)): + if g.split()[1] not in __return: + __return.append(g.split()[1]) + + elif action == "installed": + # need to run dpkg -l $( list ) + __input=list_("list", pkg_mgr, args, repos) + command_array=["dpkg","-l"] + for item in list_("list", pkg_mgr, args, repos): + command_array.append(item) + #print(command_array) + try: + __output = subprocess.check_output(command_array, stderr=subprocess.DEVNULL) + except subprocess.CalledProcessError as e: + #__output = "" + __output = e.output.decode("utf-8") + #__output = str(e.output.decode,'utf-8') + pass + #print(__output) + for line in __output.split('\n'): + #print(line) + try: + a = line.split()[1] + if "Status" not in a and "Err?" not in a and "Name" != a: + __return.append(a) + except: + pass + return __return # wrap around yum install [*], @@ -103,7 +143,7 @@ def install_(action, pkg_mgr, args, repos, assume): # if regex.match(str(line)): # print(line.decode('UTF-8','backslashreplace')) elif pkg_mgr == "apt": - eprint("list_ for " + pkg_mgr + "not yet implemented.") + eprint("install_ for " + pkg_mgr + "not yet implemented.") sys.exit(1) # wrap around yum remove [*], @@ -126,7 +166,7 @@ def remove_(pkg_mgr, args, repos, assume): # if regex.match(str(line)): # print(line.decode('UTF-8','backslashreplace')) elif pkg_mgr == "apt": - eprint("list_ for " + pkg_mgr + "not yet implemented.") + eprint("remove_ for " + pkg_mgr + "not yet implemented.") sys.exit(1) # wrap around yum clean cache for approved repos -- cgit