From 0eda775e09d2f9b9a98910bffc7886112ba0f53e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 18 May 2017 15:15:44 +0200 Subject: [PATCH] .deb file install management + remove + purge + cleaned and evolved `help` message + -- and - arguments --- command/debinstall | 81 +++++++++++++++++++++ command/fetch | 14 ++++ install => command/install | 10 +-- purge => command/purge | 11 +-- remove => command/remove | 11 +-- update => command/update | 2 +- install_apt-plus.sh | 3 + main | 142 +++++++++++++++++++++++++++---------- manifest | 0 stdout | 10 +++ 10 files changed, 230 insertions(+), 54 deletions(-) create mode 100644 command/debinstall create mode 100644 command/fetch rename install => command/install (87%) mode change 100644 => 100755 rename purge => command/purge (83%) mode change 100644 => 100755 rename remove => command/remove (83%) mode change 100644 => 100755 rename update => command/update (97%) mode change 100644 => 100755 create mode 100755 install_apt-plus.sh mode change 100644 => 100755 main mode change 100644 => 100755 manifest create mode 100644 stdout diff --git a/command/debinstall b/command/debinstall new file mode 100644 index 0000000..fca606c --- /dev/null +++ b/command/debinstall @@ -0,0 +1,81 @@ +#!/usr/bin/env python + +import re; +from os import system as ossystem; +from os import path as ospath; +from sys import argv as sysargv; + +# [1] get absolute path +#========================================================# +path = ospath.dirname(ospath.dirname(ospath.realpath(__file__))); + + +# [2] get @deb_file argument +#========================================================# +deb_file = sysargv[1]; + + +# [3] search for manifest file +#========================================================# +manifest=""; +with open( "%s/manifest" % path, 'r' ) as f: + manifest=f.readline(); + + + +# [4] search for already installed entries in manifest +#========================================================# +alrdy = []; +findPkg = re.compile("^\s*\[[\. x]\]\s([a-zA-Z0-9_ \[\]-]+)$", 0); +with open(manifest, 'r+') as f: + for line in f: + m = findPkg.search(line); + if ( m != None ): + alrdy.append( m.group(1) ) ; + + +# [5] Install the deb_file +#========================================================# +pkgName = ""; + +# (1) Try to install # +print "\033[38;5;208m[deb-install] %s\033[0m" % (deb_file); +exitcode = ossystem("sudo dpkg -i %s > %s/stdout" % (deb_file, path)); + +# (2) Read local stdout copy of stdout # +findName = re.compile("^Unpacking ([a-zA-Z0-9_-]+)", re.MULTILINE) +with open( "%s/stdout" % path, 'r' ) as f: + line = f.read(); + print line; + m = findName.search(line); + + if ( m != None ): + pkgName = "%s [deb]" % m.group(1); + + +# (3) On success and notice user # +if ( exitcode == 0 and len(pkgName) > 0 ): + print "\033[38;5;78msuccess\033[0m"; + +# (4) On failure notice user -> abort # +else: + print "\033[38;5;161merror\033[0m"; + exit(1); + + + + +# [6] Update manifest +#========================================================# +print "\033[38;5;39m[notifying] %s\033[0m" % (pkgName); + +# if new package +if( not pkgName in alrdy ): + with open(manifest, "a") as f: + f.write(" [x] %s\n" % pkgName); +else: + with open(manifest, 'r') as reader: + replaced = reader.read().replace("[ ] %s" % pkgName, "[x] %s" % pkgName).replace("[.] %s" % pkgName, "[x] %s" % pkgName); + + with open(manifest, 'w') as writer: + writer.write( replaced ); diff --git a/command/fetch b/command/fetch new file mode 100644 index 0000000..e15cf0b --- /dev/null +++ b/command/fetch @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +success_f(){ + echo "\033[38;5;78mSUCCESS\033[31;0m"; +} + +abort_f(){ + echo "\033[38;5;161mABORTING\033[31;0m"; + exit; +} + +# Fetching sources list +echo "\033[38;5;208m[1/1] FETCHING PACKAGES\033[31;0m"; +sudo apt-get update && success_f || abort_f; \ No newline at end of file diff --git a/install b/command/install old mode 100644 new mode 100755 similarity index 87% rename from install rename to command/install index 6de3ae1..1dd188d --- a/install +++ b/command/install @@ -1,4 +1,4 @@ -#!/bin/py +#!/usr/bin/env python import re; from os import system as ossystem; @@ -6,7 +6,7 @@ from os import path as ospath; from sys import argv as sysargv; # [1] get absolute path -path = ospath.dirname(ospath.realpath(__file__)); +path = ospath.dirname(ospath.dirname(ospath.realpath(__file__))); # [2] get packages packages = sysargv[1:]; @@ -18,19 +18,19 @@ with open( "%s/manifest" % path, 'r' ) as f: # [4] search for non-installed entries in manifest alrdy = []; +findAlrdy = re.compile("^\s*\[[\. x]\]\s([a-zA-Z0-9_ \[\]-]+)$", 0); with open(manifest, 'r+') as f: for line in f: - m = re.search(r"\s*\[[\. ]\] ([a-zA-Z0-9_-]+)", line); + m = findAlrdy.search(line); if ( m != None ): alrdy.append( m.group(1) ) ; # [5] Install packages installed = []; - for pkg in packages: print "\033[38;5;208m[install](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg); exitcode = ossystem("sudo apt-get install %s" % pkg); - + # if all ran successfully if ( exitcode == 0 ): print "\033[38;5;78msuccess\033[0m"; diff --git a/purge b/command/purge old mode 100644 new mode 100755 similarity index 83% rename from purge rename to command/purge index b941b0d..755cf60 --- a/purge +++ b/command/purge @@ -1,4 +1,4 @@ -#!/bin/py +#!/usr/bin/env python import re; from os import system as ossystem; @@ -6,7 +6,7 @@ from os import path as ospath; from sys import argv as sysargv; # [1] get absolute path -path = ospath.dirname(ospath.realpath(__file__)); +path = ospath.dirname(ospath.dirname(ospath.realpath(__file__))); # [2] get packages packages = sysargv[1:]; @@ -18,9 +18,10 @@ with open( "%s/manifest" % path, 'r' ) as f: # [4] search for installed entries in manifest alrdy = []; +findAlrdy = re.compile("^\s*\[x\]\s(?:([a-zA-Z0-9_-]+)(?: \[deb\])?)$", 0); with open(manifest, 'r+') as f: for line in f: - m = re.search(r"\s*\[x\] ([a-zA-Z0-9_-]+)", line); + m = findAlrdy.search(line); if ( m != None ): alrdy.append( m.group(1) ) ; @@ -30,7 +31,7 @@ purged = []; for pkg in packages: print "\033[38;5;208m[purge](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg); exitcode = ossystem("sudo apt-get purge %s" % pkg); - + # if all ran successfully if ( exitcode == 0 ): print "\033[38;5;78msuccess\033[0m"; @@ -49,7 +50,7 @@ for pkg in purged: f.write(" [ ] %s\n" % pkg); else: with open(manifest, 'r') as reader: - replaced = reader.read().replace("[x] %s" % pkg, "[ ] %s" % pkg); + replaced = reader.read().replace("[x] %s" % pkg, "[ ] %s" % pkg).replace("[x] %s [deb]" % pkg, "[ ] %s [deb]" % pkg); with open(manifest, 'w') as writer: writer.write( replaced ); diff --git a/remove b/command/remove old mode 100644 new mode 100755 similarity index 83% rename from remove rename to command/remove index dc84e34..17b1fd4 --- a/remove +++ b/command/remove @@ -1,4 +1,4 @@ -#!/bin/py +#!/usr/bin/env python import re; from os import system as ossystem; @@ -6,7 +6,7 @@ from os import path as ospath; from sys import argv as sysargv; # [1] get absolute path -path = ospath.dirname(ospath.realpath(__file__)); +path = ospath.dirname(ospath.dirname(ospath.realpath(__file__))); # [2] get packages packages = sysargv[1:]; @@ -18,9 +18,10 @@ with open( "%s/manifest" % path, 'r' ) as f: # [4] search for installed entries in manifest alrdy = []; +findAlrdy = re.compile("^\s*\[x\]\s(?:([a-zA-Z0-9_-]+)(?: \[deb\])?)$", 0); with open(manifest, 'r+') as f: for line in f: - m = re.search(r"\s*\[x\] ([a-zA-Z0-9_-]+)", line); + m = findAlrdy.search(line); if ( m != None ): alrdy.append( m.group(1) ) ; @@ -30,7 +31,7 @@ removed = []; for pkg in packages: print "\033[38;5;208m[remove](%d/%d) %s\033[0m" % (packages.index(pkg)+1, len(packages), pkg); exitcode = ossystem("sudo apt-get remove %s" % pkg); - + # if all ran successfully if ( exitcode == 0 ): print "\033[38;5;78msuccess\033[0m"; @@ -49,7 +50,7 @@ for pkg in removed: f.write(" [.] %s\n" % pkg); else: with open(manifest, 'r') as reader: - replaced = reader.read().replace("[x] %s" % pkg, "[.] %s" % pkg); + replaced = reader.read().replace("[x] %s" % pkg, "[.] %s" % pkg).replace("[x] %s [deb]" % pkg, "[.] %s [deb]" % pkg); with open(manifest, 'w') as writer: writer.write( replaced ); diff --git a/update b/command/update old mode 100644 new mode 100755 similarity index 97% rename from update rename to command/update index fbbc5e0..a3c7a7d --- a/update +++ b/command/update @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env sh success_f(){ echo "\033[38;5;78mSUCCESS\033[31;0m"; diff --git a/install_apt-plus.sh b/install_apt-plus.sh new file mode 100755 index 0000000..a272584 --- /dev/null +++ b/install_apt-plus.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +sudo ln -fs $( dirname $(readlink -f $0) )/main /usr/bin/apt-plus; diff --git a/main b/main old mode 100644 new mode 100755 index 5e20c8a..f5dc544 --- a/main +++ b/main @@ -1,4 +1,4 @@ -#!/bin/py +#!/usr/bin/env python ########### @@ -12,70 +12,136 @@ import sys; # get absolute path path = ospath.dirname(ospath.realpath(__file__)); -# help function +# [1] Help function +#========================================================# def showhelp(): - print "apt-plus 1.0.0 (amd64)"; - print "Usage: apt-plus command arguments"; - print "Usage: apt-plus install|remove|purge pkg1 [pkg2 ...]"; - print "Usage: apt-plus manifest /path/to/manifest.txt"; + print "\033[1mNAME\033[0m" + print "\tapt-plus 1.0.0 (amd64)"; print; - print "apt-plus is a set of shell aliases using apt-get"; - print "in order to automate basic routines"; - print "installed packages are notices in the manifest file"; - print "(default is ~/.x-migration)"; + print "\033[1mSYNOPSIS\033[0m" + print "\tapt-plus "; print; - print "List of commands:"; - print " update - Upgrades packages and kernel and cleans after"; - print " install - Installs packages and notice it in the manifest"; - print " remove - Removes packages and notice it in the manifest"; - print " purge - Purges packages and notice it in the manifest"; - print " manifest - To set the manifest file"; + print "\033[1mDESCRIPTION\033[0m" + print "" + print "\tapt-plus is top-level wrapper of apt-get and dpkg, also"; + print "\tit logs every package you have installed, removed, purged."; + print "\tThese logs are stored in the manifest file (default is ~/.x-migration)."; + print; + print "\033[1mOPTIONS\033[0m" + print "\t-d, --deb "; + print "\t\tInstalls a package from a .deb file and notice it in the manifest."; + print; + print "\t-i, --install "; + print "\t\tInstalls one or more packages and notice it in the manifest, note that"; + print "\t\tthe argument is a list of packages' names separated by spaces."; + print; + print "\t-f, --fetch"; + print "\t\tFetches packages from remote to local."; + print; + print "\t-m --manifest "; + print "\t\tSet the global manifest file for registering packages\n"; + print; + print "\t-p, --purge "; + print "\t\tPurges one or more packages and notice it in the manifest, note that"; + print "\t\tthe is a list of packages' names separated by spaces."; + print "\t\tThe packages configurations won't be kept, --remove will keep them."; + print; + print "\t-r, --remove "; + print "\t\tRemoves one or more packages and notice it in the manifest, note that"; + print "\t\tthe is a list of packages' names separated by spaces."; + print "\t\tThe packages configurations will be kept, --purge won't keep them."; + print; + print "\t-u, --update"; + print "\t\tUpgrades packages and kernel and cleans after"; print; -# If at least 1 arg given +# [2] Command dispatch (if at least 1 arg) +#========================================================# if ( len(sys.argv) > 1 ): - command=sys.argv[1]; + # (1) Get the command argument (first) # + command = sys.argv[1]; + + # (1) --update, -u + #--------------------------------------------------------# + if ( command == "-u" or command == "--update" ): + + ossystem("/usr/bin/env sh %s/command/update;" % path); + + # (2) --fetch, -f + #--------------------------------------------------------# + elif ( command == "-f" or command == "--fetch" ): + + ossystem("/usr/bin/env sh %s/command/fetch;" % path); + + # (3) --deb-install, -d + #--------------------------------------------------------# + elif ( command == "-d" or command == "--deb" ): - # if "updated" - if ( command == "update" ): - ossystem("sh %s/update;" % path); - elif ( command == "install" ): # if package/s given if ( len(sys.argv) < 3 ): - sys.exit("Missing argument : package names"); - + sys.exit("\n(!) Missing argument "); + + ossystem("/usr/bin/env python %s/command/debinstall %s" % (path, sys.argv[2]) ); + + # (4) --install, -i + #--------------------------------------------------------# + elif ( command == "-i" or command == "--install" ): + + # if package/s given + if ( len(sys.argv) < 3 ): + sys.exit("\n(!) Missing argument "); + packages = " ".join(sys.argv[2:]); - ossystem( "python %s/install %s" % (path, packages) ); - elif ( command == "remove" ): + ossystem("/usr/bin/env python %s/command/install %s" % (path, packages) ); + + # (5) --remove, -r + #--------------------------------------------------------# + elif ( command == "-r" or command == "--remove" ): + # if package/s given if ( len(sys.argv) < 3 ): - sys.exit("Missing argument : package names"); - + sys.exit("\n(!) Missing argument "); + packages = " ".join(sys.argv[2:]); - ossystem( "python %s/remove %s" % (path, packages) ); - elif ( command == "purge" ): + ossystem("/usr/bin/env python %s/command/remove %s" % (path, packages) ); + + # (6) --purge, -p + #--------------------------------------------------------# + elif ( command == "-p" or command == "--purge" ): # if package/s given if ( len(sys.argv) < 3 ): - sys.exit("Missing argument : package names"); - + sys.exit("\n(!) Missing argument "); + packages = " ".join(sys.argv[2:]); - ossystem( "python %s/purge %s" % (path, packages) ); - - elif ( command == "manifest" ): - # if package/s given + ossystem("/usr/bin/env python %s/command/purge %s" % (path, packages) ); + + # (7) --manifest, -m + #--------------------------------------------------------# + elif ( command == "-m" or command == "--manifest" ): + + # (1) If argument missing -> abort # if ( len(sys.argv) < 3 ): - sys.exit("Missing argument : manifest file"); + sys.exit("\n(!) Missing argument "); + + # (2) If argument is not a valid file -> abort # elif ( not ospath.isfile(sys.argv[2]) ): sys.exit("Given path isn't a file"); + # (3) Store absolute path in ./manifest file # file = ospath.realpath(sys.argv[2]); with open( ("%s/manifest" % path), "w") as f: f.write(file); + + # (8) If no match -> show help message + #--------------------------------------------------------# else: showhelp(); -# if missing 1 arg + + +# [3] If not enough arguments -> show help message +#========================================================# else: showhelp(); diff --git a/manifest b/manifest old mode 100644 new mode 100755 diff --git a/stdout b/stdout new file mode 100644 index 0000000..737103d --- /dev/null +++ b/stdout @@ -0,0 +1,10 @@ +Selecting previously unselected package gitkraken. +(Reading database ... 323395 files and directories currently installed.) +Preparing to unpack gitkraken-amd64.deb ... +Unpacking gitkraken (2.5.0) ... +Setting up gitkraken (2.5.0) ... +Processing triggers for desktop-file-utils (0.22-1ubuntu5.1) ... +Processing triggers for bamfdaemon (0.5.3~bzr0+16.04.20160824-0ubuntu1) ... +Rebuilding /usr/share/applications/bamf-2.index... +Processing triggers for gnome-menus (3.13.3-6ubuntu3.1) ... +Processing triggers for mime-support (3.59ubuntu1) ...